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