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