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