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(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 527 | |
| 528 | /** |
| 529 | * Returns the script id value. |
| 530 | */ |
| 531 | Local<Value> Id(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | }; |
| 533 | |
| 534 | |
| 535 | /** |
| 536 | * An error message. |
| 537 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 538 | class EXPORT Message { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 539 | public: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 540 | Local<String> Get() const; |
| 541 | Local<String> GetSourceLine() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 542 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 543 | Handle<Value> GetScriptResourceName() 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 number, 1-based, of the line where the error occurred. |
| 547 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 548 | int GetLineNumber() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 549 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 550 | /** |
| 551 | * Returns the index within the script of the first character where |
| 552 | * the error occurred. |
| 553 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 554 | int GetStartPosition() const; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 555 | |
| 556 | /** |
| 557 | * Returns the index within the script of the last character where |
| 558 | * the error occurred. |
| 559 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 560 | int GetEndPosition() const; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 561 | |
| 562 | /** |
| 563 | * Returns the index within the line of the first character where |
| 564 | * the error occurred. |
| 565 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 566 | int GetStartColumn() const; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 567 | |
| 568 | /** |
| 569 | * Returns the index within the line of the last character where |
| 570 | * the error occurred. |
| 571 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 572 | int GetEndColumn() const; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 573 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 574 | // TODO(1245381): Print to a string instead of on a FILE. |
| 575 | static void PrintCurrentStackTrace(FILE* out); |
| 576 | }; |
| 577 | |
| 578 | |
| 579 | // --- V a l u e --- |
| 580 | |
| 581 | |
| 582 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 583 | * The superclass of all JavaScript values and objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 584 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 585 | class EXPORT Value : public Data { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 586 | public: |
| 587 | |
| 588 | /** |
| 589 | * Returns true if this value is the undefined value. See ECMA-262 |
| 590 | * 4.3.10. |
| 591 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 592 | bool IsUndefined() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 593 | |
| 594 | /** |
| 595 | * Returns true if this value is the null value. See ECMA-262 |
| 596 | * 4.3.11. |
| 597 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 598 | bool IsNull() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 599 | |
| 600 | /** |
| 601 | * Returns true if this value is true. |
| 602 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 603 | bool IsTrue() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 604 | |
| 605 | /** |
| 606 | * Returns true if this value is false. |
| 607 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 608 | bool IsFalse() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 609 | |
| 610 | /** |
| 611 | * Returns true if this value is an instance of the String type. |
| 612 | * See ECMA-262 8.4. |
| 613 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 614 | bool IsString() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 615 | |
| 616 | /** |
| 617 | * Returns true if this value is a function. |
| 618 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 619 | bool IsFunction() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 620 | |
| 621 | /** |
| 622 | * Returns true if this value is an array. |
| 623 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 624 | bool IsArray() 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 an object. |
| 628 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 629 | bool IsObject() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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 boolean. |
| 633 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 634 | bool IsBoolean() const; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +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 a number. |
| 638 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 639 | bool IsNumber() 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 external. |
| 643 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 644 | bool IsExternal() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 645 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 646 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 647 | * Returns true if this value is a 32-bit signed integer. |
| 648 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 649 | bool IsInt32() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 650 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 651 | /** |
| 652 | * Returns true if this value is a Date. |
| 653 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 654 | bool IsDate() const; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 655 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 656 | Local<Boolean> ToBoolean() const; |
| 657 | Local<Number> ToNumber() const; |
| 658 | Local<String> ToString() const; |
| 659 | Local<String> ToDetailString() const; |
| 660 | Local<Object> ToObject() const; |
| 661 | Local<Integer> ToInteger() const; |
| 662 | Local<Uint32> ToUint32() const; |
| 663 | Local<Int32> ToInt32() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 664 | |
| 665 | /** |
| 666 | * Attempts to convert a string to an array index. |
| 667 | * Returns an empty handle if the conversion fails. |
| 668 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 669 | Local<Uint32> ToArrayIndex() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 670 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 671 | bool BooleanValue() const; |
| 672 | double NumberValue() const; |
| 673 | int64_t IntegerValue() const; |
| 674 | uint32_t Uint32Value() const; |
| 675 | int32_t Int32Value() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 676 | |
| 677 | /** JS == */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 678 | bool Equals(Handle<Value> that) const; |
| 679 | bool StrictEquals(Handle<Value> that) const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 680 | }; |
| 681 | |
| 682 | |
| 683 | /** |
| 684 | * The superclass of primitive values. See ECMA-262 4.3.2. |
| 685 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 686 | class EXPORT Primitive : public Value { }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 687 | |
| 688 | |
| 689 | /** |
| 690 | * A primitive boolean value (ECMA-262, 4.3.14). Either the true |
| 691 | * or false value. |
| 692 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 693 | class EXPORT Boolean : public Primitive { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 694 | public: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 695 | bool Value() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 696 | static inline Handle<Boolean> New(bool value); |
| 697 | }; |
| 698 | |
| 699 | |
| 700 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 701 | * A JavaScript string value (ECMA-262, 4.3.17). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 702 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 703 | class EXPORT String : public Primitive { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 704 | public: |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 705 | |
| 706 | /** |
| 707 | * Returns the number of characters in this string. |
| 708 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 709 | int Length() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 710 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 711 | /** |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 712 | * Returns the number of bytes in the UTF-8 encoded |
| 713 | * representation of this string. |
| 714 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 715 | int Utf8Length() const; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 716 | |
| 717 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 718 | * Write the contents of the string to an external buffer. |
| 719 | * If no arguments are given, expects the buffer to be large |
| 720 | * enough to hold the entire string and NULL terminator. Copies |
| 721 | * the contents of the string and the NULL terminator into the |
| 722 | * buffer. |
| 723 | * |
| 724 | * Copies up to length characters into the output buffer. |
| 725 | * Only null-terminates if there is enough space in the buffer. |
| 726 | * |
| 727 | * \param buffer The buffer into which the string will be copied. |
| 728 | * \param start The starting position within the string at which |
| 729 | * copying begins. |
| 730 | * \param length The number of bytes to copy from the string. |
| 731 | * \return The number of characters copied to the buffer |
| 732 | * excluding the NULL terminator. |
| 733 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 734 | int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16 |
| 735 | int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII |
| 736 | int WriteUtf8(char* buffer, int length = -1) const; // UTF-8 |
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 | /** |
ager@chromium.org | 563b8dc | 2009-03-20 14:23:52 +0000 | [diff] [blame^] | 739 | * A zero length string. |
| 740 | */ |
| 741 | static v8::Local<v8::String> Empty(); |
| 742 | |
| 743 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 744 | * Returns true if the string is external |
| 745 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 746 | bool IsExternal() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 747 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 748 | /** |
| 749 | * Returns true if the string is both external and ascii |
| 750 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 751 | bool IsExternalAscii() const; |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 752 | /** |
| 753 | * An ExternalStringResource is a wrapper around a two-byte string |
| 754 | * buffer that resides outside V8's heap. Implement an |
| 755 | * ExternalStringResource to manage the life cycle of the underlying |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 756 | * buffer. Note that the string data must be immutable. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 757 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 758 | class EXPORT ExternalStringResource { // NOLINT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 759 | public: |
| 760 | /** |
| 761 | * Override the destructor to manage the life cycle of the underlying |
| 762 | * buffer. |
| 763 | */ |
| 764 | virtual ~ExternalStringResource() {} |
| 765 | /** The string data from the underlying buffer.*/ |
| 766 | virtual const uint16_t* data() const = 0; |
| 767 | /** The length of the string. That is, the number of two-byte characters.*/ |
| 768 | virtual size_t length() const = 0; |
| 769 | protected: |
| 770 | ExternalStringResource() {} |
| 771 | private: |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 772 | // Disallow copying and assigning. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 773 | ExternalStringResource(const ExternalStringResource&); |
| 774 | void operator=(const ExternalStringResource&); |
| 775 | }; |
| 776 | |
| 777 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 778 | * An ExternalAsciiStringResource is a wrapper around an ascii |
| 779 | * string buffer that resides outside V8's heap. Implement an |
| 780 | * ExternalAsciiStringResource to manage the life cycle of the |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 781 | * underlying buffer. Note that the string data must be immutable |
| 782 | * and that the data must be strict 7-bit ASCII, not Latin1 or |
| 783 | * UTF-8, which would require special treatment internally in the |
| 784 | * engine and, in the case of UTF-8, do not allow efficient indexing. |
| 785 | * 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] | 786 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 787 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 788 | class EXPORT ExternalAsciiStringResource { // NOLINT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 789 | public: |
| 790 | /** |
| 791 | * Override the destructor to manage the life cycle of the underlying |
| 792 | * buffer. |
| 793 | */ |
| 794 | virtual ~ExternalAsciiStringResource() {} |
| 795 | /** The string data from the underlying buffer.*/ |
| 796 | virtual const char* data() const = 0; |
| 797 | /** The number of ascii characters in the string.*/ |
| 798 | virtual size_t length() const = 0; |
| 799 | protected: |
| 800 | ExternalAsciiStringResource() {} |
| 801 | private: |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 802 | // Disallow copying and assigning. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 803 | ExternalAsciiStringResource(const ExternalAsciiStringResource&); |
| 804 | void operator=(const ExternalAsciiStringResource&); |
| 805 | }; |
| 806 | |
| 807 | /** |
| 808 | * Get the ExternalStringResource for an external string. Only |
| 809 | * valid if IsExternal() returns true. |
| 810 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 811 | ExternalStringResource* GetExternalStringResource() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 812 | |
| 813 | /** |
| 814 | * Get the ExternalAsciiStringResource for an external ascii string. |
| 815 | * Only valid if IsExternalAscii() returns true. |
| 816 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 817 | ExternalAsciiStringResource* GetExternalAsciiStringResource() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 818 | |
| 819 | static String* Cast(v8::Value* obj); |
| 820 | |
| 821 | /** |
| 822 | * Allocates a new string from either utf-8 encoded or ascii data. |
| 823 | * The second parameter 'length' gives the buffer length. |
| 824 | * If the data is utf-8 encoded, the caller must |
| 825 | * be careful to supply the length parameter. |
| 826 | * If it is not given, the function calls |
| 827 | * 'strlen' to determine the buffer length, it might be |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 828 | * wrong if 'data' contains a null character. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 829 | */ |
| 830 | static Local<String> New(const char* data, int length = -1); |
| 831 | |
| 832 | /** Allocates a new string from utf16 data.*/ |
| 833 | static Local<String> New(const uint16_t* data, int length = -1); |
| 834 | |
| 835 | /** Creates a symbol. Returns one if it exists already.*/ |
| 836 | static Local<String> NewSymbol(const char* data, int length = -1); |
| 837 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 838 | /** |
| 839 | * Creates a new external string using the 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(ExternalStringResource* resource); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 847 | |
| 848 | /** |
| 849 | * Associate an external string resource with this string by transforming it |
| 850 | * in place so that existing references to this string in the JavaScript heap |
| 851 | * will use the external string resource. The external string resource's |
| 852 | * character contents needs to be equivalent to this string. |
| 853 | * Returns true if the string has been changed to be an external string. |
| 854 | * The string is not modified if the operation fails. |
| 855 | */ |
| 856 | bool MakeExternal(ExternalStringResource* resource); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 857 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 858 | /** |
| 859 | * Creates a new external string using the ascii data defined in the given |
| 860 | * resource. The resource is deleted when the external string is no |
| 861 | * longer live on V8's heap. The caller of this function should not |
| 862 | * delete or modify the resource. Neither should the underlying buffer be |
| 863 | * deallocated or modified except through the destructor of the |
| 864 | * external string resource. |
| 865 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 866 | static Local<String> NewExternal(ExternalAsciiStringResource* resource); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 867 | |
| 868 | /** |
| 869 | * Associate an external string resource with this string by transforming it |
| 870 | * in place so that existing references to this string in the JavaScript heap |
| 871 | * will use the external string resource. The external string resource's |
| 872 | * character contents needs to be equivalent to this string. |
| 873 | * Returns true if the string has been changed to be an external string. |
| 874 | * The string is not modified if the operation fails. |
| 875 | */ |
| 876 | bool MakeExternal(ExternalAsciiStringResource* resource); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 877 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 878 | /** 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] | 879 | static Local<String> NewUndetectable(const char* data, int length = -1); |
| 880 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 881 | /** Creates an undetectable string from the supplied utf-16 data.*/ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 882 | static Local<String> NewUndetectable(const uint16_t* data, int length = -1); |
| 883 | |
| 884 | /** |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 885 | * Converts an object to a utf8-encoded character array. Useful if |
| 886 | * you want to print the object. |
| 887 | */ |
| 888 | class EXPORT Utf8Value { |
| 889 | public: |
| 890 | explicit Utf8Value(Handle<v8::Value> obj); |
| 891 | ~Utf8Value(); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 892 | char* operator*() const { return str_; } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 893 | int length() { return length_; } |
| 894 | private: |
| 895 | char* str_; |
| 896 | int length_; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 897 | |
| 898 | // Disallow copying and assigning. |
| 899 | Utf8Value(const Utf8Value&); |
| 900 | void operator=(const Utf8Value&); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 901 | }; |
| 902 | |
| 903 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 904 | * Converts an object to an ascii string. |
| 905 | * Useful if you want to print the object. |
| 906 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 907 | class EXPORT AsciiValue { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 908 | public: |
| 909 | explicit AsciiValue(Handle<v8::Value> obj); |
| 910 | ~AsciiValue(); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 911 | char* operator*() const { return str_; } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 912 | int length() { return length_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 913 | private: |
| 914 | char* str_; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 915 | int length_; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 916 | |
| 917 | // Disallow copying and assigning. |
| 918 | AsciiValue(const AsciiValue&); |
| 919 | void operator=(const AsciiValue&); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 920 | }; |
| 921 | |
| 922 | /** |
| 923 | * Converts an object to a two-byte string. |
| 924 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 925 | class EXPORT Value { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 926 | public: |
| 927 | explicit Value(Handle<v8::Value> obj); |
| 928 | ~Value(); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 929 | uint16_t* operator*() const { return str_; } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 930 | int length() { return length_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 931 | private: |
| 932 | uint16_t* str_; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 933 | int length_; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 934 | |
| 935 | // Disallow copying and assigning. |
| 936 | Value(const Value&); |
| 937 | void operator=(const Value&); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 938 | }; |
| 939 | }; |
| 940 | |
| 941 | |
| 942 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 943 | * A JavaScript number value (ECMA-262, 4.3.20) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 944 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 945 | class EXPORT Number : public Primitive { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 946 | public: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 947 | double Value() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 948 | static Local<Number> New(double value); |
| 949 | static Number* Cast(v8::Value* obj); |
| 950 | private: |
| 951 | Number(); |
| 952 | }; |
| 953 | |
| 954 | |
| 955 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 956 | * A JavaScript value representing a signed integer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 957 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 958 | class EXPORT Integer : public Number { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 959 | public: |
| 960 | static Local<Integer> New(int32_t value); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 961 | int64_t Value() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 962 | static Integer* Cast(v8::Value* obj); |
| 963 | private: |
| 964 | Integer(); |
| 965 | }; |
| 966 | |
| 967 | |
| 968 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 969 | * A JavaScript value representing a 32-bit signed integer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 970 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 971 | class EXPORT Int32 : public Integer { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 972 | public: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 973 | int32_t Value() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 974 | private: |
| 975 | Int32(); |
| 976 | }; |
| 977 | |
| 978 | |
| 979 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 980 | * A JavaScript value representing a 32-bit unsigned integer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 981 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 982 | class EXPORT Uint32 : public Integer { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 983 | public: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 984 | uint32_t Value() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 985 | private: |
| 986 | Uint32(); |
| 987 | }; |
| 988 | |
| 989 | |
| 990 | /** |
| 991 | * An instance of the built-in Date constructor (ECMA-262, 15.9). |
| 992 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 993 | class EXPORT Date : public Value { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 994 | public: |
| 995 | static Local<Value> New(double time); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 996 | |
| 997 | /** |
| 998 | * A specialization of Value::NumberValue that is more efficient |
| 999 | * because we know the structure of this object. |
| 1000 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1001 | double NumberValue() const; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1002 | |
| 1003 | static Date* Cast(v8::Value* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1004 | }; |
| 1005 | |
| 1006 | |
| 1007 | enum PropertyAttribute { |
| 1008 | None = 0, |
| 1009 | ReadOnly = 1 << 0, |
| 1010 | DontEnum = 1 << 1, |
| 1011 | DontDelete = 1 << 2 |
| 1012 | }; |
| 1013 | |
| 1014 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1015 | * A JavaScript object (ECMA-262, 4.3.3) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1016 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1017 | class EXPORT Object : public Value { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1018 | public: |
| 1019 | bool Set(Handle<Value> key, |
| 1020 | Handle<Value> value, |
| 1021 | PropertyAttribute attribs = None); |
| 1022 | Local<Value> Get(Handle<Value> key); |
| 1023 | |
| 1024 | // TODO(1245389): Replace the type-specific versions of these |
| 1025 | // functions with generic ones that accept a Handle<Value> key. |
| 1026 | bool Has(Handle<String> key); |
| 1027 | bool Delete(Handle<String> key); |
| 1028 | bool Has(uint32_t index); |
| 1029 | bool Delete(uint32_t index); |
| 1030 | |
| 1031 | /** |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1032 | * Returns an array containing the names of the enumerable properties |
| 1033 | * of this object, including properties from prototype objects. The |
| 1034 | * array returned by this method contains the same values as would |
| 1035 | * be enumerated by a for-in statement over this object. |
| 1036 | */ |
| 1037 | Local<Array> GetPropertyNames(); |
| 1038 | |
| 1039 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1040 | * Get the prototype object. This does not skip objects marked to |
| 1041 | * be skipped by __proto__ and it does not consult the security |
| 1042 | * handler. |
| 1043 | */ |
| 1044 | Local<Value> GetPrototype(); |
| 1045 | |
| 1046 | /** |
| 1047 | * Call builtin Object.prototype.toString on this object. |
| 1048 | * This is different from Value::ToString() that may call |
| 1049 | * user-defined toString function. This one does not. |
| 1050 | */ |
| 1051 | Local<String> ObjectProtoToString(); |
| 1052 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1053 | /** Gets the number of internal fields for this Object. */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1054 | int InternalFieldCount(); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1055 | /** Gets the value in an internal field. */ |
| 1056 | Local<Value> GetInternalField(int index); |
| 1057 | /** Sets the value in an internal field. */ |
| 1058 | void SetInternalField(int index, Handle<Value> value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1059 | |
| 1060 | // Testers for local properties. |
| 1061 | bool HasRealNamedProperty(Handle<String> key); |
| 1062 | bool HasRealIndexedProperty(uint32_t index); |
| 1063 | bool HasRealNamedCallbackProperty(Handle<String> key); |
| 1064 | |
| 1065 | /** |
| 1066 | * If result.IsEmpty() no real property was located in the prototype chain. |
| 1067 | * This means interceptors in the prototype chain are not called. |
| 1068 | */ |
| 1069 | Handle<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key); |
| 1070 | |
| 1071 | /** Tests for a named lookup interceptor.*/ |
| 1072 | bool HasNamedLookupInterceptor(); |
| 1073 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1074 | /** Tests for an index lookup interceptor.*/ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1075 | bool HasIndexedLookupInterceptor(); |
| 1076 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1077 | /** |
| 1078 | * Turns on access check on the object if the object is an instance of |
| 1079 | * a template that has access check callbacks. If an object has no |
| 1080 | * access check info, the object cannot be accessed by anyone. |
| 1081 | */ |
| 1082 | void TurnOnAccessCheck(); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 1083 | |
| 1084 | /** |
| 1085 | * Returns the identity hash for this object. The current implemenation uses |
| 1086 | * a hidden property on the object to store the identity hash. |
| 1087 | */ |
| 1088 | int GetIdentityHash(); |
| 1089 | |
| 1090 | /** |
| 1091 | * Access hidden properties on JavaScript objects. These properties are |
| 1092 | * hidden from the executing JavaScript and only accessible through the V8 |
| 1093 | * C++ API. Hidden properties introduced by V8 internally (for example the |
| 1094 | * identity hash) are prefixed with "v8::". |
| 1095 | */ |
| 1096 | bool SetHiddenValue(Handle<String> key, Handle<Value> value); |
| 1097 | Local<Value> GetHiddenValue(Handle<String> key); |
| 1098 | bool DeleteHiddenValue(Handle<String> key); |
| 1099 | |
| 1100 | /** |
| 1101 | * Clone this object with a fast but shallow copy. Values will point |
| 1102 | * to the same values as the original object. |
| 1103 | */ |
| 1104 | Local<Object> Clone(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1105 | |
| 1106 | static Local<Object> New(); |
| 1107 | static Object* Cast(Value* obj); |
| 1108 | private: |
| 1109 | Object(); |
| 1110 | }; |
| 1111 | |
| 1112 | |
| 1113 | /** |
| 1114 | * An instance of the built-in array constructor (ECMA-262, 15.4.2). |
| 1115 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1116 | class EXPORT Array : public Object { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1117 | public: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1118 | uint32_t Length() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1119 | |
| 1120 | static Local<Array> New(int length = 0); |
| 1121 | static Array* Cast(Value* obj); |
| 1122 | private: |
| 1123 | Array(); |
| 1124 | }; |
| 1125 | |
| 1126 | |
| 1127 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1128 | * A JavaScript function object (ECMA-262, 15.3). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1129 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1130 | class EXPORT Function : public Object { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1131 | public: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1132 | Local<Object> NewInstance() const; |
| 1133 | Local<Object> NewInstance(int argc, Handle<Value> argv[]) const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1134 | Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]); |
| 1135 | void SetName(Handle<String> name); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1136 | Handle<Value> GetName() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1137 | static Function* Cast(Value* obj); |
| 1138 | private: |
| 1139 | Function(); |
| 1140 | }; |
| 1141 | |
| 1142 | |
| 1143 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1144 | * A JavaScript value that wraps a c++ void*. This type of value is |
| 1145 | * mainly used to associate c++ data structures with JavaScript |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1146 | * objects. |
| 1147 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1148 | class EXPORT External : public Value { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1149 | public: |
| 1150 | static Local<External> New(void* value); |
| 1151 | static External* Cast(Value* obj); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1152 | void* Value() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1153 | private: |
| 1154 | External(); |
| 1155 | }; |
| 1156 | |
| 1157 | |
| 1158 | // --- T e m p l a t e s --- |
| 1159 | |
| 1160 | |
| 1161 | /** |
| 1162 | * The superclass of object and function templates. |
| 1163 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1164 | class EXPORT Template : public Data { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1165 | public: |
| 1166 | /** Adds a property to each instance created by this template.*/ |
| 1167 | void Set(Handle<String> name, Handle<Data> value, |
| 1168 | PropertyAttribute attributes = None); |
| 1169 | inline void Set(const char* name, Handle<Data> value); |
| 1170 | private: |
| 1171 | Template(); |
| 1172 | |
| 1173 | friend class ObjectTemplate; |
| 1174 | friend class FunctionTemplate; |
| 1175 | }; |
| 1176 | |
| 1177 | |
| 1178 | /** |
| 1179 | * The argument information given to function call callbacks. This |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1180 | * class provides access to information about the context of the call, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1181 | * including the receiver, the number and values of arguments, and |
| 1182 | * the holder of the function. |
| 1183 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1184 | class EXPORT Arguments { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1185 | public: |
| 1186 | inline int Length() const; |
| 1187 | inline Local<Value> operator[](int i) const; |
| 1188 | inline Local<Function> Callee() const; |
| 1189 | inline Local<Object> This() const; |
| 1190 | inline Local<Object> Holder() const; |
| 1191 | inline bool IsConstructCall() const; |
| 1192 | inline Local<Value> Data() const; |
| 1193 | private: |
| 1194 | Arguments(); |
| 1195 | friend class ImplementationUtilities; |
| 1196 | inline Arguments(Local<Value> data, |
| 1197 | Local<Object> holder, |
| 1198 | Local<Function> callee, |
| 1199 | bool is_construct_call, |
| 1200 | void** values, int length); |
| 1201 | Local<Value> data_; |
| 1202 | Local<Object> holder_; |
| 1203 | Local<Function> callee_; |
| 1204 | bool is_construct_call_; |
| 1205 | void** values_; |
| 1206 | int length_; |
| 1207 | }; |
| 1208 | |
| 1209 | |
| 1210 | /** |
| 1211 | * The information passed to an accessor callback about the context |
| 1212 | * of the property access. |
| 1213 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1214 | class EXPORT AccessorInfo { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1215 | public: |
| 1216 | inline AccessorInfo(Local<Object> self, |
| 1217 | Local<Value> data, |
| 1218 | Local<Object> holder) |
| 1219 | : self_(self), data_(data), holder_(holder) { } |
| 1220 | inline Local<Value> Data() const; |
| 1221 | inline Local<Object> This() const; |
| 1222 | inline Local<Object> Holder() const; |
| 1223 | private: |
| 1224 | Local<Object> self_; |
| 1225 | Local<Value> data_; |
| 1226 | Local<Object> holder_; |
| 1227 | }; |
| 1228 | |
| 1229 | |
| 1230 | typedef Handle<Value> (*InvocationCallback)(const Arguments& args); |
| 1231 | |
| 1232 | typedef int (*LookupCallback)(Local<Object> self, Local<String> name); |
| 1233 | |
| 1234 | /** |
| 1235 | * Accessor[Getter|Setter] are used as callback functions when |
| 1236 | * setting|getting a particular property. See objectTemplate::SetAccessor. |
| 1237 | */ |
| 1238 | typedef Handle<Value> (*AccessorGetter)(Local<String> property, |
| 1239 | const AccessorInfo& info); |
| 1240 | |
| 1241 | |
| 1242 | typedef void (*AccessorSetter)(Local<String> property, |
| 1243 | Local<Value> value, |
| 1244 | const AccessorInfo& info); |
| 1245 | |
| 1246 | |
| 1247 | /** |
| 1248 | * NamedProperty[Getter|Setter] are used as interceptors on object. |
| 1249 | * See ObjectTemplate::SetNamedPropertyHandler. |
| 1250 | */ |
| 1251 | typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property, |
| 1252 | const AccessorInfo& info); |
| 1253 | |
| 1254 | |
| 1255 | /** |
| 1256 | * Returns the value if the setter intercepts the request. |
| 1257 | * Otherwise, returns an empty handle. |
| 1258 | */ |
| 1259 | typedef Handle<Value> (*NamedPropertySetter)(Local<String> property, |
| 1260 | Local<Value> value, |
| 1261 | const AccessorInfo& info); |
| 1262 | |
| 1263 | |
| 1264 | /** |
| 1265 | * Returns a non-empty handle if the interceptor intercepts the request. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1266 | * The result is true if the property exists and false otherwise. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1267 | */ |
| 1268 | typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property, |
| 1269 | const AccessorInfo& info); |
| 1270 | |
| 1271 | |
| 1272 | /** |
| 1273 | * Returns a non-empty handle if the deleter intercepts the request. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1274 | * The return value is true if the property could be deleted and false |
| 1275 | * otherwise. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1276 | */ |
| 1277 | typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property, |
| 1278 | const AccessorInfo& info); |
| 1279 | |
| 1280 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1281 | * Returns an array containing the names of the properties the named |
| 1282 | * property getter intercepts. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1283 | */ |
| 1284 | typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info); |
| 1285 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1286 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1287 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1288 | * Returns the value of the property if the getter intercepts the |
| 1289 | * request. Otherwise, returns an empty handle. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1290 | */ |
| 1291 | typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index, |
| 1292 | const AccessorInfo& info); |
| 1293 | |
| 1294 | |
| 1295 | /** |
| 1296 | * Returns the value if the setter intercepts the request. |
| 1297 | * Otherwise, returns an empty handle. |
| 1298 | */ |
| 1299 | typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index, |
| 1300 | Local<Value> value, |
| 1301 | const AccessorInfo& info); |
| 1302 | |
| 1303 | |
| 1304 | /** |
| 1305 | * Returns a non-empty handle if the interceptor intercepts the request. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1306 | * The result is true if the property exists and false otherwise. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1307 | */ |
| 1308 | typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index, |
| 1309 | const AccessorInfo& info); |
| 1310 | |
| 1311 | /** |
| 1312 | * Returns a non-empty handle if the deleter intercepts the request. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1313 | * The return value is true if the property could be deleted and false |
| 1314 | * otherwise. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1315 | */ |
| 1316 | typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index, |
| 1317 | const AccessorInfo& info); |
| 1318 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1319 | /** |
| 1320 | * Returns an array containing the indices of the properties the |
| 1321 | * indexed property getter intercepts. |
| 1322 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1323 | typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info); |
| 1324 | |
| 1325 | |
| 1326 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1327 | * Access control specifications. |
| 1328 | * |
| 1329 | * Some accessors should be accessible across contexts. These |
| 1330 | * accessors have an explicit access control parameter which specifies |
| 1331 | * the kind of cross-context access that should be allowed. |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1332 | * |
| 1333 | * Additionally, for security, accessors can prohibit overwriting by |
| 1334 | * accessors defined in JavaScript. For objects that have such |
| 1335 | * accessors either locally or in their prototype chain it is not |
| 1336 | * possible to overwrite the accessor by using __defineGetter__ or |
| 1337 | * __defineSetter__ from JavaScript code. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1338 | */ |
| 1339 | enum AccessControl { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1340 | DEFAULT = 0, |
| 1341 | ALL_CAN_READ = 1, |
| 1342 | ALL_CAN_WRITE = 1 << 1, |
| 1343 | PROHIBITS_OVERWRITING = 1 << 2 |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1344 | }; |
| 1345 | |
| 1346 | |
| 1347 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1348 | * Access type specification. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1349 | */ |
| 1350 | enum AccessType { |
| 1351 | ACCESS_GET, |
| 1352 | ACCESS_SET, |
| 1353 | ACCESS_HAS, |
| 1354 | ACCESS_DELETE, |
| 1355 | ACCESS_KEYS |
| 1356 | }; |
| 1357 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1358 | |
| 1359 | /** |
| 1360 | * Returns true if cross-context access should be allowed to the named |
| 1361 | * property with the given key on the global object. |
| 1362 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1363 | typedef bool (*NamedSecurityCallback)(Local<Object> global, |
| 1364 | Local<Value> key, |
| 1365 | AccessType type, |
| 1366 | Local<Value> data); |
| 1367 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1368 | |
| 1369 | /** |
| 1370 | * Returns true if cross-context access should be allowed to the indexed |
| 1371 | * property with the given index on the global object. |
| 1372 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1373 | typedef bool (*IndexedSecurityCallback)(Local<Object> global, |
| 1374 | uint32_t index, |
| 1375 | AccessType type, |
| 1376 | Local<Value> data); |
| 1377 | |
| 1378 | |
| 1379 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1380 | * A FunctionTemplate is used to create functions at runtime. There |
| 1381 | * can only be one function created from a FunctionTemplate in a |
| 1382 | * context. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1383 | * |
| 1384 | * A FunctionTemplate can have properties, these properties are added to the |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1385 | * function object when it is created. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1386 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1387 | * A FunctionTemplate has a corresponding instance template which is |
| 1388 | * used to create object instances when the function is used as a |
| 1389 | * constructor. Properties added to the instance template are added to |
| 1390 | * each object instance. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1391 | * |
| 1392 | * A FunctionTemplate can have a prototype template. The prototype template |
| 1393 | * is used to create the prototype object of the function. |
| 1394 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1395 | * The following example shows how to use a FunctionTemplate: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1396 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1397 | * \code |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1398 | * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 1399 | * t->Set("func_property", v8::Number::New(1)); |
| 1400 | * |
| 1401 | * v8::Local<v8::Template> proto_t = t->PrototypeTemplate(); |
| 1402 | * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback)); |
| 1403 | * proto_t->Set("proto_const", v8::Number::New(2)); |
| 1404 | * |
| 1405 | * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate(); |
| 1406 | * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback); |
| 1407 | * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...); |
| 1408 | * instance_t->Set("instance_property", Number::New(3)); |
| 1409 | * |
| 1410 | * v8::Local<v8::Function> function = t->GetFunction(); |
| 1411 | * v8::Local<v8::Object> instance = function->NewInstance(); |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1412 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1413 | * |
| 1414 | * 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] | 1415 | * and "instance" for the instance object created above. The function |
| 1416 | * and the instance will have the following properties: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1417 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1418 | * \code |
| 1419 | * func_property in function == true; |
| 1420 | * function.func_property == 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1421 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1422 | * function.prototype.proto_method() invokes 'InvokeCallback' |
| 1423 | * function.prototype.proto_const == 2; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1424 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1425 | * instance instanceof function == true; |
| 1426 | * instance.instance_accessor calls 'InstanceAccessorCallback' |
| 1427 | * instance.instance_property == 3; |
| 1428 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1429 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1430 | * A FunctionTemplate can inherit from another one by calling the |
| 1431 | * FunctionTemplate::Inherit method. The following graph illustrates |
| 1432 | * the semantics of inheritance: |
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 | * \code |
| 1435 | * FunctionTemplate Parent -> Parent() . prototype -> { } |
| 1436 | * ^ ^ |
| 1437 | * | Inherit(Parent) | .__proto__ |
| 1438 | * | | |
| 1439 | * FunctionTemplate Child -> Child() . prototype -> { } |
| 1440 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1441 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1442 | * A FunctionTemplate 'Child' inherits from 'Parent', the prototype |
| 1443 | * object of the Child() function has __proto__ pointing to the |
| 1444 | * Parent() function's prototype object. An instance of the Child |
| 1445 | * function has all properties on Parent's instance templates. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1446 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1447 | * Let Parent be the FunctionTemplate initialized in the previous |
| 1448 | * section and create a Child FunctionTemplate by: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1449 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1450 | * \code |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1451 | * Local<FunctionTemplate> parent = t; |
| 1452 | * Local<FunctionTemplate> child = FunctionTemplate::New(); |
| 1453 | * child->Inherit(parent); |
| 1454 | * |
| 1455 | * Local<Function> child_function = child->GetFunction(); |
| 1456 | * Local<Object> child_instance = child_function->NewInstance(); |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1457 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1458 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1459 | * The Child function and Child instance will have the following |
| 1460 | * properties: |
| 1461 | * |
| 1462 | * \code |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1463 | * child_func.prototype.__proto__ == function.prototype; |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1464 | * child_instance.instance_accessor calls 'InstanceAccessorCallback' |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1465 | * child_instance.instance_property == 3; |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1466 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1467 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1468 | class EXPORT FunctionTemplate : public Template { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1469 | public: |
| 1470 | /** Creates a function template.*/ |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1471 | static Local<FunctionTemplate> New( |
| 1472 | InvocationCallback callback = 0, |
| 1473 | Handle<Value> data = Handle<Value>(), |
| 1474 | Handle<Signature> signature = Handle<Signature>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1475 | /** Returns the unique function instance in the current execution context.*/ |
| 1476 | Local<Function> GetFunction(); |
| 1477 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1478 | /** |
| 1479 | * Set the call-handler callback for a FunctionTemplate. This |
| 1480 | * callback is called whenever the function created from this |
| 1481 | * FunctionTemplate is called. |
| 1482 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1483 | void SetCallHandler(InvocationCallback callback, |
| 1484 | Handle<Value> data = Handle<Value>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1485 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1486 | /** Get the InstanceTemplate. */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1487 | Local<ObjectTemplate> InstanceTemplate(); |
| 1488 | |
| 1489 | /** Causes the function template to inherit from a parent function template.*/ |
| 1490 | void Inherit(Handle<FunctionTemplate> parent); |
| 1491 | |
| 1492 | /** |
| 1493 | * A PrototypeTemplate is the template used to create the prototype object |
| 1494 | * of the function created by this template. |
| 1495 | */ |
| 1496 | Local<ObjectTemplate> PrototypeTemplate(); |
| 1497 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1498 | |
| 1499 | /** |
| 1500 | * Set the class name of the FunctionTemplate. This is used for |
| 1501 | * printing objects created with the function created from the |
| 1502 | * FunctionTemplate as its constructor. |
| 1503 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1504 | void SetClassName(Handle<String> name); |
| 1505 | |
| 1506 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1507 | * Determines whether the __proto__ accessor ignores instances of |
| 1508 | * the function template. If instances of the function template are |
| 1509 | * ignored, __proto__ skips all instances and instead returns the |
| 1510 | * next object in the prototype chain. |
| 1511 | * |
| 1512 | * Call with a value of true to make the __proto__ accessor ignore |
| 1513 | * instances of the function template. Call with a value of false |
| 1514 | * to make the __proto__ accessor not ignore instances of the |
| 1515 | * function template. By default, instances of a function template |
| 1516 | * are not ignored. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1517 | */ |
| 1518 | void SetHiddenPrototype(bool value); |
| 1519 | |
| 1520 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1521 | * Returns true if the given object is an instance of this function |
| 1522 | * template. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1523 | */ |
| 1524 | bool HasInstance(Handle<Value> object); |
| 1525 | |
| 1526 | private: |
| 1527 | FunctionTemplate(); |
| 1528 | void AddInstancePropertyAccessor(Handle<String> name, |
| 1529 | AccessorGetter getter, |
| 1530 | AccessorSetter setter, |
| 1531 | Handle<Value> data, |
| 1532 | AccessControl settings, |
| 1533 | PropertyAttribute attributes); |
| 1534 | void SetNamedInstancePropertyHandler(NamedPropertyGetter getter, |
| 1535 | NamedPropertySetter setter, |
| 1536 | NamedPropertyQuery query, |
| 1537 | NamedPropertyDeleter remover, |
| 1538 | NamedPropertyEnumerator enumerator, |
| 1539 | Handle<Value> data); |
| 1540 | void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter, |
| 1541 | IndexedPropertySetter setter, |
| 1542 | IndexedPropertyQuery query, |
| 1543 | IndexedPropertyDeleter remover, |
| 1544 | IndexedPropertyEnumerator enumerator, |
| 1545 | Handle<Value> data); |
| 1546 | void SetInstanceCallAsFunctionHandler(InvocationCallback callback, |
| 1547 | Handle<Value> data); |
| 1548 | |
| 1549 | friend class Context; |
| 1550 | friend class ObjectTemplate; |
| 1551 | }; |
| 1552 | |
| 1553 | |
| 1554 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1555 | * An ObjectTemplate is used to create objects at runtime. |
| 1556 | * |
| 1557 | * Properties added to an ObjectTemplate are added to each object |
| 1558 | * created from the ObjectTemplate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1559 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1560 | class EXPORT ObjectTemplate : public Template { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1561 | public: |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1562 | /** Creates an ObjectTemplate. */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1563 | static Local<ObjectTemplate> New(); |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1564 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1565 | /** Creates a new instance of this template.*/ |
| 1566 | Local<Object> NewInstance(); |
| 1567 | |
| 1568 | /** |
| 1569 | * Sets an accessor on the object template. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1570 | * |
| 1571 | * Whenever the property with the given name is accessed on objects |
| 1572 | * created from this ObjectTemplate the getter and setter callbacks |
| 1573 | * are called instead of getting and setting the property directly |
| 1574 | * on the JavaScript object. |
| 1575 | * |
| 1576 | * \param name The name of the property for which an accessor is added. |
| 1577 | * \param getter The callback to invoke when getting the property. |
| 1578 | * \param setter The callback to invoke when setting the property. |
| 1579 | * \param data A piece of data that will be passed to the getter and setter |
| 1580 | * callbacks whenever they are invoked. |
| 1581 | * \param settings Access control settings for the accessor. This is a bit |
| 1582 | * field consisting of one of more of |
| 1583 | * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2. |
| 1584 | * The default is to not allow cross-context access. |
| 1585 | * ALL_CAN_READ means that all cross-context reads are allowed. |
| 1586 | * ALL_CAN_WRITE means that all cross-context writes are allowed. |
| 1587 | * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all |
| 1588 | * cross-context access. |
| 1589 | * \param attribute The attributes of the property for which an accessor |
| 1590 | * is added. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1591 | */ |
| 1592 | void SetAccessor(Handle<String> name, |
| 1593 | AccessorGetter getter, |
| 1594 | AccessorSetter setter = 0, |
| 1595 | Handle<Value> data = Handle<Value>(), |
| 1596 | AccessControl settings = DEFAULT, |
| 1597 | PropertyAttribute attribute = None); |
| 1598 | |
| 1599 | /** |
| 1600 | * Sets a named property handler on the object template. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1601 | * |
| 1602 | * Whenever a named property is accessed on objects created from |
| 1603 | * this object template, the provided callback is invoked instead of |
| 1604 | * accessing the property directly on the JavaScript object. |
| 1605 | * |
| 1606 | * \param getter The callback to invoke when getting a property. |
| 1607 | * \param setter The callback to invoke when setting a property. |
| 1608 | * \param query The callback to invoke to check is an object has a property. |
| 1609 | * \param deleter The callback to invoke when deleting a property. |
| 1610 | * \param enumerator The callback to invoke to enumerate all the named |
| 1611 | * properties of an object. |
| 1612 | * \param data A piece of data that will be passed to the callbacks |
| 1613 | * whenever they are invoked. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1614 | */ |
| 1615 | void SetNamedPropertyHandler(NamedPropertyGetter getter, |
| 1616 | NamedPropertySetter setter = 0, |
| 1617 | NamedPropertyQuery query = 0, |
| 1618 | NamedPropertyDeleter deleter = 0, |
| 1619 | NamedPropertyEnumerator enumerator = 0, |
| 1620 | Handle<Value> data = Handle<Value>()); |
| 1621 | |
| 1622 | /** |
| 1623 | * Sets an indexed property handler on the object template. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1624 | * |
| 1625 | * Whenever an indexed property is accessed on objects created from |
| 1626 | * this object template, the provided callback is invoked instead of |
| 1627 | * accessing the property directly on the JavaScript object. |
| 1628 | * |
| 1629 | * \param getter The callback to invoke when getting a property. |
| 1630 | * \param setter The callback to invoke when setting a property. |
| 1631 | * \param query The callback to invoke to check is an object has a property. |
| 1632 | * \param deleter The callback to invoke when deleting a property. |
| 1633 | * \param enumerator The callback to invoke to enumerate all the indexed |
| 1634 | * properties of an object. |
| 1635 | * \param data A piece of data that will be passed to the callbacks |
| 1636 | * whenever they are invoked. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1637 | */ |
| 1638 | void SetIndexedPropertyHandler(IndexedPropertyGetter getter, |
| 1639 | IndexedPropertySetter setter = 0, |
| 1640 | IndexedPropertyQuery query = 0, |
| 1641 | IndexedPropertyDeleter deleter = 0, |
| 1642 | IndexedPropertyEnumerator enumerator = 0, |
| 1643 | Handle<Value> data = Handle<Value>()); |
| 1644 | /** |
| 1645 | * Sets the callback to be used when calling instances created from |
| 1646 | * this template as a function. If no callback is set, instances |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1647 | * behave like normal JavaScript objects that cannot be called as a |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1648 | * function. |
| 1649 | */ |
| 1650 | void SetCallAsFunctionHandler(InvocationCallback callback, |
| 1651 | Handle<Value> data = Handle<Value>()); |
| 1652 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1653 | /** |
| 1654 | * Mark object instances of the template as undetectable. |
| 1655 | * |
| 1656 | * In many ways, undetectable objects behave as though they are not |
| 1657 | * there. They behave like 'undefined' in conditionals and when |
| 1658 | * printed. However, properties can be accessed and called as on |
| 1659 | * normal objects. |
| 1660 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1661 | void MarkAsUndetectable(); |
| 1662 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1663 | /** |
| 1664 | * Sets access check callbacks on the object template. |
| 1665 | * |
| 1666 | * When accessing properties on instances of this object template, |
| 1667 | * the access check callback will be called to determine whether or |
| 1668 | * not to allow cross-context access to the properties. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1669 | * The last parameter specifies whether access checks are turned |
| 1670 | * on by default on instances. If access checks are off by default, |
| 1671 | * they can be turned on on individual instances by calling |
| 1672 | * Object::TurnOnAccessCheck(). |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1673 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1674 | void SetAccessCheckCallbacks(NamedSecurityCallback named_handler, |
| 1675 | IndexedSecurityCallback indexed_handler, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1676 | Handle<Value> data = Handle<Value>(), |
| 1677 | bool turned_on_by_default = true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1678 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1679 | /** |
| 1680 | * Gets the number of internal fields for objects generated from |
| 1681 | * this template. |
| 1682 | */ |
| 1683 | int InternalFieldCount(); |
| 1684 | |
| 1685 | /** |
| 1686 | * Sets the number of internal fields for objects generated from |
| 1687 | * this template. |
| 1688 | */ |
| 1689 | void SetInternalFieldCount(int value); |
| 1690 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1691 | private: |
| 1692 | ObjectTemplate(); |
| 1693 | static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor); |
| 1694 | friend class FunctionTemplate; |
| 1695 | }; |
| 1696 | |
| 1697 | |
| 1698 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1699 | * A Signature specifies which receivers and arguments a function can |
| 1700 | * legally be called with. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1701 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1702 | class EXPORT Signature : public Data { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1703 | public: |
| 1704 | static Local<Signature> New(Handle<FunctionTemplate> receiver = |
| 1705 | Handle<FunctionTemplate>(), |
| 1706 | int argc = 0, |
| 1707 | Handle<FunctionTemplate> argv[] = 0); |
| 1708 | private: |
| 1709 | Signature(); |
| 1710 | }; |
| 1711 | |
| 1712 | |
| 1713 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1714 | * A utility for determining the type of objects based on the template |
| 1715 | * they were constructed from. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1716 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1717 | class EXPORT TypeSwitch : public Data { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1718 | public: |
| 1719 | static Local<TypeSwitch> New(Handle<FunctionTemplate> type); |
| 1720 | static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]); |
| 1721 | int match(Handle<Value> value); |
| 1722 | private: |
| 1723 | TypeSwitch(); |
| 1724 | }; |
| 1725 | |
| 1726 | |
| 1727 | // --- E x t e n s i o n s --- |
| 1728 | |
| 1729 | |
| 1730 | /** |
| 1731 | * Ignore |
| 1732 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1733 | class EXPORT Extension { // NOLINT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1734 | public: |
| 1735 | Extension(const char* name, |
| 1736 | const char* source = 0, |
| 1737 | int dep_count = 0, |
| 1738 | const char** deps = 0); |
| 1739 | virtual ~Extension() { } |
| 1740 | virtual v8::Handle<v8::FunctionTemplate> |
| 1741 | GetNativeFunction(v8::Handle<v8::String> name) { |
| 1742 | return v8::Handle<v8::FunctionTemplate>(); |
| 1743 | } |
| 1744 | |
| 1745 | const char* name() { return name_; } |
| 1746 | const char* source() { return source_; } |
| 1747 | int dependency_count() { return dep_count_; } |
| 1748 | const char** dependencies() { return deps_; } |
| 1749 | void set_auto_enable(bool value) { auto_enable_ = value; } |
| 1750 | bool auto_enable() { return auto_enable_; } |
| 1751 | |
| 1752 | private: |
| 1753 | const char* name_; |
| 1754 | const char* source_; |
| 1755 | int dep_count_; |
| 1756 | const char** deps_; |
| 1757 | bool auto_enable_; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1758 | |
| 1759 | // Disallow copying and assigning. |
| 1760 | Extension(const Extension&); |
| 1761 | void operator=(const Extension&); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1762 | }; |
| 1763 | |
| 1764 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1765 | void EXPORT RegisterExtension(Extension* extension); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1766 | |
| 1767 | |
| 1768 | /** |
| 1769 | * Ignore |
| 1770 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1771 | class EXPORT DeclareExtension { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1772 | public: |
| 1773 | inline DeclareExtension(Extension* extension) { |
| 1774 | RegisterExtension(extension); |
| 1775 | } |
| 1776 | }; |
| 1777 | |
| 1778 | |
| 1779 | // --- S t a t i c s --- |
| 1780 | |
| 1781 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1782 | Handle<Primitive> EXPORT Undefined(); |
| 1783 | Handle<Primitive> EXPORT Null(); |
| 1784 | Handle<Boolean> EXPORT True(); |
| 1785 | Handle<Boolean> EXPORT False(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1786 | |
| 1787 | |
| 1788 | /** |
| 1789 | * A set of constraints that specifies the limits of the runtime's |
| 1790 | * memory use. |
| 1791 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1792 | class EXPORT ResourceConstraints { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1793 | public: |
| 1794 | ResourceConstraints(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1795 | int max_young_space_size() const { return max_young_space_size_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1796 | 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] | 1797 | int max_old_space_size() const { return max_old_space_size_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1798 | 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] | 1799 | uint32_t* stack_limit() const { return stack_limit_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1800 | void set_stack_limit(uint32_t* value) { stack_limit_ = value; } |
| 1801 | private: |
| 1802 | int max_young_space_size_; |
| 1803 | int max_old_space_size_; |
| 1804 | uint32_t* stack_limit_; |
| 1805 | }; |
| 1806 | |
| 1807 | |
| 1808 | bool SetResourceConstraints(ResourceConstraints* constraints); |
| 1809 | |
| 1810 | |
| 1811 | // --- E x c e p t i o n s --- |
| 1812 | |
| 1813 | |
| 1814 | typedef void (*FatalErrorCallback)(const char* location, const char* message); |
| 1815 | |
| 1816 | |
| 1817 | typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data); |
| 1818 | |
| 1819 | |
| 1820 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1821 | * Schedules an exception to be thrown when returning to JavaScript. When an |
| 1822 | * exception has been scheduled it is illegal to invoke any JavaScript |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1823 | * operation; the caller must return immediately and only after the exception |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1824 | * has been handled does it become legal to invoke JavaScript operations. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1825 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1826 | Handle<Value> EXPORT ThrowException(Handle<Value> exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1827 | |
| 1828 | /** |
| 1829 | * Create new error objects by calling the corresponding error object |
| 1830 | * constructor with the message. |
| 1831 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1832 | class EXPORT Exception { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1833 | public: |
| 1834 | static Local<Value> RangeError(Handle<String> message); |
| 1835 | static Local<Value> ReferenceError(Handle<String> message); |
| 1836 | static Local<Value> SyntaxError(Handle<String> message); |
| 1837 | static Local<Value> TypeError(Handle<String> message); |
| 1838 | static Local<Value> Error(Handle<String> message); |
| 1839 | }; |
| 1840 | |
| 1841 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1842 | // --- 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] | 1843 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1844 | typedef int* (*CounterLookupCallback)(const char* name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1845 | |
| 1846 | // --- 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 --- |
| 1847 | typedef void (*FailedAccessCheckCallback)(Local<Object> target, |
| 1848 | AccessType type, |
| 1849 | Local<Value> data); |
| 1850 | |
| 1851 | // --- 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 |
| 1852 | |
| 1853 | /** |
| 1854 | * Applications can register a callback function which is called |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1855 | * before and after a major garbage collection. Allocations are not |
| 1856 | * allowed in the callback function, you therefore cannot manipulate |
| 1857 | * objects (set or delete properties for example) since it is possible |
| 1858 | * such operations will result in the allocation of objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1859 | */ |
| 1860 | typedef void (*GCCallback)(); |
| 1861 | |
| 1862 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1863 | // --- 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] | 1864 | |
| 1865 | /** |
| 1866 | * Applications must provide a callback function which is called to generate |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1867 | * a context if a context was not deserialized from the snapshot. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1868 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1869 | typedef Persistent<Context> (*ContextGenerator)(); |
| 1870 | |
| 1871 | |
| 1872 | /** |
| 1873 | * Container class for static utility functions. |
| 1874 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1875 | class EXPORT V8 { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1876 | public: |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1877 | /** Set the callback to invoke in case of fatal errors. */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1878 | static void SetFatalErrorHandler(FatalErrorCallback that); |
| 1879 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1880 | /** |
| 1881 | * Ignore out-of-memory exceptions. |
| 1882 | * |
| 1883 | * V8 running out of memory is treated as a fatal error by default. |
| 1884 | * This means that the fatal error handler is called and that V8 is |
| 1885 | * terminated. |
| 1886 | * |
| 1887 | * IgnoreOutOfMemoryException can be used to not treat a |
| 1888 | * out-of-memory situation as a fatal error. This way, the contexts |
| 1889 | * that did not cause the out of memory problem might be able to |
| 1890 | * continue execution. |
| 1891 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1892 | static void IgnoreOutOfMemoryException(); |
| 1893 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1894 | /** |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1895 | * 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] | 1896 | * fatal errors such as out-of-memory situations. |
| 1897 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1898 | static bool IsDead(); |
| 1899 | |
| 1900 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1901 | * Adds a message listener. |
| 1902 | * |
| 1903 | * The same message listener can be added more than once and it that |
| 1904 | * case it will be called more than once for each message. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1905 | */ |
| 1906 | static bool AddMessageListener(MessageCallback that, |
| 1907 | Handle<Value> data = Handle<Value>()); |
| 1908 | |
| 1909 | /** |
| 1910 | * Remove all message listeners from the specified callback function. |
| 1911 | */ |
| 1912 | static void RemoveMessageListeners(MessageCallback that); |
| 1913 | |
| 1914 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1915 | * Sets V8 flags from a string. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1916 | */ |
| 1917 | static void SetFlagsFromString(const char* str, int length); |
| 1918 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1919 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1920 | * Sets V8 flags from the command line. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1921 | */ |
| 1922 | static void SetFlagsFromCommandLine(int* argc, |
| 1923 | char** argv, |
| 1924 | bool remove_flags); |
| 1925 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1926 | /** Get the version string. */ |
| 1927 | static const char* GetVersion(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1928 | |
| 1929 | /** |
| 1930 | * Enables the host application to provide a mechanism for recording |
| 1931 | * statistics counters. |
| 1932 | */ |
| 1933 | static void SetCounterFunction(CounterLookupCallback); |
| 1934 | |
| 1935 | /** |
| 1936 | * Enables the computation of a sliding window of states. The sliding |
| 1937 | * window information is recorded in statistics counters. |
| 1938 | */ |
| 1939 | static void EnableSlidingStateWindow(); |
| 1940 | |
| 1941 | /** Callback function for reporting failed access checks.*/ |
| 1942 | static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback); |
| 1943 | |
| 1944 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1945 | * Enables the host application to receive a notification before a |
| 1946 | * major garbage colletion. Allocations are not allowed in the |
| 1947 | * callback function, you therefore cannot manipulate objects (set |
| 1948 | * or delete properties for example) since it is possible such |
| 1949 | * operations will result in the allocation of objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1950 | */ |
| 1951 | static void SetGlobalGCPrologueCallback(GCCallback); |
| 1952 | |
| 1953 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1954 | * Enables the host application to receive a notification after a |
| 1955 | * major garbage collection. Allocations are not allowed in the |
| 1956 | * callback function, you therefore cannot manipulate objects (set |
| 1957 | * or delete properties for example) since it is possible such |
| 1958 | * operations will result in the allocation of objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1959 | */ |
| 1960 | static void SetGlobalGCEpilogueCallback(GCCallback); |
| 1961 | |
| 1962 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1963 | * Allows the host application to group objects together. If one |
| 1964 | * object in the group is alive, all objects in the group are alive. |
| 1965 | * After each garbage collection, object groups are removed. It is |
| 1966 | * intended to be used in the before-garbage-collection callback |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1967 | * function, for instance to simulate DOM tree connections among JS |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1968 | * wrapper objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1969 | */ |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1970 | static void AddObjectGroup(Persistent<Value>* objects, size_t length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1971 | |
| 1972 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1973 | * Initializes from snapshot if possible. Otherwise, attempts to |
| 1974 | * initialize from scratch. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1975 | */ |
| 1976 | static bool Initialize(); |
| 1977 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1978 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1979 | * Adjusts the amount of registered external memory. Used to give |
| 1980 | * V8 an indication of the amount of externally allocated memory |
| 1981 | * that is kept alive by JavaScript objects. V8 uses this to decide |
| 1982 | * when to perform global garbage collections. Registering |
| 1983 | * externally allocated memory will trigger global garbage |
| 1984 | * collections more often than otherwise in an attempt to garbage |
| 1985 | * collect the JavaScript objects keeping the externally allocated |
| 1986 | * memory alive. |
| 1987 | * |
| 1988 | * \param change_in_bytes the change in externally allocated memory |
| 1989 | * that is kept alive by JavaScript objects. |
| 1990 | * \returns the adjusted value. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1991 | */ |
| 1992 | static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes); |
| 1993 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1994 | /** |
| 1995 | * Suspends recording of tick samples in the profiler. |
| 1996 | * When the V8 profiling mode is enabled (usually via command line |
| 1997 | * switches) this function suspends recording of tick samples. |
| 1998 | * Profiling ticks are discarded until ResumeProfiler() is called. |
| 1999 | * |
| 2000 | * See also the --prof and --prof_auto command line switches to |
| 2001 | * enable V8 profiling. |
| 2002 | */ |
| 2003 | static void PauseProfiler(); |
| 2004 | |
| 2005 | /** |
| 2006 | * Resumes recording of tick samples in the profiler. |
| 2007 | * See also PauseProfiler(). |
| 2008 | */ |
| 2009 | static void ResumeProfiler(); |
| 2010 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2011 | private: |
| 2012 | V8(); |
| 2013 | |
| 2014 | static void** GlobalizeReference(void** handle); |
| 2015 | static void DisposeGlobal(void** global_handle); |
| 2016 | static void MakeWeak(void** global_handle, void* data, WeakReferenceCallback); |
| 2017 | static void ClearWeak(void** global_handle); |
| 2018 | static bool IsGlobalNearDeath(void** global_handle); |
| 2019 | static bool IsGlobalWeak(void** global_handle); |
| 2020 | |
| 2021 | template <class T> friend class Handle; |
| 2022 | template <class T> friend class Local; |
| 2023 | template <class T> friend class Persistent; |
| 2024 | friend class Context; |
| 2025 | }; |
| 2026 | |
| 2027 | |
| 2028 | /** |
| 2029 | * An external exception handler. |
| 2030 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2031 | class EXPORT TryCatch { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2032 | public: |
| 2033 | |
| 2034 | /** |
| 2035 | * Creates a new try/catch block and registers it with v8. |
| 2036 | */ |
| 2037 | TryCatch(); |
| 2038 | |
| 2039 | /** |
| 2040 | * Unregisters and deletes this try/catch block. |
| 2041 | */ |
| 2042 | ~TryCatch(); |
| 2043 | |
| 2044 | /** |
| 2045 | * Returns true if an exception has been caught by this try/catch block. |
| 2046 | */ |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2047 | bool HasCaught() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2048 | |
| 2049 | /** |
| 2050 | * Returns the exception caught by this try/catch block. If no exception has |
| 2051 | * been caught an empty handle is returned. |
| 2052 | * |
| 2053 | * The returned handle is valid until this TryCatch block has been destroyed. |
| 2054 | */ |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2055 | Local<Value> Exception() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2056 | |
| 2057 | /** |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2058 | * Returns the message associated with this exception. If there is |
| 2059 | * no message associated an empty handle is returned. |
| 2060 | * |
| 2061 | * The returned handle is valid until this TryCatch block has been |
| 2062 | * destroyed. |
| 2063 | */ |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2064 | Local<v8::Message> Message() const; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2065 | |
| 2066 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2067 | * Clears any exceptions that may have been caught by this try/catch block. |
| 2068 | * After this method has been called, HasCaught() will return false. |
| 2069 | * |
| 2070 | * It is not necessary to clear a try/catch block before using it again; if |
| 2071 | * another exception is thrown the previously caught exception will just be |
| 2072 | * overwritten. However, it is often a good idea since it makes it easier |
| 2073 | * to determine which operation threw a given exception. |
| 2074 | */ |
| 2075 | void Reset(); |
| 2076 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2077 | /** |
| 2078 | * Set verbosity of the external exception handler. |
| 2079 | * |
| 2080 | * By default, exceptions that are caught by an external exception |
| 2081 | * handler are not reported. Call SetVerbose with true on an |
| 2082 | * external exception handler to have exceptions caught by the |
| 2083 | * handler reported as if they were not caught. |
| 2084 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2085 | void SetVerbose(bool value); |
| 2086 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2087 | /** |
| 2088 | * Set whether or not this TryCatch should capture a Message object |
| 2089 | * which holds source information about where the exception |
| 2090 | * occurred. True by default. |
| 2091 | */ |
| 2092 | void SetCaptureMessage(bool value); |
| 2093 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2094 | public: |
| 2095 | TryCatch* next_; |
| 2096 | void* exception_; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2097 | void* message_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2098 | bool is_verbose_; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2099 | bool capture_message_; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2100 | void* js_handler_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2101 | }; |
| 2102 | |
| 2103 | |
| 2104 | // --- C o n t e x t --- |
| 2105 | |
| 2106 | |
| 2107 | /** |
| 2108 | * Ignore |
| 2109 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2110 | class EXPORT ExtensionConfiguration { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2111 | public: |
| 2112 | ExtensionConfiguration(int name_count, const char* names[]) |
| 2113 | : name_count_(name_count), names_(names) { } |
| 2114 | private: |
| 2115 | friend class ImplementationUtilities; |
| 2116 | int name_count_; |
| 2117 | const char** names_; |
| 2118 | }; |
| 2119 | |
| 2120 | |
| 2121 | /** |
| 2122 | * A sandboxed execution context with its own set of built-in objects |
| 2123 | * and functions. |
| 2124 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2125 | class EXPORT Context { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2126 | public: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2127 | /** Returns the global object of the context. */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2128 | Local<Object> Global(); |
| 2129 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2130 | /** |
| 2131 | * Detaches the global object from its context before |
| 2132 | * the global object can be reused to create a new context. |
| 2133 | */ |
| 2134 | void DetachGlobal(); |
| 2135 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2136 | /** Creates a new context. */ |
| 2137 | static Persistent<Context> New( |
| 2138 | ExtensionConfiguration* extensions = 0, |
| 2139 | Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(), |
| 2140 | Handle<Value> global_object = Handle<Value>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2141 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 2142 | /** Returns the last entered context. */ |
| 2143 | static Local<Context> GetEntered(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2144 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 2145 | /** Returns the context that is on the top of the stack. */ |
| 2146 | static Local<Context> GetCurrent(); |
| 2147 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2148 | /** |
| 2149 | * Sets the security token for the context. To access an object in |
| 2150 | * another context, the security tokens must match. |
| 2151 | */ |
| 2152 | void SetSecurityToken(Handle<Value> token); |
| 2153 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2154 | /** Restores the security token to the default value. */ |
| 2155 | void UseDefaultSecurityToken(); |
| 2156 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2157 | /** Returns the security token of this context.*/ |
| 2158 | Handle<Value> GetSecurityToken(); |
| 2159 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2160 | /** |
| 2161 | * Enter this context. After entering a context, all code compiled |
| 2162 | * and run is compiled and run in this context. If another context |
| 2163 | * is already entered, this old context is saved so it can be |
| 2164 | * restored when the new context is exited. |
| 2165 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2166 | void Enter(); |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2167 | |
| 2168 | /** |
| 2169 | * Exit this context. Exiting the current context restores the |
| 2170 | * context that was in place when entering the current context. |
| 2171 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2172 | void Exit(); |
| 2173 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2174 | /** 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] | 2175 | bool HasOutOfMemoryException(); |
| 2176 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2177 | /** Returns true if V8 has a current context. */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2178 | static bool InContext(); |
| 2179 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2180 | /** |
| 2181 | * Stack-allocated class which sets the execution context for all |
| 2182 | * operations executed within a local scope. |
| 2183 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2184 | class EXPORT Scope { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2185 | public: |
| 2186 | inline Scope(Handle<Context> context) : context_(context) { |
| 2187 | context_->Enter(); |
| 2188 | } |
| 2189 | inline ~Scope() { context_->Exit(); } |
| 2190 | private: |
| 2191 | Handle<Context> context_; |
| 2192 | }; |
| 2193 | |
| 2194 | private: |
| 2195 | friend class Value; |
| 2196 | friend class Script; |
| 2197 | friend class Object; |
| 2198 | friend class Function; |
| 2199 | }; |
| 2200 | |
| 2201 | |
| 2202 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2203 | * Multiple threads in V8 are allowed, but only one thread at a time |
| 2204 | * is allowed to use V8. The definition of 'using V8' includes |
| 2205 | * accessing handles or holding onto object pointers obtained from V8 |
| 2206 | * handles. It is up to the user of V8 to ensure (perhaps with |
| 2207 | * locking) that this constraint is not violated. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2208 | * |
| 2209 | * If you wish to start using V8 in a thread you can do this by constructing |
| 2210 | * a v8::Locker object. After the code using V8 has completed for the |
| 2211 | * current thread you can call the destructor. This can be combined |
| 2212 | * with C++ scope-based construction as follows: |
| 2213 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2214 | * \code |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2215 | * ... |
| 2216 | * { |
| 2217 | * v8::Locker locker; |
| 2218 | * ... |
| 2219 | * // Code using V8 goes here. |
| 2220 | * ... |
| 2221 | * } // Destructor called here |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2222 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2223 | * |
| 2224 | * If you wish to stop using V8 in a thread A you can do this by either |
| 2225 | * by destroying the v8::Locker object as above or by constructing a |
| 2226 | * v8::Unlocker object: |
| 2227 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2228 | * \code |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2229 | * { |
| 2230 | * v8::Unlocker unlocker; |
| 2231 | * ... |
| 2232 | * // Code not using V8 goes here while V8 can run in another thread. |
| 2233 | * ... |
| 2234 | * } // Destructor called here. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2235 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2236 | * |
| 2237 | * The Unlocker object is intended for use in a long-running callback |
| 2238 | * from V8, where you want to release the V8 lock for other threads to |
| 2239 | * use. |
| 2240 | * |
| 2241 | * The v8::Locker is a recursive lock. That is, you can lock more than |
| 2242 | * once in a given thread. This can be useful if you have code that can |
| 2243 | * be called either from code that holds the lock or from code that does |
| 2244 | * not. The Unlocker is not recursive so you can not have several |
| 2245 | * Unlockers on the stack at once, and you can not use an Unlocker in a |
| 2246 | * thread that is not inside a Locker's scope. |
| 2247 | * |
| 2248 | * An unlocker will unlock several lockers if it has to and reinstate |
| 2249 | * the correct depth of locking on its destruction. eg.: |
| 2250 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2251 | * \code |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2252 | * // V8 not locked. |
| 2253 | * { |
| 2254 | * v8::Locker locker; |
| 2255 | * // V8 locked. |
| 2256 | * { |
| 2257 | * v8::Locker another_locker; |
| 2258 | * // V8 still locked (2 levels). |
| 2259 | * { |
| 2260 | * v8::Unlocker unlocker; |
| 2261 | * // V8 not locked. |
| 2262 | * } |
| 2263 | * // V8 locked again (2 levels). |
| 2264 | * } |
| 2265 | * // V8 still locked (1 level). |
| 2266 | * } |
| 2267 | * // V8 Now no longer locked. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2268 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2269 | */ |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2270 | class EXPORT Unlocker { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2271 | public: |
| 2272 | Unlocker(); |
| 2273 | ~Unlocker(); |
| 2274 | }; |
| 2275 | |
| 2276 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2277 | class EXPORT Locker { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2278 | public: |
| 2279 | Locker(); |
| 2280 | ~Locker(); |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2281 | |
| 2282 | /** |
| 2283 | * Start preemption. |
| 2284 | * |
| 2285 | * When preemption is started, a timer is fired every n milli seconds |
| 2286 | * that will switch between multiple threads that are in contention |
| 2287 | * for the V8 lock. |
| 2288 | */ |
| 2289 | static void StartPreemption(int every_n_ms); |
| 2290 | |
| 2291 | /** |
| 2292 | * Stop preemption. |
| 2293 | */ |
| 2294 | static void StopPreemption(); |
| 2295 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2296 | /** |
| 2297 | * Returns whether or not the locker is locked by the current thread. |
| 2298 | */ |
| 2299 | static bool IsLocked(); |
| 2300 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 2301 | /** |
| 2302 | * Returns whether v8::Locker is being used by this V8 instance. |
| 2303 | */ |
| 2304 | static bool IsActive() { return active_; } |
| 2305 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2306 | private: |
| 2307 | bool has_lock_; |
| 2308 | bool top_level_; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2309 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 2310 | static bool active_; |
| 2311 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2312 | // Disallow copying and assigning. |
| 2313 | Locker(const Locker&); |
| 2314 | void operator=(const Locker&); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2315 | }; |
| 2316 | |
| 2317 | |
| 2318 | |
| 2319 | // --- I m p l e m e n t a t i o n --- |
| 2320 | |
| 2321 | template <class T> |
| 2322 | Handle<T>::Handle() : val_(0) { } |
| 2323 | |
| 2324 | |
| 2325 | template <class T> |
| 2326 | Local<T>::Local() : Handle<T>() { } |
| 2327 | |
| 2328 | |
| 2329 | template <class T> |
| 2330 | Local<T> Local<T>::New(Handle<T> that) { |
| 2331 | if (that.IsEmpty()) return Local<T>(); |
| 2332 | void** p = reinterpret_cast<void**>(*that); |
| 2333 | return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p))); |
| 2334 | } |
| 2335 | |
| 2336 | |
| 2337 | template <class T> |
| 2338 | Persistent<T> Persistent<T>::New(Handle<T> that) { |
| 2339 | if (that.IsEmpty()) return Persistent<T>(); |
| 2340 | void** p = reinterpret_cast<void**>(*that); |
| 2341 | return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p))); |
| 2342 | } |
| 2343 | |
| 2344 | |
| 2345 | template <class T> |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2346 | bool Persistent<T>::IsNearDeath() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2347 | if (this->IsEmpty()) return false; |
| 2348 | return V8::IsGlobalNearDeath(reinterpret_cast<void**>(**this)); |
| 2349 | } |
| 2350 | |
| 2351 | |
| 2352 | template <class T> |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2353 | bool Persistent<T>::IsWeak() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2354 | if (this->IsEmpty()) return false; |
| 2355 | return V8::IsGlobalWeak(reinterpret_cast<void**>(**this)); |
| 2356 | } |
| 2357 | |
| 2358 | |
| 2359 | template <class T> |
| 2360 | void Persistent<T>::Dispose() { |
| 2361 | if (this->IsEmpty()) return; |
| 2362 | V8::DisposeGlobal(reinterpret_cast<void**>(**this)); |
| 2363 | } |
| 2364 | |
| 2365 | |
| 2366 | template <class T> |
| 2367 | Persistent<T>::Persistent() : Handle<T>() { } |
| 2368 | |
| 2369 | template <class T> |
| 2370 | void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) { |
| 2371 | V8::MakeWeak(reinterpret_cast<void**>(**this), parameters, callback); |
| 2372 | } |
| 2373 | |
| 2374 | template <class T> |
| 2375 | void Persistent<T>::ClearWeak() { |
| 2376 | V8::ClearWeak(reinterpret_cast<void**>(**this)); |
| 2377 | } |
| 2378 | |
| 2379 | template <class T> |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2380 | T* Handle<T>::operator->() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2381 | return val_; |
| 2382 | } |
| 2383 | |
| 2384 | |
| 2385 | template <class T> |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2386 | T* Handle<T>::operator*() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2387 | return val_; |
| 2388 | } |
| 2389 | |
| 2390 | |
| 2391 | Local<Value> Arguments::operator[](int i) const { |
| 2392 | if (i < 0 || length_ <= i) return Local<Value>(*Undefined()); |
| 2393 | return Local<Value>(reinterpret_cast<Value*>(values_ - i)); |
| 2394 | } |
| 2395 | |
| 2396 | |
| 2397 | Local<Function> Arguments::Callee() const { |
| 2398 | return callee_; |
| 2399 | } |
| 2400 | |
| 2401 | |
| 2402 | Local<Object> Arguments::This() const { |
| 2403 | return Local<Object>(reinterpret_cast<Object*>(values_ + 1)); |
| 2404 | } |
| 2405 | |
| 2406 | |
| 2407 | Local<Object> Arguments::Holder() const { |
| 2408 | return holder_; |
| 2409 | } |
| 2410 | |
| 2411 | |
| 2412 | Local<Value> Arguments::Data() const { |
| 2413 | return data_; |
| 2414 | } |
| 2415 | |
| 2416 | |
| 2417 | bool Arguments::IsConstructCall() const { |
| 2418 | return is_construct_call_; |
| 2419 | } |
| 2420 | |
| 2421 | |
| 2422 | int Arguments::Length() const { |
| 2423 | return length_; |
| 2424 | } |
| 2425 | |
| 2426 | |
| 2427 | Local<Value> AccessorInfo::Data() const { |
| 2428 | return data_; |
| 2429 | } |
| 2430 | |
| 2431 | |
| 2432 | Local<Object> AccessorInfo::This() const { |
| 2433 | return self_; |
| 2434 | } |
| 2435 | |
| 2436 | |
| 2437 | Local<Object> AccessorInfo::Holder() const { |
| 2438 | return holder_; |
| 2439 | } |
| 2440 | |
| 2441 | |
| 2442 | template <class T> |
| 2443 | Local<T> HandleScope::Close(Handle<T> value) { |
| 2444 | void** after = RawClose(reinterpret_cast<void**>(*value)); |
| 2445 | return Local<T>(reinterpret_cast<T*>(after)); |
| 2446 | } |
| 2447 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2448 | Handle<Value> ScriptOrigin::ResourceName() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2449 | return resource_name_; |
| 2450 | } |
| 2451 | |
| 2452 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2453 | Handle<Integer> ScriptOrigin::ResourceLineOffset() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2454 | return resource_line_offset_; |
| 2455 | } |
| 2456 | |
| 2457 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2458 | Handle<Integer> ScriptOrigin::ResourceColumnOffset() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2459 | return resource_column_offset_; |
| 2460 | } |
| 2461 | |
| 2462 | |
| 2463 | Handle<Boolean> Boolean::New(bool value) { |
| 2464 | return value ? True() : False(); |
| 2465 | } |
| 2466 | |
| 2467 | |
| 2468 | void Template::Set(const char* name, v8::Handle<Data> value) { |
| 2469 | Set(v8::String::New(name), value); |
| 2470 | } |
| 2471 | |
| 2472 | |
| 2473 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2474 | * \example shell.cc |
| 2475 | * A simple shell that takes a list of expressions on the |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2476 | * command-line and executes them. |
| 2477 | */ |
| 2478 | |
| 2479 | |
| 2480 | /** |
| 2481 | * \example process.cc |
| 2482 | */ |
| 2483 | |
| 2484 | |
| 2485 | } // namespace v8 |
| 2486 | |
| 2487 | |
| 2488 | #undef EXPORT |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2489 | #undef EXPORT_INLINE |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2490 | #undef TYPE_CHECK |
| 2491 | |
| 2492 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2493 | #endif // V8_H_ |