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