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