christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1 | // Copyright 2007-2008 Google Inc. 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 | Add text to introduce, |
| 31 | |
| 32 | point back to code.google.com/apis/v8/index.html |
| 33 | |
| 34 | etc etc etc |
| 35 | */ |
| 36 | #ifndef _V8 |
| 37 | #define _V8 |
| 38 | |
| 39 | #include <stdio.h> |
| 40 | |
| 41 | #ifdef _WIN32 |
| 42 | typedef int int32_t; |
| 43 | typedef unsigned int uint32_t; |
| 44 | typedef unsigned short uint16_t; |
| 45 | typedef long long int64_t; |
| 46 | #else |
| 47 | #include <stdint.h> |
| 48 | #endif |
| 49 | |
| 50 | /** |
| 51 | * The v8 javascript engine. |
| 52 | */ |
| 53 | namespace v8 { |
| 54 | |
| 55 | class Context; |
| 56 | class String; |
| 57 | class Value; |
| 58 | class Utils; |
| 59 | class Number; |
| 60 | class Object; |
| 61 | class Array; |
| 62 | class Int32; |
| 63 | class Uint32; |
| 64 | class External; |
| 65 | class Primitive; |
| 66 | class Boolean; |
| 67 | class Integer; |
| 68 | class Function; |
| 69 | class Date; |
| 70 | class ImplementationUtilities; |
| 71 | class Signature; |
| 72 | template <class T> class Handle; |
| 73 | template <class T> class Local; |
| 74 | template <class T> class Persistent; |
| 75 | class FunctionTemplate; |
| 76 | class ObjectTemplate; |
| 77 | class Data; |
| 78 | |
| 79 | |
| 80 | // --- W e a k H a n d l e s |
| 81 | |
| 82 | |
| 83 | /** |
| 84 | * A weak reference callback function. |
| 85 | * |
| 86 | * \param object the weak global object to be reclaimed by the garbage collector |
| 87 | * \param parameter the value passed in when making the weak global object |
| 88 | */ |
| 89 | typedef void (*WeakReferenceCallback)(Persistent<Object> object, |
| 90 | void* parameter); |
| 91 | |
| 92 | |
| 93 | // --- H a n d l e s --- |
| 94 | |
| 95 | #define TYPE_CHECK(T, S) \ |
| 96 | while (false) { \ |
| 97 | *(static_cast<T**>(0)) = static_cast<S*>(0); \ |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * An object reference managed by the v8 garbage collector. |
| 102 | * |
| 103 | * All objects returned from v8 have to be tracked by the garbage |
| 104 | * collector so that it knows that the objects are still alive. Also, |
| 105 | * because the garbage collector may move objects, it is unsafe to |
| 106 | * point directly to an object. Instead, all objects are stored in |
| 107 | * handles which are known by the garbage collector and updated |
| 108 | * whenever an object moves. Handles should always be passed by value |
| 109 | * (except in cases like out-parameters) and they should never be |
| 110 | * allocated on the heap. |
| 111 | * |
| 112 | * There are two types of handles: local and persistent handles. |
| 113 | * Local handles are light-weight and transient and typically used in |
| 114 | * local operations. They are managed by HandleScopes. Persistent |
| 115 | * handles can be used when storing objects across several independent |
| 116 | * operations and have to be explicitly deallocated when they're no |
| 117 | * longer used. |
| 118 | * |
| 119 | * It is safe to extract the object stored in the handle by |
| 120 | * dereferencing the handle (for instance, to extract the Object* from |
| 121 | * an Handle<Object>); the value will still be governed by a handle |
| 122 | * behind the scenes and the same rules apply to these values as to |
| 123 | * their handles. |
| 124 | */ |
| 125 | template <class T> class Handle { |
| 126 | public: |
| 127 | |
| 128 | /** |
| 129 | * Creates an empty handle. |
| 130 | */ |
| 131 | Handle(); |
| 132 | |
| 133 | /** |
| 134 | * Creates a new handle for the specified value. |
| 135 | */ |
| 136 | explicit Handle(T* val) : val_(val) { } |
| 137 | |
| 138 | /** |
| 139 | * Creates a handle for the contents of the specified handle. This |
| 140 | * constructor allows you to pass handles as arguments by value and |
| 141 | * assign between handles. However, if you try to assign between |
| 142 | * incompatible handles, for instance from a Handle<String> to a |
| 143 | * Handle<Number> it will cause a compiletime error. Assigning |
| 144 | * between compatible handles, for instance assigning a |
| 145 | * Handle<String> to a variable declared as Handle<Value>, is legal |
| 146 | * because String is a subclass of Value. |
| 147 | */ |
| 148 | template <class S> inline Handle(Handle<S> that) |
| 149 | : val_(reinterpret_cast<T*>(*that)) { |
| 150 | /** |
| 151 | * This check fails when trying to convert between incompatible |
| 152 | * handles. For example, converting from a Handle<String> to a |
| 153 | * Handle<Number>. |
| 154 | */ |
| 155 | TYPE_CHECK(T, S); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns true if the handle is empty. |
| 160 | */ |
| 161 | bool IsEmpty() { return val_ == 0; } |
| 162 | |
| 163 | T* operator->(); |
| 164 | |
| 165 | T* operator*(); |
| 166 | |
| 167 | /** |
| 168 | * Sets the handle to be empty. IsEmpty() will then return true. |
| 169 | */ |
| 170 | void Clear() { this->val_ = 0; } |
| 171 | |
| 172 | /** |
| 173 | * Checks whether two handles are the same. |
| 174 | * Returns true if both are empty, or if the objects |
| 175 | * to which they refer are identical. |
| 176 | * The handles' references are not checked. |
| 177 | */ |
| 178 | template <class S> bool operator==(Handle<S> that) { |
| 179 | void** a = reinterpret_cast<void**>(**this); |
| 180 | void** b = reinterpret_cast<void**>(*that); |
| 181 | if (a == 0) return b == 0; |
| 182 | if (b == 0) return false; |
| 183 | return *a == *b; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Checks whether two handles are different. |
| 188 | * Returns true if only one of the handles is empty, or if |
| 189 | * the objects to which they refer are different. |
| 190 | * The handles' references are not checked. |
| 191 | */ |
| 192 | template <class S> bool operator!=(Handle<S> that) { |
| 193 | return !operator==(that); |
| 194 | } |
| 195 | |
| 196 | template <class S> static inline Handle<T> Cast(Handle<S> that) { |
| 197 | if (that.IsEmpty()) return Handle<T>(); |
| 198 | return Handle<T>(T::Cast(*that)); |
| 199 | } |
| 200 | |
| 201 | private: |
| 202 | T* val_; |
| 203 | }; |
| 204 | |
| 205 | |
| 206 | /** |
| 207 | * A light-weight stack-allocated object handle. All operations |
| 208 | * that return objects from within v8 return them in local handles. They |
| 209 | * are created within HandleScopes, and all local handles allocated within a |
| 210 | * handle scope are destroyed when the handle scope is destroyed. Hence it |
| 211 | * is not necessary to explicitly deallocate local handles. |
| 212 | */ |
| 213 | template <class T> class Local : public Handle<T> { |
| 214 | public: |
| 215 | Local(); |
| 216 | template <class S> inline Local(Local<S> that) |
| 217 | : Handle<T>(reinterpret_cast<T*>(*that)) { |
| 218 | /** |
| 219 | * This check fails when trying to convert between incompatible |
| 220 | * handles. For example, converting from a Handle<String> to a |
| 221 | * Handle<Number>. |
| 222 | */ |
| 223 | TYPE_CHECK(T, S); |
| 224 | } |
| 225 | template <class S> inline Local(S* that) : Handle<T>(that) { } |
| 226 | template <class S> static inline Local<T> Cast(Local<S> that) { |
| 227 | if (that.IsEmpty()) return Local<T>(); |
| 228 | return Local<T>(T::Cast(*that)); |
| 229 | } |
| 230 | |
| 231 | /** Create a local handle for the content of another handle. |
| 232 | * The referee is kept alive by the local handle even when |
| 233 | * the original handle is destroyed/disposed. |
| 234 | */ |
| 235 | static Local<T> New(Handle<T> that); |
| 236 | }; |
| 237 | |
| 238 | |
| 239 | /** |
| 240 | * An object reference that is independent of any handle scope. Where |
| 241 | * a Local handle only lives as long as the HandleScope where it was |
| 242 | * allocated, a Persistent handle remains valid until it is explicitly |
| 243 | * disposed. |
| 244 | * |
| 245 | * A persistent handle contains a reference to a storage cell within |
| 246 | * the v8 engine which holds an object value and which is updated by |
| 247 | * the garbage collector whenever the object is moved. A new storage |
| 248 | * cell can be created using Persistent::New and existing handles can |
| 249 | * be disposed using Persistent::Dispose. Since persistent handles |
| 250 | * are passed by value you may have many persistent handle objects |
| 251 | * that point to the same storage cell. For instance, if you pass a |
| 252 | * persistent handle as an argument to a function you will not get two |
| 253 | * different storage cells but rather two references to the same |
| 254 | * storage cell. |
| 255 | */ |
| 256 | template <class T> class Persistent : public Handle<T> { |
| 257 | public: |
| 258 | |
| 259 | /** |
| 260 | * Creates an empty persistent handle that doesn't point to any |
| 261 | * storage cell. |
| 262 | */ |
| 263 | Persistent(); |
| 264 | |
| 265 | /** |
| 266 | * Creates a persistent handle for the same storage cell as the |
| 267 | * specified handle. This constructor allows you to pass persistent |
| 268 | * handles as arguments by value and to assign between persistent |
| 269 | * handles. However, if you try to assign between incompatible |
| 270 | * persistent handles, for instance from a Persistent<String> to a |
| 271 | * Persistent<Number> it will cause a compiletime error. Assigning |
| 272 | * between compatible persistent handles, for instance assigning a |
| 273 | * Persistent<String> to a variable declared as Persistent<Value>, |
| 274 | * is legal because String is a subclass of Value. |
| 275 | */ |
| 276 | template <class S> inline Persistent(Persistent<S> that) |
| 277 | : Handle<T>(reinterpret_cast<T*>(*that)) { |
| 278 | /** |
| 279 | * This check fails when trying to convert between incompatible |
| 280 | * handles. For example, converting from a Handle<String> to a |
| 281 | * Handle<Number>. |
| 282 | */ |
| 283 | TYPE_CHECK(T, S); |
| 284 | } |
| 285 | |
| 286 | template <class S> inline Persistent(S* that) : Handle<T>(that) { } |
| 287 | |
| 288 | template <class S> explicit inline Persistent(Handle<S> that) |
| 289 | : Handle<T>(*that) { } |
| 290 | |
| 291 | template <class S> static inline Persistent<T> Cast(Persistent<S> that) { |
| 292 | if (that.IsEmpty()) return Persistent<T>(); |
| 293 | return Persistent<T>(T::Cast(*that)); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Creates a new persistent handle for an existing (local or |
| 298 | * persistent) handle. |
| 299 | */ |
| 300 | static Persistent<T> New(Handle<T> that); |
| 301 | |
| 302 | /** |
| 303 | * Releases the storage cell referenced by this persistent handle. |
| 304 | * Does not remove the reference to the cell from any handles. |
| 305 | * This handle's reference, and any any other references to the storage |
| 306 | * cell remain and IsEmpty will still return false. |
| 307 | */ |
| 308 | void Dispose(); |
| 309 | |
| 310 | /** |
| 311 | * Make the reference to this object weak. When only weak handles |
| 312 | * refer to the object, the garbage collector will perform a |
| 313 | * callback to the given V8::WeakReferenceCallback function, passing |
| 314 | * it the object reference and the given parameters. |
| 315 | */ |
| 316 | void MakeWeak(void* parameters, WeakReferenceCallback callback); |
| 317 | |
| 318 | /** Clears the weak reference to this object.*/ |
| 319 | void ClearWeak(); |
| 320 | |
| 321 | /** |
| 322 | *Checks if the handle holds the only reference to an object. |
| 323 | */ |
| 324 | bool IsNearDeath(); |
| 325 | |
| 326 | /** |
| 327 | * Returns true if the handle's reference is weak. |
| 328 | */ |
| 329 | bool IsWeak(); |
| 330 | |
| 331 | private: |
| 332 | friend class ImplementationUtilities; |
| 333 | friend class ObjectTemplate; |
| 334 | }; |
| 335 | |
| 336 | |
| 337 | /** |
| 338 | * A stack-allocated class that governs a number of local handles. |
| 339 | * After a handle scope has been created, all local handles will be |
| 340 | * allocated within that handle scope until either the handle scope is |
| 341 | * deleted or another handle scope is created. If there is already a |
| 342 | * handle scope and a new one is created, all allocations will take |
| 343 | * place in the new handle scope until that is deleted. After that, |
| 344 | * new handles will again be allocated in the original handle scope. |
| 345 | * |
| 346 | * After the handle scope of a local handle has been deleted the |
| 347 | * garbage collector will no longer track the object stored in the |
| 348 | * handle and may deallocate it. The behavior of accessing a handle |
| 349 | * for which the handle scope has been deleted is undefined. |
| 350 | */ |
| 351 | class HandleScope { |
| 352 | public: |
| 353 | HandleScope() : previous_(current_), is_closed_(false) { |
| 354 | current_.extensions = 0; |
| 355 | } |
| 356 | |
| 357 | ~HandleScope() { |
| 358 | // TODO(1245391): In a perfect world, there would be a way of not |
| 359 | // having to check for expl icitly closed scopes maybe through |
| 360 | // subclassing HandleScope? |
| 361 | if (!is_closed_) RestorePreviousState(); |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * TODO(1245391): Consider introducing a subclass for this. |
| 366 | * Closes the handle scope and returns the value as a handle in the |
| 367 | * previous scope, which is the new current scope after the call. |
| 368 | */ |
| 369 | template <class T> Local<T> Close(Handle<T> value); |
| 370 | |
| 371 | /** |
| 372 | * Counts the number of allocated handles. |
| 373 | */ |
| 374 | static int NumberOfHandles(); |
| 375 | |
| 376 | /** |
| 377 | * Creates a new handle with the given value. |
| 378 | */ |
| 379 | static void** CreateHandle(void* value); |
| 380 | |
| 381 | private: |
| 382 | // Make it impossible to create heap-allocated or illegal handle |
| 383 | // scopes by disallowing certain operations. |
| 384 | HandleScope(const HandleScope&); |
| 385 | void operator=(const HandleScope&); |
| 386 | void* operator new(size_t size); |
| 387 | void operator delete(void*, size_t); |
| 388 | |
| 389 | class Data { |
| 390 | public: |
| 391 | int extensions; |
| 392 | void** next; |
| 393 | void** limit; |
| 394 | inline void Initialize() { |
| 395 | extensions = -1; |
| 396 | next = limit = NULL; |
| 397 | } |
| 398 | }; |
| 399 | |
| 400 | static Data current_; |
| 401 | const Data previous_; |
| 402 | |
| 403 | /** |
| 404 | * Re-establishes the previous scope state. Should not be called for |
| 405 | * any other scope than the current scope and not more than once. |
| 406 | */ |
| 407 | void RestorePreviousState() { |
| 408 | if (current_.extensions > 0) DeleteExtensions(); |
| 409 | current_ = previous_; |
| 410 | #ifdef DEBUG |
| 411 | ZapRange(current_.next, current_.limit); |
| 412 | #endif |
| 413 | } |
| 414 | |
| 415 | // TODO(1245391): Consider creating a subclass for this. |
| 416 | bool is_closed_; |
| 417 | void** RawClose(void** value); |
| 418 | |
| 419 | /** Deallocates any extensions used by the current scope.*/ |
| 420 | static void DeleteExtensions(); |
| 421 | |
| 422 | #ifdef DEBUG |
| 423 | // Zaps the handles in the half-open interval [start, end). |
| 424 | static void ZapRange(void** start, void** end); |
| 425 | #endif |
| 426 | |
| 427 | friend class ImplementationUtilities; |
| 428 | }; |
| 429 | |
| 430 | |
| 431 | // --- S p e c i a l o b j e c t s --- |
| 432 | |
| 433 | |
| 434 | /** |
| 435 | * The superclass of values and API object templates. |
| 436 | */ |
| 437 | class Data { |
| 438 | private: |
| 439 | Data(); |
| 440 | }; |
| 441 | |
| 442 | |
| 443 | /** |
| 444 | * Pre-compilation data that can be associated with a script. This |
| 445 | * data can be calculated for a script in advance of actually |
| 446 | * compiling it, and stored between compilations. When script data |
| 447 | * is given to the compile method compilation will be faster. |
| 448 | */ |
| 449 | class ScriptData { |
| 450 | public: |
| 451 | virtual ~ScriptData() { } |
| 452 | static ScriptData* PreCompile(const char* input, int length); |
| 453 | static ScriptData* New(unsigned* data, int length); |
| 454 | |
| 455 | virtual int Length() = 0; |
| 456 | virtual unsigned* Data() = 0; |
| 457 | }; |
| 458 | |
| 459 | |
| 460 | /** |
| 461 | * The origin, within a file, of a script. |
| 462 | */ |
| 463 | class ScriptOrigin { |
| 464 | public: |
| 465 | ScriptOrigin(Handle<String> resource_name, |
| 466 | Handle<Integer> resource_line_offset = Handle<Integer>(), |
| 467 | Handle<Integer> resource_column_offset = Handle<Integer>()) |
| 468 | : resource_name_(resource_name), |
| 469 | resource_line_offset_(resource_line_offset), |
| 470 | resource_column_offset_(resource_column_offset) { } |
| 471 | inline Handle<String> ResourceName(); |
| 472 | inline Handle<Integer> ResourceLineOffset(); |
| 473 | inline Handle<Integer> ResourceColumnOffset(); |
| 474 | private: |
| 475 | Handle<String> resource_name_; |
| 476 | Handle<Integer> resource_line_offset_; |
| 477 | Handle<Integer> resource_column_offset_; |
| 478 | }; |
| 479 | |
| 480 | |
| 481 | /** |
| 482 | * A compiled javascript script. |
| 483 | */ |
| 484 | class Script { |
| 485 | public: |
| 486 | |
| 487 | /** |
| 488 | * Compiles the specified script. The ScriptOrigin* and ScriptData* |
| 489 | * parameters are owned by the caller of Script::Compile. No |
| 490 | * references to these objects are kept after compilation finishes. |
| 491 | */ |
| 492 | static Local<Script> Compile(Handle<String> source, |
| 493 | ScriptOrigin* origin = NULL, |
| 494 | ScriptData* pre_data = NULL); |
| 495 | |
| 496 | Local<Value> Run(); |
| 497 | }; |
| 498 | |
| 499 | |
| 500 | /** |
| 501 | * An error message. |
| 502 | */ |
| 503 | class Message { |
| 504 | public: |
| 505 | Local<String> Get(); |
| 506 | Local<Value> GetSourceLine(); |
| 507 | |
| 508 | // TODO(1241256): Rewrite (or remove) this method. We don't want to |
| 509 | // deal with ownership of the returned string and we want to use |
| 510 | // javascript data structures exclusively. |
| 511 | char* GetUnderline(char* source_line, char underline_char); |
| 512 | |
| 513 | Handle<String> GetScriptResourceName(); |
| 514 | |
| 515 | // TODO(1240903): Remove this when no longer used in WebKit V8 |
| 516 | // bindings. |
| 517 | Handle<Value> GetSourceData(); |
| 518 | |
| 519 | int GetLineNumber(); |
| 520 | |
| 521 | // TODO(1245381): Print to a string instead of on a FILE. |
| 522 | static void PrintCurrentStackTrace(FILE* out); |
| 523 | }; |
| 524 | |
| 525 | |
| 526 | // --- V a l u e --- |
| 527 | |
| 528 | |
| 529 | /** |
| 530 | * The superclass of all javascript values and objects. |
| 531 | */ |
| 532 | class Value : public Data { |
| 533 | public: |
| 534 | |
| 535 | /** |
| 536 | * Returns true if this value is the undefined value. See ECMA-262 |
| 537 | * 4.3.10. |
| 538 | */ |
| 539 | bool IsUndefined(); |
| 540 | |
| 541 | /** |
| 542 | * Returns true if this value is the null value. See ECMA-262 |
| 543 | * 4.3.11. |
| 544 | */ |
| 545 | bool IsNull(); |
| 546 | |
| 547 | /** |
| 548 | * Returns true if this value is true. |
| 549 | */ |
| 550 | bool IsTrue(); |
| 551 | |
| 552 | /** |
| 553 | * Returns true if this value is false. |
| 554 | */ |
| 555 | bool IsFalse(); |
| 556 | |
| 557 | /** |
| 558 | * Returns true if this value is an instance of the String type. |
| 559 | * See ECMA-262 8.4. |
| 560 | */ |
| 561 | bool IsString(); |
| 562 | |
| 563 | /** |
| 564 | * Returns true if this value is a function. |
| 565 | */ |
| 566 | bool IsFunction(); |
| 567 | |
| 568 | /** |
| 569 | * Returns true if this value is an array. |
| 570 | */ |
| 571 | bool IsArray(); |
| 572 | |
| 573 | /** |
| 574 | * Returns true if this value is an object. |
| 575 | */ |
| 576 | bool IsObject(); |
| 577 | |
| 578 | /** |
| 579 | * Returns true if this value is boolean. |
| 580 | */ |
| 581 | bool IsBoolean(); |
| 582 | |
| 583 | /** |
| 584 | * Returns true if this value is a number. |
| 585 | */ |
| 586 | bool IsNumber(); |
| 587 | |
| 588 | /** |
| 589 | * Returns true if this value is external. |
| 590 | */ |
| 591 | bool IsExternal(); |
| 592 | |
| 593 | /** |
| 594 | * Returns true if this value is a 32-bit signed integer. |
| 595 | */ |
| 596 | bool IsInt32(); |
| 597 | |
| 598 | Local<Boolean> ToBoolean(); |
| 599 | Local<Number> ToNumber(); |
| 600 | Local<String> ToString(); |
| 601 | Local<String> ToDetailString(); |
| 602 | Local<Object> ToObject(); |
| 603 | Local<Integer> ToInteger(); |
| 604 | Local<Uint32> ToUint32(); |
| 605 | Local<Int32> ToInt32(); |
| 606 | |
| 607 | /** |
| 608 | * Attempts to convert a string to an array index. |
| 609 | * Returns an empty handle if the conversion fails. |
| 610 | */ |
| 611 | Local<Uint32> ToArrayIndex(); |
| 612 | |
| 613 | bool BooleanValue(); |
| 614 | double NumberValue(); |
| 615 | int64_t IntegerValue(); |
| 616 | uint32_t Uint32Value(); |
| 617 | int32_t Int32Value(); |
| 618 | |
| 619 | /** JS == */ |
| 620 | bool Equals(Handle<Value> that); |
| 621 | bool StrictEquals(Handle<Value> that); |
| 622 | }; |
| 623 | |
| 624 | |
| 625 | /** |
| 626 | * The superclass of primitive values. See ECMA-262 4.3.2. |
| 627 | */ |
| 628 | class Primitive : public Value { }; |
| 629 | |
| 630 | |
| 631 | /** |
| 632 | * A primitive boolean value (ECMA-262, 4.3.14). Either the true |
| 633 | * or false value. |
| 634 | */ |
| 635 | class Boolean : public Primitive { |
| 636 | public: |
| 637 | bool Value(); |
| 638 | static inline Handle<Boolean> New(bool value); |
| 639 | }; |
| 640 | |
| 641 | |
| 642 | /** |
| 643 | * A javascript string value (ECMA-262, 4.3.17). |
| 644 | */ |
| 645 | class String : public Primitive { |
| 646 | public: |
| 647 | int Length(); |
| 648 | |
| 649 | /** |
| 650 | * Write the contents of the string to an external buffer. |
| 651 | * If no arguments are given, expects that buffer is large |
| 652 | * enough to hold the entire string and NULL terminator. Copies |
| 653 | * the contents of the string and the NULL terminator into |
| 654 | * buffer. |
| 655 | * |
| 656 | * Copies up to length characters into the output buffer. |
| 657 | * Only null-terminates if there is enough space in the buffer. |
| 658 | * |
| 659 | * \param buffer The buffer into which the string will be copied. |
| 660 | * \param start The starting position within the string at which |
| 661 | * copying begins. |
| 662 | * \param length The number of bytes to copy from the string. |
| 663 | * \return The number of characters copied to the buffer |
| 664 | * excluding the NULL terminator. |
| 665 | */ |
| 666 | int Write(uint16_t* buffer, int start = 0, int length = -1); // UTF-16 |
| 667 | int WriteAscii(char* buffer, |
| 668 | int start = 0, |
| 669 | int length = -1); // literally ascii |
| 670 | |
| 671 | /** |
| 672 | * Returns true if the string is external |
| 673 | */ |
| 674 | bool IsExternal(); |
| 675 | |
| 676 | /** |
| 677 | * Returns true if the string is both external and ascii |
| 678 | */ |
| 679 | bool IsExternalAscii(); |
| 680 | /** |
| 681 | * An ExternalStringResource is a wrapper around a two-byte string |
| 682 | * buffer that resides outside the V8's heap. Implement an |
| 683 | * ExternalStringResource to manage the life cycle of the underlying |
| 684 | * buffer. |
| 685 | */ |
| 686 | class ExternalStringResource { |
| 687 | public: |
| 688 | /** |
| 689 | * Override the destructor to manage the life cycle of the underlying |
| 690 | * buffer. |
| 691 | */ |
| 692 | virtual ~ExternalStringResource() {} |
| 693 | /** The string data from the underlying buffer.*/ |
| 694 | virtual const uint16_t* data() const = 0; |
| 695 | /** The length of the string. That is, the number of two-byte characters.*/ |
| 696 | virtual size_t length() const = 0; |
| 697 | protected: |
| 698 | ExternalStringResource() {} |
| 699 | private: |
| 700 | ExternalStringResource(const ExternalStringResource&); |
| 701 | void operator=(const ExternalStringResource&); |
| 702 | }; |
| 703 | |
| 704 | /** |
| 705 | * An ExternalAsciiStringResource is a wrapper around an ascii |
| 706 | * string buffer that resides outside V8's heap. Implement an |
| 707 | * ExternalAsciiStringResource to manage the life cycle of the |
| 708 | * underlying buffer. |
| 709 | */ |
| 710 | |
| 711 | class ExternalAsciiStringResource { |
| 712 | public: |
| 713 | /** |
| 714 | * Override the destructor to manage the life cycle of the underlying |
| 715 | * buffer. |
| 716 | */ |
| 717 | virtual ~ExternalAsciiStringResource() {} |
| 718 | /** The string data from the underlying buffer.*/ |
| 719 | virtual const char* data() const = 0; |
| 720 | /** The number of ascii characters in the string.*/ |
| 721 | virtual size_t length() const = 0; |
| 722 | protected: |
| 723 | ExternalAsciiStringResource() {} |
| 724 | private: |
| 725 | ExternalAsciiStringResource(const ExternalAsciiStringResource&); |
| 726 | void operator=(const ExternalAsciiStringResource&); |
| 727 | }; |
| 728 | |
| 729 | /** |
| 730 | * Get the ExternalStringResource for an external string. Only |
| 731 | * valid if IsExternal() returns true. |
| 732 | */ |
| 733 | ExternalStringResource* GetExternalStringResource(); |
| 734 | |
| 735 | /** |
| 736 | * Get the ExternalAsciiStringResource for an external ascii string. |
| 737 | * Only valid if IsExternalAscii() returns true. |
| 738 | */ |
| 739 | ExternalAsciiStringResource* GetExternalAsciiStringResource(); |
| 740 | |
| 741 | static String* Cast(v8::Value* obj); |
| 742 | |
| 743 | /** |
| 744 | * Allocates a new string from either utf-8 encoded or ascii data. |
| 745 | * The second parameter 'length' gives the buffer length. |
| 746 | * If the data is utf-8 encoded, the caller must |
| 747 | * be careful to supply the length parameter. |
| 748 | * If it is not given, the function calls |
| 749 | * 'strlen' to determine the buffer length, it might be |
| 750 | * wrong if '\0' character is in the 'data'. |
| 751 | */ |
| 752 | static Local<String> New(const char* data, int length = -1); |
| 753 | |
| 754 | /** Allocates a new string from utf16 data.*/ |
| 755 | static Local<String> New(const uint16_t* data, int length = -1); |
| 756 | |
| 757 | /** Creates a symbol. Returns one if it exists already.*/ |
| 758 | static Local<String> NewSymbol(const char* data, int length = -1); |
| 759 | |
| 760 | /** |
| 761 | * Creates a new external string using the data defined in the given |
| 762 | * resource. The resource is deleted when the external string is no |
| 763 | * longer live on V8's heap. The caller of this function should not |
| 764 | * delete or modify the resource. Neither should the underlying buffer be |
| 765 | * deallocated or modified except through the destructor of the |
| 766 | * external string resource. |
| 767 | */ |
| 768 | static Local<String> NewExternal(ExternalStringResource* resource); |
| 769 | |
| 770 | /** |
| 771 | * Creates a new external string using the ascii data defined in the given |
| 772 | * resource. The resource is deleted when the external string is no |
| 773 | * longer live on V8's heap. The caller of this function should not |
| 774 | * delete or modify the resource. Neither should the underlying buffer be |
| 775 | * deallocated or modified except through the destructor of the |
| 776 | * external string resource. |
| 777 | */ |
| 778 | static Local<String> NewExternal(ExternalAsciiStringResource* resource); |
| 779 | |
| 780 | /** Creates an undetectable string from the supplied character.*/ |
| 781 | static Local<String> NewUndetectable(const char* data, int length = -1); |
| 782 | |
| 783 | /** Creates an undetectable string from the supplied unsigned integer.*/ |
| 784 | static Local<String> NewUndetectable(const uint16_t* data, int length = -1); |
| 785 | |
| 786 | /** |
| 787 | * Converts an object to an ascii string. |
| 788 | * Useful if you want to print the object. |
| 789 | */ |
| 790 | class AsciiValue { |
| 791 | public: |
| 792 | explicit AsciiValue(Handle<v8::Value> obj); |
| 793 | ~AsciiValue(); |
| 794 | char* operator*() { return str_; } |
| 795 | private: |
| 796 | char* str_; |
| 797 | }; |
| 798 | |
| 799 | /** |
| 800 | * Converts an object to a two-byte string. |
| 801 | */ |
| 802 | class Value { |
| 803 | public: |
| 804 | explicit Value(Handle<v8::Value> obj); |
| 805 | ~Value(); |
| 806 | uint16_t* operator*() { return str_; } |
| 807 | private: |
| 808 | uint16_t* str_; |
| 809 | }; |
| 810 | }; |
| 811 | |
| 812 | |
| 813 | /** |
| 814 | * A javascript number value (ECMA-262, 4.3.20) |
| 815 | */ |
| 816 | class Number : public Primitive { |
| 817 | public: |
| 818 | double Value(); |
| 819 | static Local<Number> New(double value); |
| 820 | static Number* Cast(v8::Value* obj); |
| 821 | private: |
| 822 | Number(); |
| 823 | }; |
| 824 | |
| 825 | |
| 826 | /** |
| 827 | * A javascript value representing a signed integer. |
| 828 | */ |
| 829 | class Integer : public Number { |
| 830 | public: |
| 831 | static Local<Integer> New(int32_t value); |
| 832 | int64_t Value(); |
| 833 | static Integer* Cast(v8::Value* obj); |
| 834 | private: |
| 835 | Integer(); |
| 836 | }; |
| 837 | |
| 838 | |
| 839 | /** |
| 840 | * A javascript value representing a 32-bit signed integer. |
| 841 | */ |
| 842 | class Int32 : public Integer { |
| 843 | public: |
| 844 | int32_t Value(); |
| 845 | private: |
| 846 | Int32(); |
| 847 | }; |
| 848 | |
| 849 | |
| 850 | /** |
| 851 | * A javascript value representing a 32-bit unsigned integer. |
| 852 | */ |
| 853 | class Uint32 : public Integer { |
| 854 | public: |
| 855 | uint32_t Value(); |
| 856 | private: |
| 857 | Uint32(); |
| 858 | }; |
| 859 | |
| 860 | |
| 861 | /** |
| 862 | * An instance of the built-in Date constructor (ECMA-262, 15.9). |
| 863 | */ |
| 864 | class Date : public Value { |
| 865 | public: |
| 866 | static Local<Value> New(double time); |
| 867 | }; |
| 868 | |
| 869 | |
| 870 | enum PropertyAttribute { |
| 871 | None = 0, |
| 872 | ReadOnly = 1 << 0, |
| 873 | DontEnum = 1 << 1, |
| 874 | DontDelete = 1 << 2 |
| 875 | }; |
| 876 | |
| 877 | /** |
| 878 | * A javascript object (ECMA-262, 4.3.3) |
| 879 | */ |
| 880 | class Object : public Value { |
| 881 | public: |
| 882 | bool Set(Handle<Value> key, |
| 883 | Handle<Value> value, |
| 884 | PropertyAttribute attribs = None); |
| 885 | Local<Value> Get(Handle<Value> key); |
| 886 | |
| 887 | // TODO(1245389): Replace the type-specific versions of these |
| 888 | // functions with generic ones that accept a Handle<Value> key. |
| 889 | bool Has(Handle<String> key); |
| 890 | bool Delete(Handle<String> key); |
| 891 | bool Has(uint32_t index); |
| 892 | bool Delete(uint32_t index); |
| 893 | |
| 894 | /** |
| 895 | * Get the prototype object. This does not skip objects marked to |
| 896 | * be skipped by __proto__ and it does not consult the security |
| 897 | * handler. |
| 898 | */ |
| 899 | Local<Value> GetPrototype(); |
| 900 | |
| 901 | /** |
| 902 | * Call builtin Object.prototype.toString on this object. |
| 903 | * This is different from Value::ToString() that may call |
| 904 | * user-defined toString function. This one does not. |
| 905 | */ |
| 906 | Local<String> ObjectProtoToString(); |
| 907 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame^] | 908 | /** Gets the number of internal fields for this Object. */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 909 | int InternalFieldCount(); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame^] | 910 | /** Gets the value in an internal field. */ |
| 911 | Local<Value> GetInternalField(int index); |
| 912 | /** Sets the value in an internal field. */ |
| 913 | void SetInternalField(int index, Handle<Value> value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 914 | |
| 915 | // Testers for local properties. |
| 916 | bool HasRealNamedProperty(Handle<String> key); |
| 917 | bool HasRealIndexedProperty(uint32_t index); |
| 918 | bool HasRealNamedCallbackProperty(Handle<String> key); |
| 919 | |
| 920 | /** |
| 921 | * If result.IsEmpty() no real property was located in the prototype chain. |
| 922 | * This means interceptors in the prototype chain are not called. |
| 923 | */ |
| 924 | Handle<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key); |
| 925 | |
| 926 | /** Tests for a named lookup interceptor.*/ |
| 927 | bool HasNamedLookupInterceptor(); |
| 928 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame^] | 929 | /** Tests for an index lookup interceptor.*/ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 930 | bool HasIndexedLookupInterceptor(); |
| 931 | |
| 932 | |
| 933 | static Local<Object> New(); |
| 934 | static Object* Cast(Value* obj); |
| 935 | private: |
| 936 | Object(); |
| 937 | }; |
| 938 | |
| 939 | |
| 940 | /** |
| 941 | * An instance of the built-in array constructor (ECMA-262, 15.4.2). |
| 942 | */ |
| 943 | class Array : public Object { |
| 944 | public: |
| 945 | uint32_t Length(); |
| 946 | |
| 947 | static Local<Array> New(int length = 0); |
| 948 | static Array* Cast(Value* obj); |
| 949 | private: |
| 950 | Array(); |
| 951 | }; |
| 952 | |
| 953 | |
| 954 | /** |
| 955 | * A javascript function object (ECMA-262, 15.3). |
| 956 | */ |
| 957 | class Function : public Object { |
| 958 | public: |
| 959 | Local<Object> NewInstance(); |
| 960 | Local<Object> NewInstance(int argc, Handle<Value> argv[]); |
| 961 | Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]); |
| 962 | void SetName(Handle<String> name); |
| 963 | Handle<Value> GetName(); |
| 964 | static Function* Cast(Value* obj); |
| 965 | private: |
| 966 | Function(); |
| 967 | }; |
| 968 | |
| 969 | |
| 970 | /** |
| 971 | * A javascript value that wraps a c++ void*. This type of value is |
| 972 | * mainly used to associate c++ data structures with javascript |
| 973 | * objects. |
| 974 | */ |
| 975 | class External : public Value { |
| 976 | public: |
| 977 | static Local<External> New(void* value); |
| 978 | static External* Cast(Value* obj); |
| 979 | void* Value(); |
| 980 | private: |
| 981 | External(); |
| 982 | }; |
| 983 | |
| 984 | |
| 985 | // --- T e m p l a t e s --- |
| 986 | |
| 987 | |
| 988 | /** |
| 989 | * The superclass of object and function templates. |
| 990 | */ |
| 991 | class Template : public Data { |
| 992 | public: |
| 993 | /** Adds a property to each instance created by this template.*/ |
| 994 | void Set(Handle<String> name, Handle<Data> value, |
| 995 | PropertyAttribute attributes = None); |
| 996 | inline void Set(const char* name, Handle<Data> value); |
| 997 | private: |
| 998 | Template(); |
| 999 | |
| 1000 | friend class ObjectTemplate; |
| 1001 | friend class FunctionTemplate; |
| 1002 | }; |
| 1003 | |
| 1004 | |
| 1005 | /** |
| 1006 | * The argument information given to function call callbacks. This |
| 1007 | * class provides access to information about context of the call, |
| 1008 | * including the receiver, the number and values of arguments, and |
| 1009 | * the holder of the function. |
| 1010 | */ |
| 1011 | class Arguments { |
| 1012 | public: |
| 1013 | inline int Length() const; |
| 1014 | inline Local<Value> operator[](int i) const; |
| 1015 | inline Local<Function> Callee() const; |
| 1016 | inline Local<Object> This() const; |
| 1017 | inline Local<Object> Holder() const; |
| 1018 | inline bool IsConstructCall() const; |
| 1019 | inline Local<Value> Data() const; |
| 1020 | private: |
| 1021 | Arguments(); |
| 1022 | friend class ImplementationUtilities; |
| 1023 | inline Arguments(Local<Value> data, |
| 1024 | Local<Object> holder, |
| 1025 | Local<Function> callee, |
| 1026 | bool is_construct_call, |
| 1027 | void** values, int length); |
| 1028 | Local<Value> data_; |
| 1029 | Local<Object> holder_; |
| 1030 | Local<Function> callee_; |
| 1031 | bool is_construct_call_; |
| 1032 | void** values_; |
| 1033 | int length_; |
| 1034 | }; |
| 1035 | |
| 1036 | |
| 1037 | /** |
| 1038 | * The information passed to an accessor callback about the context |
| 1039 | * of the property access. |
| 1040 | */ |
| 1041 | class AccessorInfo { |
| 1042 | public: |
| 1043 | inline AccessorInfo(Local<Object> self, |
| 1044 | Local<Value> data, |
| 1045 | Local<Object> holder) |
| 1046 | : self_(self), data_(data), holder_(holder) { } |
| 1047 | inline Local<Value> Data() const; |
| 1048 | inline Local<Object> This() const; |
| 1049 | inline Local<Object> Holder() const; |
| 1050 | private: |
| 1051 | Local<Object> self_; |
| 1052 | Local<Value> data_; |
| 1053 | Local<Object> holder_; |
| 1054 | }; |
| 1055 | |
| 1056 | |
| 1057 | typedef Handle<Value> (*InvocationCallback)(const Arguments& args); |
| 1058 | |
| 1059 | typedef int (*LookupCallback)(Local<Object> self, Local<String> name); |
| 1060 | |
| 1061 | /** |
| 1062 | * Accessor[Getter|Setter] are used as callback functions when |
| 1063 | * setting|getting a particular property. See objectTemplate::SetAccessor. |
| 1064 | */ |
| 1065 | typedef Handle<Value> (*AccessorGetter)(Local<String> property, |
| 1066 | const AccessorInfo& info); |
| 1067 | |
| 1068 | |
| 1069 | typedef void (*AccessorSetter)(Local<String> property, |
| 1070 | Local<Value> value, |
| 1071 | const AccessorInfo& info); |
| 1072 | |
| 1073 | |
| 1074 | /** |
| 1075 | * NamedProperty[Getter|Setter] are used as interceptors on object. |
| 1076 | * See ObjectTemplate::SetNamedPropertyHandler. |
| 1077 | */ |
| 1078 | typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property, |
| 1079 | const AccessorInfo& info); |
| 1080 | |
| 1081 | |
| 1082 | /** |
| 1083 | * Returns the value if the setter intercepts the request. |
| 1084 | * Otherwise, returns an empty handle. |
| 1085 | */ |
| 1086 | typedef Handle<Value> (*NamedPropertySetter)(Local<String> property, |
| 1087 | Local<Value> value, |
| 1088 | const AccessorInfo& info); |
| 1089 | |
| 1090 | |
| 1091 | /** |
| 1092 | * Returns a non-empty handle if the interceptor intercepts the request. |
| 1093 | * The result is true to indicate the property is found. |
| 1094 | */ |
| 1095 | typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property, |
| 1096 | const AccessorInfo& info); |
| 1097 | |
| 1098 | |
| 1099 | /** |
| 1100 | * Returns a non-empty handle if the deleter intercepts the request. |
| 1101 | * Otherwise, the return value is the value of deleted expression. |
| 1102 | */ |
| 1103 | typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property, |
| 1104 | const AccessorInfo& info); |
| 1105 | |
| 1106 | /** |
| 1107 | * TODO(758124): Add documentation? |
| 1108 | */ |
| 1109 | typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info); |
| 1110 | |
| 1111 | /** |
| 1112 | * TODO(758124): Add documentation? |
| 1113 | */ |
| 1114 | typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index, |
| 1115 | const AccessorInfo& info); |
| 1116 | |
| 1117 | |
| 1118 | /** |
| 1119 | * Returns the value if the setter intercepts the request. |
| 1120 | * Otherwise, returns an empty handle. |
| 1121 | */ |
| 1122 | typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index, |
| 1123 | Local<Value> value, |
| 1124 | const AccessorInfo& info); |
| 1125 | |
| 1126 | |
| 1127 | /** |
| 1128 | * Returns a non-empty handle if the interceptor intercepts the request. |
| 1129 | * The result is true to indicate the property is found. |
| 1130 | */ |
| 1131 | typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index, |
| 1132 | const AccessorInfo& info); |
| 1133 | |
| 1134 | /** |
| 1135 | * Returns a non-empty handle if the deleter intercepts the request. |
| 1136 | * Otherwise, the return value is the value of deleted expression. |
| 1137 | */ |
| 1138 | typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index, |
| 1139 | const AccessorInfo& info); |
| 1140 | |
| 1141 | |
| 1142 | typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info); |
| 1143 | |
| 1144 | |
| 1145 | /** |
| 1146 | * TODO(758124): Clarify documentation? Determines whether host |
| 1147 | * objects can read or write an accessor? (What does the default |
| 1148 | * allow? Both or neither?) If a host object needs access check and |
| 1149 | * the check failed, some properties (accessors created by API) are |
| 1150 | * still accessible. Such properties have AccessControl to allow read |
| 1151 | * or write. |
| 1152 | */ |
| 1153 | enum AccessControl { |
| 1154 | DEFAULT = 0, |
| 1155 | ALL_CAN_READ = 1, |
| 1156 | ALL_CAN_WRITE = 2 |
| 1157 | }; |
| 1158 | |
| 1159 | |
| 1160 | /** |
| 1161 | * Undocumented security features. |
| 1162 | */ |
| 1163 | enum AccessType { |
| 1164 | ACCESS_GET, |
| 1165 | ACCESS_SET, |
| 1166 | ACCESS_HAS, |
| 1167 | ACCESS_DELETE, |
| 1168 | ACCESS_KEYS |
| 1169 | }; |
| 1170 | |
| 1171 | typedef bool (*NamedSecurityCallback)(Local<Object> global, |
| 1172 | Local<Value> key, |
| 1173 | AccessType type, |
| 1174 | Local<Value> data); |
| 1175 | |
| 1176 | typedef bool (*IndexedSecurityCallback)(Local<Object> global, |
| 1177 | uint32_t index, |
| 1178 | AccessType type, |
| 1179 | Local<Value> data); |
| 1180 | |
| 1181 | |
| 1182 | /** |
| 1183 | * TODO(758124): Make sure this documentation is up to date. |
| 1184 | * |
| 1185 | * A FunctionTemplate is used to create functions at runtime. There can only be |
| 1186 | * ONE function created in an environment. |
| 1187 | * |
| 1188 | * A FunctionTemplate can have properties, these properties are added to the |
| 1189 | * function object which it is created. |
| 1190 | * |
| 1191 | * A FunctionTemplate has a corresponding instance template which is used to |
| 1192 | * create object instances when the function used as a constructor. Properties |
| 1193 | * added to the instance template are added to each object instance. |
| 1194 | * |
| 1195 | * A FunctionTemplate can have a prototype template. The prototype template |
| 1196 | * is used to create the prototype object of the function. |
| 1197 | * |
| 1198 | * Following example illustrates relationship between FunctionTemplate and |
| 1199 | * various pieces: |
| 1200 | * |
| 1201 | * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 1202 | * t->Set("func_property", v8::Number::New(1)); |
| 1203 | * |
| 1204 | * v8::Local<v8::Template> proto_t = t->PrototypeTemplate(); |
| 1205 | * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback)); |
| 1206 | * proto_t->Set("proto_const", v8::Number::New(2)); |
| 1207 | * |
| 1208 | * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate(); |
| 1209 | * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback); |
| 1210 | * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...); |
| 1211 | * instance_t->Set("instance_property", Number::New(3)); |
| 1212 | * |
| 1213 | * v8::Local<v8::Function> function = t->GetFunction(); |
| 1214 | * v8::Local<v8::Object> instance = function->NewInstance(); |
| 1215 | * |
| 1216 | * Let's use "function" as the JS variable name of the function object |
| 1217 | * and "instance" for the instance object created above, the following |
| 1218 | * JavaScript statements hold: |
| 1219 | * |
| 1220 | * func_property in function == true |
| 1221 | * function.func_property == 1 |
| 1222 | * |
| 1223 | * function.prototype.proto_method() invokes 'callback' |
| 1224 | * function.prototype.proto_const == 2 |
| 1225 | * |
| 1226 | * instance instanceof function == true |
| 1227 | * instance.instance_accessor calls InstanceAccessorCallback |
| 1228 | * instance.instance_property == 3 |
| 1229 | * |
| 1230 | * |
| 1231 | * Inheritance: |
| 1232 | * |
| 1233 | * A FunctionTemplate can inherit from another one by calling Inherit method. |
| 1234 | * Following graph illustrates the semantic of inheritance: |
| 1235 | * |
| 1236 | * FunctionTemplate Parent -> Parent() . prototype -> { } |
| 1237 | * ^ ^ |
| 1238 | * | Inherit(Parent) | .__proto__ |
| 1239 | * | | |
| 1240 | * FunctionTemplate Child -> Child() . prototype -> { } |
| 1241 | * |
| 1242 | * A FunctionTemplate 'Child' inherits from 'Parent', the prototype object |
| 1243 | * of Child() function has __proto__ pointing to Parent() function's prototype |
| 1244 | * object. An instance of Child function has all properties on parents' |
| 1245 | * instance templates. |
| 1246 | * |
| 1247 | * Let Parent be the FunctionTemplate initialized in previous section and |
| 1248 | * create a Child function template by: |
| 1249 | * |
| 1250 | * Local<FunctionTemplate> parent = t; |
| 1251 | * Local<FunctionTemplate> child = FunctionTemplate::New(); |
| 1252 | * child->Inherit(parent); |
| 1253 | * |
| 1254 | * Local<Function> child_function = child->GetFunction(); |
| 1255 | * Local<Object> child_instance = child_function->NewInstance(); |
| 1256 | * |
| 1257 | * The following JS code holds: |
| 1258 | * child_func.prototype.__proto__ == function.prototype; |
| 1259 | * child_instance.instance_accessor calls InstanceAccessorCallback |
| 1260 | * child_instance.instance_property == 3; |
| 1261 | */ |
| 1262 | class FunctionTemplate : public Template { |
| 1263 | public: |
| 1264 | /** Creates a function template.*/ |
| 1265 | static Local<FunctionTemplate> New(InvocationCallback callback = 0, |
| 1266 | Handle<Value> data = Handle<Value>(), |
| 1267 | Handle<Signature> signature = |
| 1268 | Handle<Signature>()); |
| 1269 | /** Returns the unique function instance in the current execution context.*/ |
| 1270 | Local<Function> GetFunction(); |
| 1271 | |
| 1272 | void SetCallHandler(InvocationCallback callback, |
| 1273 | Handle<Value> data = Handle<Value>()); |
| 1274 | void SetLookupHandler(LookupCallback handler); |
| 1275 | |
| 1276 | Local<ObjectTemplate> InstanceTemplate(); |
| 1277 | |
| 1278 | /** Causes the function template to inherit from a parent function template.*/ |
| 1279 | void Inherit(Handle<FunctionTemplate> parent); |
| 1280 | |
| 1281 | /** |
| 1282 | * A PrototypeTemplate is the template used to create the prototype object |
| 1283 | * of the function created by this template. |
| 1284 | */ |
| 1285 | Local<ObjectTemplate> PrototypeTemplate(); |
| 1286 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1287 | void SetClassName(Handle<String> name); |
| 1288 | |
| 1289 | /** |
| 1290 | * Determines whether the __proto__ accessor ignores instances of the function template. |
| 1291 | * Call with a value of true to make the __proto__ accessor ignore instances of the function template. |
| 1292 | * Call with a value of false to make the __proto__ accessor not ignore instances of the function template. |
| 1293 | * By default, instances of a function template are not ignored. |
| 1294 | * TODO(758124): What does "not ignored" mean? |
| 1295 | */ |
| 1296 | void SetHiddenPrototype(bool value); |
| 1297 | |
| 1298 | /** |
| 1299 | * Returns true if the given object is an instance of this function template. |
| 1300 | */ |
| 1301 | bool HasInstance(Handle<Value> object); |
| 1302 | |
| 1303 | private: |
| 1304 | FunctionTemplate(); |
| 1305 | void AddInstancePropertyAccessor(Handle<String> name, |
| 1306 | AccessorGetter getter, |
| 1307 | AccessorSetter setter, |
| 1308 | Handle<Value> data, |
| 1309 | AccessControl settings, |
| 1310 | PropertyAttribute attributes); |
| 1311 | void SetNamedInstancePropertyHandler(NamedPropertyGetter getter, |
| 1312 | NamedPropertySetter setter, |
| 1313 | NamedPropertyQuery query, |
| 1314 | NamedPropertyDeleter remover, |
| 1315 | NamedPropertyEnumerator enumerator, |
| 1316 | Handle<Value> data); |
| 1317 | void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter, |
| 1318 | IndexedPropertySetter setter, |
| 1319 | IndexedPropertyQuery query, |
| 1320 | IndexedPropertyDeleter remover, |
| 1321 | IndexedPropertyEnumerator enumerator, |
| 1322 | Handle<Value> data); |
| 1323 | void SetInstanceCallAsFunctionHandler(InvocationCallback callback, |
| 1324 | Handle<Value> data); |
| 1325 | |
| 1326 | friend class Context; |
| 1327 | friend class ObjectTemplate; |
| 1328 | }; |
| 1329 | |
| 1330 | |
| 1331 | /** |
| 1332 | * ObjectTemplate: (TODO(758124): Add comments.) |
| 1333 | */ |
| 1334 | class ObjectTemplate : public Template { |
| 1335 | public: |
| 1336 | static Local<ObjectTemplate> New(); |
| 1337 | /** Creates a new instance of this template.*/ |
| 1338 | Local<Object> NewInstance(); |
| 1339 | |
| 1340 | /** |
| 1341 | * Sets an accessor on the object template. |
| 1342 | * /param name (TODO(758124): Describe) |
| 1343 | * /param getter (TODO(758124): Describe) |
| 1344 | * /param setter (TODO(758124): Describe) |
| 1345 | * /param data ((TODO(758124): Describe) |
| 1346 | * /param settings settings must be one of: |
| 1347 | * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2 |
| 1348 | * /param attribute (TODO(758124): Describe) |
| 1349 | */ |
| 1350 | void SetAccessor(Handle<String> name, |
| 1351 | AccessorGetter getter, |
| 1352 | AccessorSetter setter = 0, |
| 1353 | Handle<Value> data = Handle<Value>(), |
| 1354 | AccessControl settings = DEFAULT, |
| 1355 | PropertyAttribute attribute = None); |
| 1356 | |
| 1357 | /** |
| 1358 | * Sets a named property handler on the object template. |
| 1359 | * /param getter (TODO(758124): Describe) |
| 1360 | * /param setter (TODO(758124): Describe) |
| 1361 | * /param query (TODO(758124): Describe) |
| 1362 | * /param deleter (TODO(758124): Describe) |
| 1363 | * /param enumerator (TODO(758124): Describe) |
| 1364 | * /param data (TODO(758124): Describe) |
| 1365 | */ |
| 1366 | void SetNamedPropertyHandler(NamedPropertyGetter getter, |
| 1367 | NamedPropertySetter setter = 0, |
| 1368 | NamedPropertyQuery query = 0, |
| 1369 | NamedPropertyDeleter deleter = 0, |
| 1370 | NamedPropertyEnumerator enumerator = 0, |
| 1371 | Handle<Value> data = Handle<Value>()); |
| 1372 | |
| 1373 | /** |
| 1374 | * Sets an indexed property handler on the object template. |
| 1375 | * /param getter (TODO(758124): Describe) |
| 1376 | * /param setter (TODO(758124): Describe) |
| 1377 | * /param query (TODO(758124): Describe) |
| 1378 | * /param deleter (TODO(758124): Describe) |
| 1379 | * /param enumerator (TODO(758124): Describe) |
| 1380 | * /param data (TODO(758124): Describe) |
| 1381 | */ |
| 1382 | void SetIndexedPropertyHandler(IndexedPropertyGetter getter, |
| 1383 | IndexedPropertySetter setter = 0, |
| 1384 | IndexedPropertyQuery query = 0, |
| 1385 | IndexedPropertyDeleter deleter = 0, |
| 1386 | IndexedPropertyEnumerator enumerator = 0, |
| 1387 | Handle<Value> data = Handle<Value>()); |
| 1388 | /** |
| 1389 | * Sets the callback to be used when calling instances created from |
| 1390 | * this template as a function. If no callback is set, instances |
| 1391 | * behave like normal javascript objects that cannot be called as a |
| 1392 | * function. |
| 1393 | */ |
| 1394 | void SetCallAsFunctionHandler(InvocationCallback callback, |
| 1395 | Handle<Value> data = Handle<Value>()); |
| 1396 | |
| 1397 | /** Make object instances of the template as undetectable.*/ |
| 1398 | void MarkAsUndetectable(); |
| 1399 | |
| 1400 | /** TODO(758124): Clarify documentation: Object instances of the |
| 1401 | * template need access check.*/ |
| 1402 | void SetAccessCheckCallbacks(NamedSecurityCallback named_handler, |
| 1403 | IndexedSecurityCallback indexed_handler, |
| 1404 | Handle<Value> data = Handle<Value>()); |
| 1405 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame^] | 1406 | /** |
| 1407 | * Gets the number of internal fields for objects generated from |
| 1408 | * this template. |
| 1409 | */ |
| 1410 | int InternalFieldCount(); |
| 1411 | |
| 1412 | /** |
| 1413 | * Sets the number of internal fields for objects generated from |
| 1414 | * this template. |
| 1415 | */ |
| 1416 | void SetInternalFieldCount(int value); |
| 1417 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1418 | private: |
| 1419 | ObjectTemplate(); |
| 1420 | static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor); |
| 1421 | friend class FunctionTemplate; |
| 1422 | }; |
| 1423 | |
| 1424 | |
| 1425 | /** |
| 1426 | * A function signature which specifies which receivers and arguments |
| 1427 | * in can legally be called with. |
| 1428 | */ |
| 1429 | class Signature : public Data { |
| 1430 | public: |
| 1431 | static Local<Signature> New(Handle<FunctionTemplate> receiver = |
| 1432 | Handle<FunctionTemplate>(), |
| 1433 | int argc = 0, |
| 1434 | Handle<FunctionTemplate> argv[] = 0); |
| 1435 | private: |
| 1436 | Signature(); |
| 1437 | }; |
| 1438 | |
| 1439 | |
| 1440 | /** |
| 1441 | * A utility for determining the type of objects based on which |
| 1442 | * template they were constructed from. |
| 1443 | */ |
| 1444 | class TypeSwitch : public Data { |
| 1445 | public: |
| 1446 | static Local<TypeSwitch> New(Handle<FunctionTemplate> type); |
| 1447 | static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]); |
| 1448 | int match(Handle<Value> value); |
| 1449 | private: |
| 1450 | TypeSwitch(); |
| 1451 | }; |
| 1452 | |
| 1453 | |
| 1454 | // --- E x t e n s i o n s --- |
| 1455 | |
| 1456 | |
| 1457 | /** |
| 1458 | * Ignore |
| 1459 | */ |
| 1460 | class Extension { |
| 1461 | public: |
| 1462 | Extension(const char* name, |
| 1463 | const char* source = 0, |
| 1464 | int dep_count = 0, |
| 1465 | const char** deps = 0); |
| 1466 | virtual ~Extension() { } |
| 1467 | virtual v8::Handle<v8::FunctionTemplate> |
| 1468 | GetNativeFunction(v8::Handle<v8::String> name) { |
| 1469 | return v8::Handle<v8::FunctionTemplate>(); |
| 1470 | } |
| 1471 | |
| 1472 | const char* name() { return name_; } |
| 1473 | const char* source() { return source_; } |
| 1474 | int dependency_count() { return dep_count_; } |
| 1475 | const char** dependencies() { return deps_; } |
| 1476 | void set_auto_enable(bool value) { auto_enable_ = value; } |
| 1477 | bool auto_enable() { return auto_enable_; } |
| 1478 | |
| 1479 | private: |
| 1480 | const char* name_; |
| 1481 | const char* source_; |
| 1482 | int dep_count_; |
| 1483 | const char** deps_; |
| 1484 | bool auto_enable_; |
| 1485 | }; |
| 1486 | |
| 1487 | |
| 1488 | void RegisterExtension(Extension* extension); |
| 1489 | |
| 1490 | |
| 1491 | /** |
| 1492 | * Ignore |
| 1493 | */ |
| 1494 | class DeclareExtension { |
| 1495 | public: |
| 1496 | inline DeclareExtension(Extension* extension) { |
| 1497 | RegisterExtension(extension); |
| 1498 | } |
| 1499 | }; |
| 1500 | |
| 1501 | |
| 1502 | // --- S t a t i c s --- |
| 1503 | |
| 1504 | |
| 1505 | Handle<Primitive> Undefined(); |
| 1506 | Handle<Primitive> Null(); |
| 1507 | Handle<Boolean> True(); |
| 1508 | Handle<Boolean> False(); |
| 1509 | |
| 1510 | |
| 1511 | /** |
| 1512 | * A set of constraints that specifies the limits of the runtime's |
| 1513 | * memory use. |
| 1514 | */ |
| 1515 | class ResourceConstraints { |
| 1516 | public: |
| 1517 | ResourceConstraints(); |
| 1518 | int max_young_space_size() { return max_young_space_size_; } |
| 1519 | void set_max_young_space_size(int value) { max_young_space_size_ = value; } |
| 1520 | int max_old_space_size() { return max_old_space_size_; } |
| 1521 | void set_max_old_space_size(int value) { max_old_space_size_ = value; } |
| 1522 | uint32_t* stack_limit() { return stack_limit_; } |
| 1523 | void set_stack_limit(uint32_t* value) { stack_limit_ = value; } |
| 1524 | private: |
| 1525 | int max_young_space_size_; |
| 1526 | int max_old_space_size_; |
| 1527 | uint32_t* stack_limit_; |
| 1528 | }; |
| 1529 | |
| 1530 | |
| 1531 | bool SetResourceConstraints(ResourceConstraints* constraints); |
| 1532 | |
| 1533 | |
| 1534 | // --- E x c e p t i o n s --- |
| 1535 | |
| 1536 | |
| 1537 | typedef void (*FatalErrorCallback)(const char* location, const char* message); |
| 1538 | |
| 1539 | |
| 1540 | typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data); |
| 1541 | |
| 1542 | |
| 1543 | /** |
| 1544 | * Schedules an exception to be thrown when returning to javascript. When an |
| 1545 | * exception has been scheduled it is illegal to invoke any javascript |
| 1546 | * operation; the caller must return immediately and only after the exception |
| 1547 | * has been handled does it become legal to invoke javascript operations. |
| 1548 | */ |
| 1549 | Handle<Value> ThrowException(Handle<Value> exception); |
| 1550 | |
| 1551 | /** |
| 1552 | * Create new error objects by calling the corresponding error object |
| 1553 | * constructor with the message. |
| 1554 | */ |
| 1555 | class Exception { |
| 1556 | public: |
| 1557 | static Local<Value> RangeError(Handle<String> message); |
| 1558 | static Local<Value> ReferenceError(Handle<String> message); |
| 1559 | static Local<Value> SyntaxError(Handle<String> message); |
| 1560 | static Local<Value> TypeError(Handle<String> message); |
| 1561 | static Local<Value> Error(Handle<String> message); |
| 1562 | }; |
| 1563 | |
| 1564 | |
| 1565 | /** |
| 1566 | * Ignore |
| 1567 | */ |
| 1568 | struct VersionInfo { |
| 1569 | int major, minor, build_major, build_minor, revision; |
| 1570 | }; |
| 1571 | |
| 1572 | // --- C o u n t e r s C a l l b a c k s |
| 1573 | |
| 1574 | typedef int* (*CounterLookupCallback)(const wchar_t* name); |
| 1575 | |
| 1576 | // --- 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 --- |
| 1577 | typedef void (*FailedAccessCheckCallback)(Local<Object> target, |
| 1578 | AccessType type, |
| 1579 | Local<Value> data); |
| 1580 | |
| 1581 | // --- 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 |
| 1582 | |
| 1583 | /** |
| 1584 | * Applications can register a callback function which is called |
| 1585 | * before and after a major Garbage Collection. |
| 1586 | * Allocations are not allowed in the callback function, you therefore. |
| 1587 | * cannot manipulate objects (set or delete properties for example) |
| 1588 | * since it is likely such operations will result in the allocation of objects. |
| 1589 | */ |
| 1590 | typedef void (*GCCallback)(); |
| 1591 | |
| 1592 | |
| 1593 | // --- C o n t e x t G e n e r a t o r |
| 1594 | |
| 1595 | /** |
| 1596 | * Applications must provide a callback function which is called to generate |
| 1597 | * a context if a context wasn't deserialized from the snapshot. |
| 1598 | */ |
| 1599 | |
| 1600 | typedef Persistent<Context> (*ContextGenerator)(); |
| 1601 | |
| 1602 | |
| 1603 | /** |
| 1604 | * Container class for static utility functions. |
| 1605 | */ |
| 1606 | class V8 { |
| 1607 | public: |
| 1608 | static void SetFatalErrorHandler(FatalErrorCallback that); |
| 1609 | |
| 1610 | // TODO(758124): Clarify documentation: Prevent top level from |
| 1611 | // calling V8::FatalProcessOutOfMemory if HasOutOfMemoryException(); |
| 1612 | static void IgnoreOutOfMemoryException(); |
| 1613 | |
| 1614 | // Check if V8 is dead. |
| 1615 | static bool IsDead(); |
| 1616 | |
| 1617 | /** |
| 1618 | * TODO(758124): Clarify documentation - what is the "ones" in |
| 1619 | * "existing ones": Adds a message listener, does not overwrite any |
| 1620 | * existing ones with the same callback function. |
| 1621 | */ |
| 1622 | static bool AddMessageListener(MessageCallback that, |
| 1623 | Handle<Value> data = Handle<Value>()); |
| 1624 | |
| 1625 | /** |
| 1626 | * Remove all message listeners from the specified callback function. |
| 1627 | */ |
| 1628 | static void RemoveMessageListeners(MessageCallback that); |
| 1629 | |
| 1630 | /** |
| 1631 | * Sets v8 flags from a string. |
| 1632 | * TODO(758124): Describe flags? |
| 1633 | */ |
| 1634 | static void SetFlagsFromString(const char* str, int length); |
| 1635 | |
| 1636 | /** Sets the version fields in the given VersionInfo struct.*/ |
| 1637 | static void GetVersion(VersionInfo* info); |
| 1638 | |
| 1639 | /** |
| 1640 | * Enables the host application to provide a mechanism for recording |
| 1641 | * statistics counters. |
| 1642 | */ |
| 1643 | static void SetCounterFunction(CounterLookupCallback); |
| 1644 | |
| 1645 | /** |
| 1646 | * Enables the computation of a sliding window of states. The sliding |
| 1647 | * window information is recorded in statistics counters. |
| 1648 | */ |
| 1649 | static void EnableSlidingStateWindow(); |
| 1650 | |
| 1651 | /** Callback function for reporting failed access checks.*/ |
| 1652 | static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback); |
| 1653 | |
| 1654 | /** |
| 1655 | * Enables the host application to receive a notification before a major GC. |
| 1656 | * Allocations are not allowed in the callback function, you therefore |
| 1657 | * cannot manipulate objects (set or delete properties for example) |
| 1658 | * since it is likely such operations will result in the allocation of objects. |
| 1659 | */ |
| 1660 | static void SetGlobalGCPrologueCallback(GCCallback); |
| 1661 | |
| 1662 | /** |
| 1663 | * Enables the host application to receive a notification after a major GC. |
| 1664 | * (TODO(758124): is the following true for this one too?) |
| 1665 | * Allocations are not allowed in the callback function, you therefore |
| 1666 | * cannot manipulate objects (set or delete properties for example) |
| 1667 | * since it is likely such operations will result in the allocation of objects. |
| 1668 | */ |
| 1669 | static void SetGlobalGCEpilogueCallback(GCCallback); |
| 1670 | |
| 1671 | /** |
| 1672 | * Allows the host application to group objects together. If one object |
| 1673 | * in the group is alive, all objects in the group are alive. |
| 1674 | * After each GC, object groups are removed. It is intended to be used |
| 1675 | * in the before-GC callback function to simulate DOM tree connections |
| 1676 | * among JS wrapper objects. |
| 1677 | */ |
| 1678 | static void AddObjectToGroup(void* id, Persistent<Object> obj); |
| 1679 | |
| 1680 | /** |
| 1681 | * Initializes from snapshot if possible. Otherwise, attempts to initialize |
| 1682 | * from scratch. |
| 1683 | */ |
| 1684 | static bool Initialize(); |
| 1685 | |
| 1686 | private: |
| 1687 | V8(); |
| 1688 | |
| 1689 | static void** GlobalizeReference(void** handle); |
| 1690 | static void DisposeGlobal(void** global_handle); |
| 1691 | static void MakeWeak(void** global_handle, void* data, WeakReferenceCallback); |
| 1692 | static void ClearWeak(void** global_handle); |
| 1693 | static bool IsGlobalNearDeath(void** global_handle); |
| 1694 | static bool IsGlobalWeak(void** global_handle); |
| 1695 | |
| 1696 | template <class T> friend class Handle; |
| 1697 | template <class T> friend class Local; |
| 1698 | template <class T> friend class Persistent; |
| 1699 | friend class Context; |
| 1700 | }; |
| 1701 | |
| 1702 | |
| 1703 | /** |
| 1704 | * An external exception handler. |
| 1705 | */ |
| 1706 | class TryCatch { |
| 1707 | public: |
| 1708 | |
| 1709 | /** |
| 1710 | * Creates a new try/catch block and registers it with v8. |
| 1711 | */ |
| 1712 | TryCatch(); |
| 1713 | |
| 1714 | /** |
| 1715 | * Unregisters and deletes this try/catch block. |
| 1716 | */ |
| 1717 | ~TryCatch(); |
| 1718 | |
| 1719 | /** |
| 1720 | * Returns true if an exception has been caught by this try/catch block. |
| 1721 | */ |
| 1722 | bool HasCaught(); |
| 1723 | |
| 1724 | /** |
| 1725 | * Returns the exception caught by this try/catch block. If no exception has |
| 1726 | * been caught an empty handle is returned. |
| 1727 | * |
| 1728 | * The returned handle is valid until this TryCatch block has been destroyed. |
| 1729 | */ |
| 1730 | Local<Value> Exception(); |
| 1731 | |
| 1732 | /** |
| 1733 | * Clears any exceptions that may have been caught by this try/catch block. |
| 1734 | * After this method has been called, HasCaught() will return false. |
| 1735 | * |
| 1736 | * It is not necessary to clear a try/catch block before using it again; if |
| 1737 | * another exception is thrown the previously caught exception will just be |
| 1738 | * overwritten. However, it is often a good idea since it makes it easier |
| 1739 | * to determine which operation threw a given exception. |
| 1740 | */ |
| 1741 | void Reset(); |
| 1742 | |
| 1743 | void SetVerbose(bool value); |
| 1744 | |
| 1745 | public: |
| 1746 | TryCatch* next_; |
| 1747 | void* exception_; |
| 1748 | bool is_verbose_; |
| 1749 | }; |
| 1750 | |
| 1751 | |
| 1752 | // --- C o n t e x t --- |
| 1753 | |
| 1754 | |
| 1755 | /** |
| 1756 | * Ignore |
| 1757 | */ |
| 1758 | class ExtensionConfiguration { |
| 1759 | public: |
| 1760 | ExtensionConfiguration(int name_count, const char* names[]) |
| 1761 | : name_count_(name_count), names_(names) { } |
| 1762 | private: |
| 1763 | friend class ImplementationUtilities; |
| 1764 | int name_count_; |
| 1765 | const char** names_; |
| 1766 | }; |
| 1767 | |
| 1768 | |
| 1769 | /** |
| 1770 | * A sandboxed execution context with its own set of built-in objects |
| 1771 | * and functions. |
| 1772 | */ |
| 1773 | class Context { |
| 1774 | public: |
| 1775 | Local<Object> Global(); |
| 1776 | |
| 1777 | static Persistent<Context> New(ExtensionConfiguration* extensions = 0, |
| 1778 | Handle<ObjectTemplate> global_template = |
| 1779 | Handle<ObjectTemplate>(), |
| 1780 | Handle<Value> global_object = Handle<Value>()); |
| 1781 | |
| 1782 | /** Returns the context that is on the top of the stack.*/ |
| 1783 | static Local<Context> Current(); |
| 1784 | |
| 1785 | /** Returns the security context used to start JS execution.*/ |
| 1786 | static Local<Context> GetSecurityContext(); |
| 1787 | |
| 1788 | /** |
| 1789 | * Sets the security token for the context. To access an object in |
| 1790 | * another context, the security tokens must match. |
| 1791 | */ |
| 1792 | void SetSecurityToken(Handle<Value> token); |
| 1793 | |
| 1794 | /** Returns the security token of this context.*/ |
| 1795 | Handle<Value> GetSecurityToken(); |
| 1796 | |
| 1797 | void Enter(); |
| 1798 | void Exit(); |
| 1799 | |
| 1800 | /** Returns true if the context has experienced an out of memory situation.*/ |
| 1801 | bool HasOutOfMemoryException(); |
| 1802 | |
| 1803 | /** Returns true if called from within a context.*/ |
| 1804 | static bool InContext(); |
| 1805 | |
| 1806 | /** Returns true if called from within a security context.*/ |
| 1807 | static bool InSecurityContext(); |
| 1808 | |
| 1809 | /** |
| 1810 | * Stack-allocated class which sets the execution context for all |
| 1811 | * operations executed within a local scope. |
| 1812 | */ |
| 1813 | class Scope { |
| 1814 | public: |
| 1815 | inline Scope(Handle<Context> context) : context_(context) { |
| 1816 | context_->Enter(); |
| 1817 | } |
| 1818 | inline ~Scope() { context_->Exit(); } |
| 1819 | private: |
| 1820 | Handle<Context> context_; |
| 1821 | }; |
| 1822 | |
| 1823 | private: |
| 1824 | friend class Value; |
| 1825 | friend class Script; |
| 1826 | friend class Object; |
| 1827 | friend class Function; |
| 1828 | }; |
| 1829 | |
| 1830 | |
| 1831 | /** |
| 1832 | * Multiple threads in V8 are allowed, but only one thread at a time is |
| 1833 | * allowed to use V8. The definition of using V8' includes accessing |
| 1834 | * handles or holding onto object pointers obtained from V8 handles. |
| 1835 | * It is up to the user of V8 to ensure (perhaps with locking) that |
| 1836 | * this constraint is not violated. |
| 1837 | * |
| 1838 | * If you wish to start using V8 in a thread you can do this by constructing |
| 1839 | * a v8::Locker object. After the code using V8 has completed for the |
| 1840 | * current thread you can call the destructor. This can be combined |
| 1841 | * with C++ scope-based construction as follows: |
| 1842 | * |
| 1843 | * ... |
| 1844 | * { |
| 1845 | * v8::Locker locker; |
| 1846 | * ... |
| 1847 | * // Code using V8 goes here. |
| 1848 | * ... |
| 1849 | * } // Destructor called here |
| 1850 | * |
| 1851 | * If you wish to stop using V8 in a thread A you can do this by either |
| 1852 | * by destroying the v8::Locker object as above or by constructing a |
| 1853 | * v8::Unlocker object: |
| 1854 | * |
| 1855 | * { |
| 1856 | * v8::Unlocker unlocker; |
| 1857 | * ... |
| 1858 | * // Code not using V8 goes here while V8 can run in another thread. |
| 1859 | * ... |
| 1860 | * } // Destructor called here. |
| 1861 | * |
| 1862 | * The Unlocker object is intended for use in a long-running callback |
| 1863 | * from V8, where you want to release the V8 lock for other threads to |
| 1864 | * use. |
| 1865 | * |
| 1866 | * The v8::Locker is a recursive lock. That is, you can lock more than |
| 1867 | * once in a given thread. This can be useful if you have code that can |
| 1868 | * be called either from code that holds the lock or from code that does |
| 1869 | * not. The Unlocker is not recursive so you can not have several |
| 1870 | * Unlockers on the stack at once, and you can not use an Unlocker in a |
| 1871 | * thread that is not inside a Locker's scope. |
| 1872 | * |
| 1873 | * An unlocker will unlock several lockers if it has to and reinstate |
| 1874 | * the correct depth of locking on its destruction. eg.: |
| 1875 | * |
| 1876 | * // V8 not locked. |
| 1877 | * { |
| 1878 | * v8::Locker locker; |
| 1879 | * // V8 locked. |
| 1880 | * { |
| 1881 | * v8::Locker another_locker; |
| 1882 | * // V8 still locked (2 levels). |
| 1883 | * { |
| 1884 | * v8::Unlocker unlocker; |
| 1885 | * // V8 not locked. |
| 1886 | * } |
| 1887 | * // V8 locked again (2 levels). |
| 1888 | * } |
| 1889 | * // V8 still locked (1 level). |
| 1890 | * } |
| 1891 | * // V8 Now no longer locked. |
| 1892 | */ |
| 1893 | class Unlocker { |
| 1894 | public: |
| 1895 | Unlocker(); |
| 1896 | ~Unlocker(); |
| 1897 | }; |
| 1898 | |
| 1899 | |
| 1900 | class Locker { |
| 1901 | public: |
| 1902 | Locker(); |
| 1903 | ~Locker(); |
| 1904 | #ifdef DEBUG |
| 1905 | static void AssertIsLocked(); |
| 1906 | #else |
| 1907 | static inline void AssertIsLocked() { } |
| 1908 | #endif |
| 1909 | /* |
| 1910 | * Fires a timer every n ms that will switch between |
| 1911 | * multiple threads that are in contention for the V8 lock. |
| 1912 | */ |
| 1913 | static void StartPreemption(int every_n_ms); |
| 1914 | static void StopPreemption(); |
| 1915 | private: |
| 1916 | bool has_lock_; |
| 1917 | bool top_level_; |
| 1918 | }; |
| 1919 | |
| 1920 | |
| 1921 | |
| 1922 | // --- I m p l e m e n t a t i o n --- |
| 1923 | |
| 1924 | template <class T> |
| 1925 | Handle<T>::Handle() : val_(0) { } |
| 1926 | |
| 1927 | |
| 1928 | template <class T> |
| 1929 | Local<T>::Local() : Handle<T>() { } |
| 1930 | |
| 1931 | |
| 1932 | template <class T> |
| 1933 | Local<T> Local<T>::New(Handle<T> that) { |
| 1934 | if (that.IsEmpty()) return Local<T>(); |
| 1935 | void** p = reinterpret_cast<void**>(*that); |
| 1936 | return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p))); |
| 1937 | } |
| 1938 | |
| 1939 | |
| 1940 | template <class T> |
| 1941 | Persistent<T> Persistent<T>::New(Handle<T> that) { |
| 1942 | if (that.IsEmpty()) return Persistent<T>(); |
| 1943 | void** p = reinterpret_cast<void**>(*that); |
| 1944 | return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p))); |
| 1945 | } |
| 1946 | |
| 1947 | |
| 1948 | template <class T> |
| 1949 | bool Persistent<T>::IsNearDeath() { |
| 1950 | if (this->IsEmpty()) return false; |
| 1951 | return V8::IsGlobalNearDeath(reinterpret_cast<void**>(**this)); |
| 1952 | } |
| 1953 | |
| 1954 | |
| 1955 | template <class T> |
| 1956 | bool Persistent<T>::IsWeak() { |
| 1957 | if (this->IsEmpty()) return false; |
| 1958 | return V8::IsGlobalWeak(reinterpret_cast<void**>(**this)); |
| 1959 | } |
| 1960 | |
| 1961 | |
| 1962 | template <class T> |
| 1963 | void Persistent<T>::Dispose() { |
| 1964 | if (this->IsEmpty()) return; |
| 1965 | V8::DisposeGlobal(reinterpret_cast<void**>(**this)); |
| 1966 | } |
| 1967 | |
| 1968 | |
| 1969 | template <class T> |
| 1970 | Persistent<T>::Persistent() : Handle<T>() { } |
| 1971 | |
| 1972 | template <class T> |
| 1973 | void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) { |
| 1974 | V8::MakeWeak(reinterpret_cast<void**>(**this), parameters, callback); |
| 1975 | } |
| 1976 | |
| 1977 | template <class T> |
| 1978 | void Persistent<T>::ClearWeak() { |
| 1979 | V8::ClearWeak(reinterpret_cast<void**>(**this)); |
| 1980 | } |
| 1981 | |
| 1982 | template <class T> |
| 1983 | T* Handle<T>::operator->() { |
| 1984 | return val_; |
| 1985 | } |
| 1986 | |
| 1987 | |
| 1988 | template <class T> |
| 1989 | T* Handle<T>::operator*() { |
| 1990 | return val_; |
| 1991 | } |
| 1992 | |
| 1993 | |
| 1994 | Local<Value> Arguments::operator[](int i) const { |
| 1995 | if (i < 0 || length_ <= i) return Local<Value>(*Undefined()); |
| 1996 | return Local<Value>(reinterpret_cast<Value*>(values_ - i)); |
| 1997 | } |
| 1998 | |
| 1999 | |
| 2000 | Local<Function> Arguments::Callee() const { |
| 2001 | return callee_; |
| 2002 | } |
| 2003 | |
| 2004 | |
| 2005 | Local<Object> Arguments::This() const { |
| 2006 | return Local<Object>(reinterpret_cast<Object*>(values_ + 1)); |
| 2007 | } |
| 2008 | |
| 2009 | |
| 2010 | Local<Object> Arguments::Holder() const { |
| 2011 | return holder_; |
| 2012 | } |
| 2013 | |
| 2014 | |
| 2015 | Local<Value> Arguments::Data() const { |
| 2016 | return data_; |
| 2017 | } |
| 2018 | |
| 2019 | |
| 2020 | bool Arguments::IsConstructCall() const { |
| 2021 | return is_construct_call_; |
| 2022 | } |
| 2023 | |
| 2024 | |
| 2025 | int Arguments::Length() const { |
| 2026 | return length_; |
| 2027 | } |
| 2028 | |
| 2029 | |
| 2030 | Local<Value> AccessorInfo::Data() const { |
| 2031 | return data_; |
| 2032 | } |
| 2033 | |
| 2034 | |
| 2035 | Local<Object> AccessorInfo::This() const { |
| 2036 | return self_; |
| 2037 | } |
| 2038 | |
| 2039 | |
| 2040 | Local<Object> AccessorInfo::Holder() const { |
| 2041 | return holder_; |
| 2042 | } |
| 2043 | |
| 2044 | |
| 2045 | template <class T> |
| 2046 | Local<T> HandleScope::Close(Handle<T> value) { |
| 2047 | void** after = RawClose(reinterpret_cast<void**>(*value)); |
| 2048 | return Local<T>(reinterpret_cast<T*>(after)); |
| 2049 | } |
| 2050 | |
| 2051 | Handle<String> ScriptOrigin::ResourceName() { |
| 2052 | return resource_name_; |
| 2053 | } |
| 2054 | |
| 2055 | |
| 2056 | Handle<Integer> ScriptOrigin::ResourceLineOffset() { |
| 2057 | return resource_line_offset_; |
| 2058 | } |
| 2059 | |
| 2060 | |
| 2061 | Handle<Integer> ScriptOrigin::ResourceColumnOffset() { |
| 2062 | return resource_column_offset_; |
| 2063 | } |
| 2064 | |
| 2065 | |
| 2066 | Handle<Boolean> Boolean::New(bool value) { |
| 2067 | return value ? True() : False(); |
| 2068 | } |
| 2069 | |
| 2070 | |
| 2071 | void Template::Set(const char* name, v8::Handle<Data> value) { |
| 2072 | Set(v8::String::New(name), value); |
| 2073 | } |
| 2074 | |
| 2075 | |
| 2076 | /** |
| 2077 | * \example evaluator.cc |
| 2078 | * A simple evaluator that takes a list of expressions on the |
| 2079 | * command-line and executes them. |
| 2080 | */ |
| 2081 | |
| 2082 | |
| 2083 | /** |
| 2084 | * \example process.cc |
| 2085 | */ |
| 2086 | |
| 2087 | |
| 2088 | } // namespace v8 |
| 2089 | |
| 2090 | |
| 2091 | #undef EXPORT |
| 2092 | #undef TYPE_CHECK |
| 2093 | |
| 2094 | |
| 2095 | #endif // _V8 |