sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1 | // Copyright 2007-2009 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 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 130 | namespace internal { |
| 131 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 132 | class Arguments; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 133 | class Object; |
| 134 | class Top; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 135 | |
| 136 | } |
| 137 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 138 | |
| 139 | // --- W e a k H a n d l e s |
| 140 | |
| 141 | |
| 142 | /** |
| 143 | * A weak reference callback function. |
| 144 | * |
| 145 | * \param object the weak global object to be reclaimed by the garbage collector |
| 146 | * \param parameter the value passed in when making the weak global object |
| 147 | */ |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 148 | typedef void (*WeakReferenceCallback)(Persistent<Value> object, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 149 | void* parameter); |
| 150 | |
| 151 | |
| 152 | // --- H a n d l e s --- |
| 153 | |
| 154 | #define TYPE_CHECK(T, S) \ |
| 155 | while (false) { \ |
| 156 | *(static_cast<T**>(0)) = static_cast<S*>(0); \ |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * An object reference managed by the v8 garbage collector. |
| 161 | * |
| 162 | * All objects returned from v8 have to be tracked by the garbage |
| 163 | * collector so that it knows that the objects are still alive. Also, |
| 164 | * because the garbage collector may move objects, it is unsafe to |
| 165 | * point directly to an object. Instead, all objects are stored in |
| 166 | * handles which are known by the garbage collector and updated |
| 167 | * whenever an object moves. Handles should always be passed by value |
| 168 | * (except in cases like out-parameters) and they should never be |
| 169 | * allocated on the heap. |
| 170 | * |
| 171 | * There are two types of handles: local and persistent handles. |
| 172 | * Local handles are light-weight and transient and typically used in |
| 173 | * local operations. They are managed by HandleScopes. Persistent |
| 174 | * handles can be used when storing objects across several independent |
| 175 | * operations and have to be explicitly deallocated when they're no |
| 176 | * longer used. |
| 177 | * |
| 178 | * It is safe to extract the object stored in the handle by |
| 179 | * dereferencing the handle (for instance, to extract the Object* from |
| 180 | * an Handle<Object>); the value will still be governed by a handle |
| 181 | * behind the scenes and the same rules apply to these values as to |
| 182 | * their handles. |
| 183 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 184 | template <class T> class V8EXPORT_INLINE Handle { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 185 | public: |
| 186 | |
| 187 | /** |
| 188 | * Creates an empty handle. |
| 189 | */ |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 190 | inline Handle(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * Creates a new handle for the specified value. |
| 194 | */ |
| 195 | explicit Handle(T* val) : val_(val) { } |
| 196 | |
| 197 | /** |
| 198 | * Creates a handle for the contents of the specified handle. This |
| 199 | * constructor allows you to pass handles as arguments by value and |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 200 | * to assign between handles. However, if you try to assign between |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 201 | * incompatible handles, for instance from a Handle<String> to a |
| 202 | * Handle<Number> it will cause a compiletime error. Assigning |
| 203 | * between compatible handles, for instance assigning a |
| 204 | * Handle<String> to a variable declared as Handle<Value>, is legal |
| 205 | * because String is a subclass of Value. |
| 206 | */ |
| 207 | template <class S> inline Handle(Handle<S> that) |
| 208 | : val_(reinterpret_cast<T*>(*that)) { |
| 209 | /** |
| 210 | * This check fails when trying to convert between incompatible |
| 211 | * handles. For example, converting from a Handle<String> to a |
| 212 | * Handle<Number>. |
| 213 | */ |
| 214 | TYPE_CHECK(T, S); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Returns true if the handle is empty. |
| 219 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 220 | bool IsEmpty() const { return val_ == 0; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 221 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 222 | T* operator->() const { return val_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 223 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 224 | T* operator*() const { return val_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 225 | |
| 226 | /** |
| 227 | * Sets the handle to be empty. IsEmpty() will then return true. |
| 228 | */ |
| 229 | void Clear() { this->val_ = 0; } |
| 230 | |
| 231 | /** |
| 232 | * Checks whether two handles are the same. |
| 233 | * Returns true if both are empty, or if the objects |
| 234 | * to which they refer are identical. |
| 235 | * The handles' references are not checked. |
| 236 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 237 | template <class S> bool operator==(Handle<S> that) const { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 238 | internal::Object** a = reinterpret_cast<internal::Object**>(**this); |
| 239 | internal::Object** b = reinterpret_cast<internal::Object**>(*that); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 240 | if (a == 0) return b == 0; |
| 241 | if (b == 0) return false; |
| 242 | return *a == *b; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Checks whether two handles are different. |
| 247 | * Returns true if only one of the handles is empty, or if |
| 248 | * the objects to which they refer are different. |
| 249 | * The handles' references are not checked. |
| 250 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 251 | template <class S> bool operator!=(Handle<S> that) const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 252 | return !operator==(that); |
| 253 | } |
| 254 | |
| 255 | template <class S> static inline Handle<T> Cast(Handle<S> that) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 256 | #ifdef V8_ENABLE_CHECKS |
| 257 | // If we're going to perform the type check then we have to check |
| 258 | // that the handle isn't empty before doing the checked cast. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 259 | if (that.IsEmpty()) return Handle<T>(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 260 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 261 | return Handle<T>(T::Cast(*that)); |
| 262 | } |
| 263 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame^] | 264 | template <class S> inline Handle<S> As() { |
| 265 | return Handle<S>::Cast(*this); |
| 266 | } |
| 267 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 268 | private: |
| 269 | T* val_; |
| 270 | }; |
| 271 | |
| 272 | |
| 273 | /** |
| 274 | * A light-weight stack-allocated object handle. All operations |
| 275 | * that return objects from within v8 return them in local handles. They |
| 276 | * are created within HandleScopes, and all local handles allocated within a |
| 277 | * handle scope are destroyed when the handle scope is destroyed. Hence it |
| 278 | * is not necessary to explicitly deallocate local handles. |
| 279 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 280 | template <class T> class V8EXPORT_INLINE Local : public Handle<T> { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 281 | public: |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 282 | inline Local(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 283 | template <class S> inline Local(Local<S> that) |
| 284 | : Handle<T>(reinterpret_cast<T*>(*that)) { |
| 285 | /** |
| 286 | * This check fails when trying to convert between incompatible |
| 287 | * handles. For example, converting from a Handle<String> to a |
| 288 | * Handle<Number>. |
| 289 | */ |
| 290 | TYPE_CHECK(T, S); |
| 291 | } |
| 292 | template <class S> inline Local(S* that) : Handle<T>(that) { } |
| 293 | template <class S> static inline Local<T> Cast(Local<S> that) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 294 | #ifdef V8_ENABLE_CHECKS |
| 295 | // If we're going to perform the type check then we have to check |
| 296 | // that the handle isn't empty before doing the checked cast. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 297 | if (that.IsEmpty()) return Local<T>(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 298 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 299 | return Local<T>(T::Cast(*that)); |
| 300 | } |
| 301 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame^] | 302 | template <class S> inline Local<S> As() { |
| 303 | return Local<S>::Cast(*this); |
| 304 | } |
| 305 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 306 | /** Create a local handle for the content of another handle. |
| 307 | * The referee is kept alive by the local handle even when |
| 308 | * the original handle is destroyed/disposed. |
| 309 | */ |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 310 | inline static Local<T> New(Handle<T> that); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | |
| 314 | /** |
| 315 | * An object reference that is independent of any handle scope. Where |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 316 | * 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] | 317 | * allocated, a Persistent handle remains valid until it is explicitly |
| 318 | * disposed. |
| 319 | * |
| 320 | * A persistent handle contains a reference to a storage cell within |
| 321 | * the v8 engine which holds an object value and which is updated by |
| 322 | * the garbage collector whenever the object is moved. A new storage |
| 323 | * cell can be created using Persistent::New and existing handles can |
| 324 | * be disposed using Persistent::Dispose. Since persistent handles |
| 325 | * are passed by value you may have many persistent handle objects |
| 326 | * that point to the same storage cell. For instance, if you pass a |
| 327 | * persistent handle as an argument to a function you will not get two |
| 328 | * different storage cells but rather two references to the same |
| 329 | * storage cell. |
| 330 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 331 | template <class T> class V8EXPORT_INLINE Persistent : public Handle<T> { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 332 | public: |
| 333 | |
| 334 | /** |
| 335 | * Creates an empty persistent handle that doesn't point to any |
| 336 | * storage cell. |
| 337 | */ |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 338 | inline Persistent(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 339 | |
| 340 | /** |
| 341 | * Creates a persistent handle for the same storage cell as the |
| 342 | * specified handle. This constructor allows you to pass persistent |
| 343 | * handles as arguments by value and to assign between persistent |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 344 | * handles. However, attempting to assign between incompatible |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 345 | * persistent handles, for instance from a Persistent<String> to a |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 346 | * Persistent<Number> will cause a compiletime error. Assigning |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 347 | * between compatible persistent handles, for instance assigning a |
| 348 | * Persistent<String> to a variable declared as Persistent<Value>, |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 349 | * is allowed as String is a subclass of Value. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 350 | */ |
| 351 | template <class S> inline Persistent(Persistent<S> that) |
| 352 | : Handle<T>(reinterpret_cast<T*>(*that)) { |
| 353 | /** |
| 354 | * This check fails when trying to convert between incompatible |
| 355 | * handles. For example, converting from a Handle<String> to a |
| 356 | * Handle<Number>. |
| 357 | */ |
| 358 | TYPE_CHECK(T, S); |
| 359 | } |
| 360 | |
| 361 | template <class S> inline Persistent(S* that) : Handle<T>(that) { } |
| 362 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 363 | /** |
| 364 | * "Casts" a plain handle which is known to be a persistent handle |
| 365 | * to a persistent handle. |
| 366 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 367 | template <class S> explicit inline Persistent(Handle<S> that) |
| 368 | : Handle<T>(*that) { } |
| 369 | |
| 370 | template <class S> static inline Persistent<T> Cast(Persistent<S> that) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 371 | #ifdef V8_ENABLE_CHECKS |
| 372 | // If we're going to perform the type check then we have to check |
| 373 | // that the handle isn't empty before doing the checked cast. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 374 | if (that.IsEmpty()) return Persistent<T>(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 375 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 376 | return Persistent<T>(T::Cast(*that)); |
| 377 | } |
| 378 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame^] | 379 | template <class S> inline Persistent<S> As() { |
| 380 | return Persistent<S>::Cast(*this); |
| 381 | } |
| 382 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 383 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 384 | * Creates a new persistent handle for an existing local or |
| 385 | * persistent handle. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 386 | */ |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 387 | inline static Persistent<T> New(Handle<T> that); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 388 | |
| 389 | /** |
| 390 | * Releases the storage cell referenced by this persistent handle. |
| 391 | * Does not remove the reference to the cell from any handles. |
| 392 | * This handle's reference, and any any other references to the storage |
| 393 | * cell remain and IsEmpty will still return false. |
| 394 | */ |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 395 | inline void Dispose(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 396 | |
| 397 | /** |
| 398 | * Make the reference to this object weak. When only weak handles |
| 399 | * refer to the object, the garbage collector will perform a |
| 400 | * callback to the given V8::WeakReferenceCallback function, passing |
| 401 | * it the object reference and the given parameters. |
| 402 | */ |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 403 | inline void MakeWeak(void* parameters, WeakReferenceCallback callback); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 404 | |
| 405 | /** Clears the weak reference to this object.*/ |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 406 | inline void ClearWeak(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 407 | |
| 408 | /** |
| 409 | *Checks if the handle holds the only reference to an object. |
| 410 | */ |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 411 | inline bool IsNearDeath() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 412 | |
| 413 | /** |
| 414 | * Returns true if the handle's reference is weak. |
| 415 | */ |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 416 | inline bool IsWeak() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 417 | |
| 418 | private: |
| 419 | friend class ImplementationUtilities; |
| 420 | friend class ObjectTemplate; |
| 421 | }; |
| 422 | |
| 423 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 424 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 425 | * A stack-allocated class that governs a number of local handles. |
| 426 | * After a handle scope has been created, all local handles will be |
| 427 | * allocated within that handle scope until either the handle scope is |
| 428 | * deleted or another handle scope is created. If there is already a |
| 429 | * 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] | 430 | * 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] | 431 | * new handles will again be allocated in the original handle scope. |
| 432 | * |
| 433 | * After the handle scope of a local handle has been deleted the |
| 434 | * garbage collector will no longer track the object stored in the |
| 435 | * handle and may deallocate it. The behavior of accessing a handle |
| 436 | * for which the handle scope has been deleted is undefined. |
| 437 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 438 | class V8EXPORT HandleScope { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 439 | public: |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 440 | HandleScope(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 441 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 442 | ~HandleScope(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 443 | |
| 444 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 445 | * Closes the handle scope and returns the value as a handle in the |
| 446 | * previous scope, which is the new current scope after the call. |
| 447 | */ |
| 448 | template <class T> Local<T> Close(Handle<T> value); |
| 449 | |
| 450 | /** |
| 451 | * Counts the number of allocated handles. |
| 452 | */ |
| 453 | static int NumberOfHandles(); |
| 454 | |
| 455 | /** |
| 456 | * Creates a new handle with the given value. |
| 457 | */ |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 458 | static internal::Object** CreateHandle(internal::Object* value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 459 | |
| 460 | private: |
| 461 | // Make it impossible to create heap-allocated or illegal handle |
| 462 | // scopes by disallowing certain operations. |
| 463 | HandleScope(const HandleScope&); |
| 464 | void operator=(const HandleScope&); |
| 465 | void* operator new(size_t size); |
| 466 | void operator delete(void*, size_t); |
| 467 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 468 | // This Data class is accessible internally as HandleScopeData through a |
| 469 | // typedef in the ImplementationUtilities class. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 470 | class V8EXPORT Data { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 471 | public: |
| 472 | int extensions; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 473 | internal::Object** next; |
| 474 | internal::Object** limit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 475 | inline void Initialize() { |
| 476 | extensions = -1; |
| 477 | next = limit = NULL; |
| 478 | } |
| 479 | }; |
| 480 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 481 | Data previous_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 482 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 483 | // Allow for the active closing of HandleScopes which allows to pass a handle |
| 484 | // from the HandleScope being closed to the next top most HandleScope. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 485 | bool is_closed_; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 486 | internal::Object** RawClose(internal::Object** value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 487 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 488 | friend class ImplementationUtilities; |
| 489 | }; |
| 490 | |
| 491 | |
| 492 | // --- S p e c i a l o b j e c t s --- |
| 493 | |
| 494 | |
| 495 | /** |
| 496 | * The superclass of values and API object templates. |
| 497 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 498 | class V8EXPORT Data { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 499 | private: |
| 500 | Data(); |
| 501 | }; |
| 502 | |
| 503 | |
| 504 | /** |
| 505 | * Pre-compilation data that can be associated with a script. This |
| 506 | * data can be calculated for a script in advance of actually |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 507 | * compiling it, and can be stored between compilations. When script |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 508 | * data is given to the compile method compilation will be faster. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 509 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 510 | class V8EXPORT ScriptData { // NOLINT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 511 | public: |
| 512 | virtual ~ScriptData() { } |
| 513 | static ScriptData* PreCompile(const char* input, int length); |
| 514 | static ScriptData* New(unsigned* data, int length); |
| 515 | |
| 516 | virtual int Length() = 0; |
| 517 | virtual unsigned* Data() = 0; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 518 | virtual bool HasError() = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 519 | }; |
| 520 | |
| 521 | |
| 522 | /** |
| 523 | * The origin, within a file, of a script. |
| 524 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 525 | class V8EXPORT ScriptOrigin { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 526 | public: |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 527 | ScriptOrigin(Handle<Value> resource_name, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 528 | Handle<Integer> resource_line_offset = Handle<Integer>(), |
| 529 | Handle<Integer> resource_column_offset = Handle<Integer>()) |
| 530 | : resource_name_(resource_name), |
| 531 | resource_line_offset_(resource_line_offset), |
| 532 | resource_column_offset_(resource_column_offset) { } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 533 | inline Handle<Value> ResourceName() const; |
| 534 | inline Handle<Integer> ResourceLineOffset() const; |
| 535 | inline Handle<Integer> ResourceColumnOffset() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 536 | private: |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 537 | Handle<Value> resource_name_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 538 | Handle<Integer> resource_line_offset_; |
| 539 | Handle<Integer> resource_column_offset_; |
| 540 | }; |
| 541 | |
| 542 | |
| 543 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 544 | * A compiled JavaScript script. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 545 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 546 | class V8EXPORT Script { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 547 | public: |
| 548 | |
| 549 | /** |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 550 | * Compiles the specified script (context-independent). |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 551 | * |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 552 | * \param source Script source code. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame^] | 553 | * \param origin Script origin, owned by caller, no references are kept |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 554 | * when New() returns |
| 555 | * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile() |
| 556 | * using pre_data speeds compilation if it's done multiple times. |
| 557 | * Owned by caller, no references are kept when New() returns. |
| 558 | * \param script_data Arbitrary data associated with script. Using |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame^] | 559 | * this has same effect as calling SetData(), but allows data to be |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 560 | * available to compile event handlers. |
| 561 | * \return Compiled script object (context independent; when run it |
| 562 | * will use the currently entered context). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 563 | */ |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 564 | static Local<Script> New(Handle<String> source, |
| 565 | ScriptOrigin* origin = NULL, |
| 566 | ScriptData* pre_data = NULL, |
| 567 | Handle<String> script_data = Handle<String>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 568 | |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 569 | /** |
| 570 | * Compiles the specified script using the specified file name |
| 571 | * object (typically a string) as the script's origin. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 572 | * |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 573 | * \param source Script source code. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame^] | 574 | * \param file_name file name object (typically a string) to be used |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 575 | * as the script's origin. |
| 576 | * \return Compiled script object (context independent; when run it |
| 577 | * will use the currently entered context). |
| 578 | */ |
| 579 | static Local<Script> New(Handle<String> source, |
| 580 | Handle<Value> file_name); |
| 581 | |
| 582 | /** |
| 583 | * Compiles the specified script (bound to current context). |
| 584 | * |
| 585 | * \param source Script source code. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame^] | 586 | * \param origin Script origin, owned by caller, no references are kept |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 587 | * when Compile() returns |
| 588 | * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile() |
| 589 | * using pre_data speeds compilation if it's done multiple times. |
| 590 | * Owned by caller, no references are kept when Compile() returns. |
| 591 | * \param script_data Arbitrary data associated with script. Using |
| 592 | * this has same effect as calling SetData(), but makes data available |
| 593 | * earlier (i.e. to compile event handlers). |
| 594 | * \return Compiled script object, bound to the context that was active |
| 595 | * when this function was called. When run it will always use this |
| 596 | * context. |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 597 | */ |
| 598 | static Local<Script> Compile(Handle<String> source, |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 599 | ScriptOrigin* origin = NULL, |
| 600 | ScriptData* pre_data = NULL, |
| 601 | Handle<String> script_data = Handle<String>()); |
| 602 | |
| 603 | /** |
| 604 | * Compiles the specified script using the specified file name |
| 605 | * object (typically a string) as the script's origin. |
| 606 | * |
| 607 | * \param source Script source code. |
| 608 | * \param file_name File name to use as script's origin |
| 609 | * \param script_data Arbitrary data associated with script. Using |
| 610 | * this has same effect as calling SetData(), but makes data available |
| 611 | * earlier (i.e. to compile event handlers). |
| 612 | * \return Compiled script object, bound to the context that was active |
| 613 | * when this function was called. When run it will always use this |
| 614 | * context. |
| 615 | */ |
| 616 | static Local<Script> Compile(Handle<String> source, |
| 617 | Handle<Value> file_name, |
| 618 | Handle<String> script_data = Handle<String>()); |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 619 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 620 | /** |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 621 | * Runs the script returning the resulting value. If the script is |
| 622 | * context independent (created using ::New) it will be run in the |
| 623 | * currently entered context. If it is context specific (created |
| 624 | * using ::Compile) it will be run in the context in which it was |
| 625 | * compiled. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 626 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 627 | Local<Value> Run(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 628 | |
| 629 | /** |
| 630 | * Returns the script id value. |
| 631 | */ |
| 632 | Local<Value> Id(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 633 | |
| 634 | /** |
| 635 | * Associate an additional data object with the script. This is mainly used |
| 636 | * with the debugger as this data object is only available through the |
| 637 | * debugger API. |
| 638 | */ |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 639 | void SetData(Handle<String> data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 640 | }; |
| 641 | |
| 642 | |
| 643 | /** |
| 644 | * An error message. |
| 645 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 646 | class V8EXPORT Message { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 647 | public: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 648 | Local<String> Get() const; |
| 649 | Local<String> GetSourceLine() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 650 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 651 | /** |
| 652 | * Returns the resource name for the script from where the function causing |
| 653 | * the error originates. |
| 654 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 655 | Handle<Value> GetScriptResourceName() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 656 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 657 | /** |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 658 | * Returns the resource data for the script from where the function causing |
| 659 | * the error originates. |
| 660 | */ |
| 661 | Handle<Value> GetScriptData() const; |
| 662 | |
| 663 | /** |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 664 | * Returns the number, 1-based, of the line where the error occurred. |
| 665 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 666 | int GetLineNumber() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 667 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 668 | /** |
| 669 | * Returns the index within the script of the first character where |
| 670 | * the error occurred. |
| 671 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 672 | int GetStartPosition() const; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 673 | |
| 674 | /** |
| 675 | * Returns the index within the script of the last character where |
| 676 | * the error occurred. |
| 677 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 678 | int GetEndPosition() const; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 679 | |
| 680 | /** |
| 681 | * Returns the index within the line of the first character where |
| 682 | * the error occurred. |
| 683 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 684 | int GetStartColumn() const; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 685 | |
| 686 | /** |
| 687 | * Returns the index within the line of the last character where |
| 688 | * the error occurred. |
| 689 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 690 | int GetEndColumn() const; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 691 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 692 | // TODO(1245381): Print to a string instead of on a FILE. |
| 693 | static void PrintCurrentStackTrace(FILE* out); |
| 694 | }; |
| 695 | |
| 696 | |
| 697 | // --- V a l u e --- |
| 698 | |
| 699 | |
| 700 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 701 | * The superclass of all JavaScript values and objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 702 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 703 | class V8EXPORT Value : public Data { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 704 | public: |
| 705 | |
| 706 | /** |
| 707 | * Returns true if this value is the undefined value. See ECMA-262 |
| 708 | * 4.3.10. |
| 709 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 710 | bool IsUndefined() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 711 | |
| 712 | /** |
| 713 | * Returns true if this value is the null value. See ECMA-262 |
| 714 | * 4.3.11. |
| 715 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 716 | bool IsNull() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 717 | |
| 718 | /** |
| 719 | * Returns true if this value is true. |
| 720 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 721 | bool IsTrue() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 722 | |
| 723 | /** |
| 724 | * Returns true if this value is false. |
| 725 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 726 | bool IsFalse() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 727 | |
| 728 | /** |
| 729 | * Returns true if this value is an instance of the String type. |
| 730 | * See ECMA-262 8.4. |
| 731 | */ |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 732 | inline bool IsString() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 733 | |
| 734 | /** |
| 735 | * Returns true if this value is a function. |
| 736 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 737 | bool IsFunction() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 738 | |
| 739 | /** |
| 740 | * Returns true if this value is an array. |
| 741 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 742 | bool IsArray() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 743 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 744 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 745 | * Returns true if this value is an object. |
| 746 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 747 | bool IsObject() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 748 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 749 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 750 | * Returns true if this value is boolean. |
| 751 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 752 | bool IsBoolean() const; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 753 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 754 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 755 | * Returns true if this value is a number. |
| 756 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 757 | bool IsNumber() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 758 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 759 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 760 | * Returns true if this value is external. |
| 761 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 762 | bool IsExternal() const; |
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 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 765 | * Returns true if this value is a 32-bit signed integer. |
| 766 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 767 | bool IsInt32() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 768 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 769 | /** |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame^] | 770 | * Returns true if this value is a 32-bit signed integer. |
| 771 | */ |
| 772 | bool IsUint32() const; |
| 773 | |
| 774 | /** |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 775 | * Returns true if this value is a Date. |
| 776 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 777 | bool IsDate() const; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 778 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 779 | Local<Boolean> ToBoolean() const; |
| 780 | Local<Number> ToNumber() const; |
| 781 | Local<String> ToString() const; |
| 782 | Local<String> ToDetailString() const; |
| 783 | Local<Object> ToObject() const; |
| 784 | Local<Integer> ToInteger() const; |
| 785 | Local<Uint32> ToUint32() const; |
| 786 | Local<Int32> ToInt32() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 787 | |
| 788 | /** |
| 789 | * Attempts to convert a string to an array index. |
| 790 | * Returns an empty handle if the conversion fails. |
| 791 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 792 | Local<Uint32> ToArrayIndex() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 793 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 794 | bool BooleanValue() const; |
| 795 | double NumberValue() const; |
| 796 | int64_t IntegerValue() const; |
| 797 | uint32_t Uint32Value() const; |
| 798 | int32_t Int32Value() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 799 | |
| 800 | /** JS == */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 801 | bool Equals(Handle<Value> that) const; |
| 802 | bool StrictEquals(Handle<Value> that) const; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 803 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 804 | private: |
| 805 | inline bool QuickIsString() const; |
| 806 | bool FullIsString() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 807 | }; |
| 808 | |
| 809 | |
| 810 | /** |
| 811 | * The superclass of primitive values. See ECMA-262 4.3.2. |
| 812 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 813 | class V8EXPORT Primitive : public Value { }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 814 | |
| 815 | |
| 816 | /** |
| 817 | * A primitive boolean value (ECMA-262, 4.3.14). Either the true |
| 818 | * or false value. |
| 819 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 820 | class V8EXPORT Boolean : public Primitive { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 821 | public: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 822 | bool Value() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 823 | static inline Handle<Boolean> New(bool value); |
| 824 | }; |
| 825 | |
| 826 | |
| 827 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 828 | * A JavaScript string value (ECMA-262, 4.3.17). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 829 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 830 | class V8EXPORT String : public Primitive { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 831 | public: |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 832 | |
| 833 | /** |
| 834 | * Returns the number of characters in this string. |
| 835 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 836 | int Length() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 837 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 838 | /** |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 839 | * Returns the number of bytes in the UTF-8 encoded |
| 840 | * representation of this string. |
| 841 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 842 | int Utf8Length() const; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 843 | |
| 844 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 845 | * Write the contents of the string to an external buffer. |
| 846 | * If no arguments are given, expects the buffer to be large |
| 847 | * enough to hold the entire string and NULL terminator. Copies |
| 848 | * the contents of the string and the NULL terminator into the |
| 849 | * buffer. |
| 850 | * |
| 851 | * Copies up to length characters into the output buffer. |
| 852 | * Only null-terminates if there is enough space in the buffer. |
| 853 | * |
| 854 | * \param buffer The buffer into which the string will be copied. |
| 855 | * \param start The starting position within the string at which |
| 856 | * copying begins. |
| 857 | * \param length The number of bytes to copy from the string. |
| 858 | * \return The number of characters copied to the buffer |
| 859 | * excluding the NULL terminator. |
| 860 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 861 | int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16 |
| 862 | int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII |
| 863 | int WriteUtf8(char* buffer, int length = -1) const; // UTF-8 |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 864 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 865 | /** |
ager@chromium.org | 563b8dc | 2009-03-20 14:23:52 +0000 | [diff] [blame] | 866 | * A zero length string. |
| 867 | */ |
| 868 | static v8::Local<v8::String> Empty(); |
| 869 | |
| 870 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 871 | * Returns true if the string is external |
| 872 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 873 | bool IsExternal() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 874 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 875 | /** |
| 876 | * Returns true if the string is both external and ascii |
| 877 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 878 | bool IsExternalAscii() const; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 879 | |
| 880 | class V8EXPORT ExternalStringResourceBase { |
| 881 | public: |
| 882 | virtual ~ExternalStringResourceBase() {} |
| 883 | protected: |
| 884 | ExternalStringResourceBase() {} |
| 885 | private: |
| 886 | // Disallow copying and assigning. |
| 887 | ExternalStringResourceBase(const ExternalStringResourceBase&); |
| 888 | void operator=(const ExternalStringResourceBase&); |
| 889 | }; |
| 890 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 891 | /** |
| 892 | * An ExternalStringResource is a wrapper around a two-byte string |
| 893 | * buffer that resides outside V8's heap. Implement an |
| 894 | * ExternalStringResource to manage the life cycle of the underlying |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 895 | * buffer. Note that the string data must be immutable. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 896 | */ |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 897 | class V8EXPORT ExternalStringResource |
| 898 | : public ExternalStringResourceBase { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 899 | public: |
| 900 | /** |
| 901 | * Override the destructor to manage the life cycle of the underlying |
| 902 | * buffer. |
| 903 | */ |
| 904 | virtual ~ExternalStringResource() {} |
| 905 | /** The string data from the underlying buffer.*/ |
| 906 | virtual const uint16_t* data() const = 0; |
| 907 | /** The length of the string. That is, the number of two-byte characters.*/ |
| 908 | virtual size_t length() const = 0; |
| 909 | protected: |
| 910 | ExternalStringResource() {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 911 | }; |
| 912 | |
| 913 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 914 | * An ExternalAsciiStringResource is a wrapper around an ascii |
| 915 | * string buffer that resides outside V8's heap. Implement an |
| 916 | * ExternalAsciiStringResource to manage the life cycle of the |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 917 | * underlying buffer. Note that the string data must be immutable |
| 918 | * and that the data must be strict 7-bit ASCII, not Latin1 or |
| 919 | * UTF-8, which would require special treatment internally in the |
| 920 | * engine and, in the case of UTF-8, do not allow efficient indexing. |
| 921 | * 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] | 922 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 923 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 924 | class V8EXPORT ExternalAsciiStringResource |
| 925 | : public ExternalStringResourceBase { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 926 | public: |
| 927 | /** |
| 928 | * Override the destructor to manage the life cycle of the underlying |
| 929 | * buffer. |
| 930 | */ |
| 931 | virtual ~ExternalAsciiStringResource() {} |
| 932 | /** The string data from the underlying buffer.*/ |
| 933 | virtual const char* data() const = 0; |
| 934 | /** The number of ascii characters in the string.*/ |
| 935 | virtual size_t length() const = 0; |
| 936 | protected: |
| 937 | ExternalAsciiStringResource() {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 938 | }; |
| 939 | |
| 940 | /** |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 941 | * Get the ExternalStringResource for an external string. Returns |
| 942 | * NULL if IsExternal() doesn't return true. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 943 | */ |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 944 | inline ExternalStringResource* GetExternalStringResource() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 945 | |
| 946 | /** |
| 947 | * Get the ExternalAsciiStringResource for an external ascii string. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 948 | * Returns NULL if IsExternalAscii() doesn't return true. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 949 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 950 | ExternalAsciiStringResource* GetExternalAsciiStringResource() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 951 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 952 | static inline String* Cast(v8::Value* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 953 | |
| 954 | /** |
| 955 | * Allocates a new string from either utf-8 encoded or ascii data. |
| 956 | * The second parameter 'length' gives the buffer length. |
| 957 | * If the data is utf-8 encoded, the caller must |
| 958 | * be careful to supply the length parameter. |
| 959 | * If it is not given, the function calls |
| 960 | * 'strlen' to determine the buffer length, it might be |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 961 | * wrong if 'data' contains a null character. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 962 | */ |
| 963 | static Local<String> New(const char* data, int length = -1); |
| 964 | |
| 965 | /** Allocates a new string from utf16 data.*/ |
| 966 | static Local<String> New(const uint16_t* data, int length = -1); |
| 967 | |
| 968 | /** Creates a symbol. Returns one if it exists already.*/ |
| 969 | static Local<String> NewSymbol(const char* data, int length = -1); |
| 970 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 971 | /** |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 972 | * Creates a new string by concatenating the left and the right strings |
| 973 | * passed in as parameters. |
| 974 | */ |
| 975 | static Local<String> Concat(Handle<String> left, Handle<String>right); |
| 976 | |
| 977 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 978 | * Creates a new external string using the data defined in the given |
| 979 | * resource. The resource is deleted when the external string is no |
| 980 | * longer live on V8's heap. The caller of this function should not |
| 981 | * delete or modify the resource. Neither should the underlying buffer be |
| 982 | * deallocated or modified except through the destructor of the |
| 983 | * external string resource. |
| 984 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 985 | static Local<String> NewExternal(ExternalStringResource* resource); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 986 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 987 | /** |
| 988 | * Associate an external string resource with this string by transforming it |
| 989 | * in place so that existing references to this string in the JavaScript heap |
| 990 | * will use the external string resource. The external string resource's |
| 991 | * character contents needs to be equivalent to this string. |
| 992 | * Returns true if the string has been changed to be an external string. |
| 993 | * The string is not modified if the operation fails. |
| 994 | */ |
| 995 | bool MakeExternal(ExternalStringResource* resource); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 996 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 997 | /** |
| 998 | * Creates a new external string using the ascii data defined in the given |
| 999 | * resource. The resource is deleted when the external string is no |
| 1000 | * longer live on V8's heap. The caller of this function should not |
| 1001 | * delete or modify the resource. Neither should the underlying buffer be |
| 1002 | * deallocated or modified except through the destructor of the |
| 1003 | * external string resource. |
| 1004 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1005 | static Local<String> NewExternal(ExternalAsciiStringResource* resource); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1006 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1007 | /** |
| 1008 | * Associate an external string resource with this string by transforming it |
| 1009 | * in place so that existing references to this string in the JavaScript heap |
| 1010 | * will use the external string resource. The external string resource's |
| 1011 | * character contents needs to be equivalent to this string. |
| 1012 | * Returns true if the string has been changed to be an external string. |
| 1013 | * The string is not modified if the operation fails. |
| 1014 | */ |
| 1015 | bool MakeExternal(ExternalAsciiStringResource* resource); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1016 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1017 | /** |
| 1018 | * Returns true if this string can be made external. |
| 1019 | */ |
| 1020 | bool CanMakeExternal(); |
| 1021 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1022 | /** 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] | 1023 | static Local<String> NewUndetectable(const char* data, int length = -1); |
| 1024 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1025 | /** Creates an undetectable string from the supplied utf-16 data.*/ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1026 | static Local<String> NewUndetectable(const uint16_t* data, int length = -1); |
| 1027 | |
| 1028 | /** |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1029 | * Converts an object to a utf8-encoded character array. Useful if |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 1030 | * you want to print the object. If conversion to a string fails |
| 1031 | * (eg. due to an exception in the toString() method of the object) |
| 1032 | * then the length() method returns 0 and the * operator returns |
| 1033 | * NULL. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1034 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1035 | class V8EXPORT Utf8Value { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1036 | public: |
| 1037 | explicit Utf8Value(Handle<v8::Value> obj); |
| 1038 | ~Utf8Value(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1039 | char* operator*() { return str_; } |
| 1040 | const char* operator*() const { return str_; } |
| 1041 | int length() const { return length_; } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1042 | private: |
| 1043 | char* str_; |
| 1044 | int length_; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1045 | |
| 1046 | // Disallow copying and assigning. |
| 1047 | Utf8Value(const Utf8Value&); |
| 1048 | void operator=(const Utf8Value&); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1049 | }; |
| 1050 | |
| 1051 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1052 | * Converts an object to an ascii string. |
| 1053 | * Useful if you want to print the object. |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 1054 | * If conversion to a string fails (eg. due to an exception in the toString() |
| 1055 | * method of the object) then the length() method returns 0 and the * operator |
| 1056 | * returns NULL. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1057 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1058 | class V8EXPORT AsciiValue { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1059 | public: |
| 1060 | explicit AsciiValue(Handle<v8::Value> obj); |
| 1061 | ~AsciiValue(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1062 | char* operator*() { return str_; } |
| 1063 | const char* operator*() const { return str_; } |
| 1064 | int length() const { return length_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1065 | private: |
| 1066 | char* str_; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1067 | int length_; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1068 | |
| 1069 | // Disallow copying and assigning. |
| 1070 | AsciiValue(const AsciiValue&); |
| 1071 | void operator=(const AsciiValue&); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1072 | }; |
| 1073 | |
| 1074 | /** |
| 1075 | * Converts an object to a two-byte string. |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 1076 | * If conversion to a string fails (eg. due to an exception in the toString() |
| 1077 | * method of the object) then the length() method returns 0 and the * operator |
| 1078 | * returns NULL. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1079 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1080 | class V8EXPORT Value { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1081 | public: |
| 1082 | explicit Value(Handle<v8::Value> obj); |
| 1083 | ~Value(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1084 | uint16_t* operator*() { return str_; } |
| 1085 | const uint16_t* operator*() const { return str_; } |
| 1086 | int length() const { return length_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1087 | private: |
| 1088 | uint16_t* str_; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1089 | int length_; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1090 | |
| 1091 | // Disallow copying and assigning. |
| 1092 | Value(const Value&); |
| 1093 | void operator=(const Value&); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1094 | }; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1095 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1096 | private: |
| 1097 | void VerifyExternalStringResource(ExternalStringResource* val) const; |
| 1098 | static void CheckCast(v8::Value* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1099 | }; |
| 1100 | |
| 1101 | |
| 1102 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1103 | * A JavaScript number value (ECMA-262, 4.3.20) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1104 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1105 | class V8EXPORT Number : public Primitive { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1106 | public: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1107 | double Value() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1108 | static Local<Number> New(double value); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1109 | static inline Number* Cast(v8::Value* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1110 | private: |
| 1111 | Number(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1112 | static void CheckCast(v8::Value* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1113 | }; |
| 1114 | |
| 1115 | |
| 1116 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1117 | * A JavaScript value representing a signed integer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1118 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1119 | class V8EXPORT Integer : public Number { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1120 | public: |
| 1121 | static Local<Integer> New(int32_t value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1122 | static Local<Integer> NewFromUnsigned(uint32_t value); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1123 | int64_t Value() const; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1124 | static inline Integer* Cast(v8::Value* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1125 | private: |
| 1126 | Integer(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1127 | static void CheckCast(v8::Value* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1128 | }; |
| 1129 | |
| 1130 | |
| 1131 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1132 | * A JavaScript value representing a 32-bit signed integer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1133 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1134 | class V8EXPORT Int32 : public Integer { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1135 | public: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1136 | int32_t Value() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1137 | private: |
| 1138 | Int32(); |
| 1139 | }; |
| 1140 | |
| 1141 | |
| 1142 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1143 | * A JavaScript value representing a 32-bit unsigned integer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1144 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1145 | class V8EXPORT Uint32 : public Integer { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1146 | public: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1147 | uint32_t Value() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1148 | private: |
| 1149 | Uint32(); |
| 1150 | }; |
| 1151 | |
| 1152 | |
| 1153 | /** |
| 1154 | * An instance of the built-in Date constructor (ECMA-262, 15.9). |
| 1155 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1156 | class V8EXPORT Date : public Value { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1157 | public: |
| 1158 | static Local<Value> New(double time); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1159 | |
| 1160 | /** |
| 1161 | * A specialization of Value::NumberValue that is more efficient |
| 1162 | * because we know the structure of this object. |
| 1163 | */ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1164 | double NumberValue() const; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1165 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1166 | static inline Date* Cast(v8::Value* obj); |
| 1167 | private: |
| 1168 | static void CheckCast(v8::Value* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1169 | }; |
| 1170 | |
| 1171 | |
| 1172 | enum PropertyAttribute { |
| 1173 | None = 0, |
| 1174 | ReadOnly = 1 << 0, |
| 1175 | DontEnum = 1 << 1, |
| 1176 | DontDelete = 1 << 2 |
| 1177 | }; |
| 1178 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1179 | enum ExternalArrayType { |
| 1180 | kExternalByteArray = 1, |
| 1181 | kExternalUnsignedByteArray, |
| 1182 | kExternalShortArray, |
| 1183 | kExternalUnsignedShortArray, |
| 1184 | kExternalIntArray, |
| 1185 | kExternalUnsignedIntArray, |
| 1186 | kExternalFloatArray |
| 1187 | }; |
| 1188 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1189 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1190 | * A JavaScript object (ECMA-262, 4.3.3) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1191 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1192 | class V8EXPORT Object : public Value { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1193 | public: |
| 1194 | bool Set(Handle<Value> key, |
| 1195 | Handle<Value> value, |
| 1196 | PropertyAttribute attribs = None); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1197 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame^] | 1198 | bool Set(uint32_t index, |
| 1199 | Handle<Value> value); |
| 1200 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1201 | // Sets a local property on this object bypassing interceptors and |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1202 | // overriding accessors or read-only properties. |
| 1203 | // |
| 1204 | // Note that if the object has an interceptor the property will be set |
| 1205 | // locally, but since the interceptor takes precedence the local property |
| 1206 | // will only be returned if the interceptor doesn't return a value. |
| 1207 | // |
| 1208 | // Note also that this only works for named properties. |
| 1209 | bool ForceSet(Handle<Value> key, |
| 1210 | Handle<Value> value, |
| 1211 | PropertyAttribute attribs = None); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1212 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1213 | Local<Value> Get(Handle<Value> key); |
| 1214 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame^] | 1215 | Local<Value> Get(uint32_t index); |
| 1216 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1217 | // TODO(1245389): Replace the type-specific versions of these |
| 1218 | // functions with generic ones that accept a Handle<Value> key. |
| 1219 | bool Has(Handle<String> key); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1220 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1221 | bool Delete(Handle<String> key); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1222 | |
| 1223 | // Delete a property on this object bypassing interceptors and |
| 1224 | // ignoring dont-delete attributes. |
| 1225 | bool ForceDelete(Handle<Value> key); |
| 1226 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1227 | bool Has(uint32_t index); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1228 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1229 | bool Delete(uint32_t index); |
| 1230 | |
| 1231 | /** |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1232 | * Returns an array containing the names of the enumerable properties |
| 1233 | * of this object, including properties from prototype objects. The |
| 1234 | * array returned by this method contains the same values as would |
| 1235 | * be enumerated by a for-in statement over this object. |
| 1236 | */ |
| 1237 | Local<Array> GetPropertyNames(); |
| 1238 | |
| 1239 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1240 | * Get the prototype object. This does not skip objects marked to |
| 1241 | * be skipped by __proto__ and it does not consult the security |
| 1242 | * handler. |
| 1243 | */ |
| 1244 | Local<Value> GetPrototype(); |
| 1245 | |
| 1246 | /** |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1247 | * Set the prototype object. This does not skip objects marked to |
| 1248 | * be skipped by __proto__ and it does not consult the security |
| 1249 | * handler. |
| 1250 | */ |
| 1251 | bool SetPrototype(Handle<Value> prototype); |
| 1252 | |
| 1253 | /** |
sgjesse@chromium.org | 900d3b7 | 2009-08-07 11:24:25 +0000 | [diff] [blame] | 1254 | * Finds an instance of the given function template in the prototype |
| 1255 | * chain. |
| 1256 | */ |
| 1257 | Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl); |
| 1258 | |
| 1259 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1260 | * Call builtin Object.prototype.toString on this object. |
| 1261 | * This is different from Value::ToString() that may call |
| 1262 | * user-defined toString function. This one does not. |
| 1263 | */ |
| 1264 | Local<String> ObjectProtoToString(); |
| 1265 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1266 | /** Gets the number of internal fields for this Object. */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1267 | int InternalFieldCount(); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1268 | /** Gets the value in an internal field. */ |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1269 | inline Local<Value> GetInternalField(int index); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1270 | /** Sets the value in an internal field. */ |
| 1271 | void SetInternalField(int index, Handle<Value> value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1272 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 1273 | /** Gets a native pointer from an internal field. */ |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1274 | inline void* GetPointerFromInternalField(int index); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1275 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 1276 | /** Sets a native pointer in an internal field. */ |
| 1277 | void SetPointerInInternalField(int index, void* value); |
| 1278 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1279 | // Testers for local properties. |
| 1280 | bool HasRealNamedProperty(Handle<String> key); |
| 1281 | bool HasRealIndexedProperty(uint32_t index); |
| 1282 | bool HasRealNamedCallbackProperty(Handle<String> key); |
| 1283 | |
| 1284 | /** |
| 1285 | * If result.IsEmpty() no real property was located in the prototype chain. |
| 1286 | * This means interceptors in the prototype chain are not called. |
| 1287 | */ |
sgjesse@chromium.org | 98aff2f | 2009-09-30 08:27:10 +0000 | [diff] [blame] | 1288 | Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key); |
| 1289 | |
| 1290 | /** |
| 1291 | * If result.IsEmpty() no real property was located on the object or |
| 1292 | * in the prototype chain. |
| 1293 | * This means interceptors in the prototype chain are not called. |
| 1294 | */ |
| 1295 | Local<Value> GetRealNamedProperty(Handle<String> key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1296 | |
| 1297 | /** Tests for a named lookup interceptor.*/ |
| 1298 | bool HasNamedLookupInterceptor(); |
| 1299 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1300 | /** Tests for an index lookup interceptor.*/ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1301 | bool HasIndexedLookupInterceptor(); |
| 1302 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1303 | /** |
| 1304 | * Turns on access check on the object if the object is an instance of |
| 1305 | * a template that has access check callbacks. If an object has no |
| 1306 | * access check info, the object cannot be accessed by anyone. |
| 1307 | */ |
| 1308 | void TurnOnAccessCheck(); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 1309 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 1310 | /** |
| 1311 | * Returns the identity hash for this object. The current implemenation uses |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1312 | * a hidden property on the object to store the identity hash. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 1313 | * |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1314 | * 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] | 1315 | * unique. |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 1316 | */ |
| 1317 | int GetIdentityHash(); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 1318 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 1319 | /** |
| 1320 | * Access hidden properties on JavaScript objects. These properties are |
| 1321 | * hidden from the executing JavaScript and only accessible through the V8 |
| 1322 | * C++ API. Hidden properties introduced by V8 internally (for example the |
| 1323 | * identity hash) are prefixed with "v8::". |
| 1324 | */ |
| 1325 | bool SetHiddenValue(Handle<String> key, Handle<Value> value); |
| 1326 | Local<Value> GetHiddenValue(Handle<String> key); |
| 1327 | bool DeleteHiddenValue(Handle<String> key); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1328 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 1329 | /** |
| 1330 | * Returns true if this is an instance of an api function (one |
| 1331 | * created from a function created from a function template) and has |
| 1332 | * been modified since it was created. Note that this method is |
| 1333 | * conservative and may return true for objects that haven't actually |
| 1334 | * been modified. |
| 1335 | */ |
| 1336 | bool IsDirty(); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 1337 | |
| 1338 | /** |
| 1339 | * Clone this object with a fast but shallow copy. Values will point |
| 1340 | * to the same values as the original object. |
| 1341 | */ |
| 1342 | Local<Object> Clone(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1343 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1344 | /** |
| 1345 | * Set the backing store of the indexed properties to be managed by the |
| 1346 | * embedding layer. Access to the indexed properties will follow the rules |
| 1347 | * spelled out in CanvasPixelArray. |
| 1348 | * Note: The embedding program still owns the data and needs to ensure that |
| 1349 | * the backing store is preserved while V8 has a reference. |
| 1350 | */ |
| 1351 | void SetIndexedPropertiesToPixelData(uint8_t* data, int length); |
| 1352 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1353 | /** |
| 1354 | * Set the backing store of the indexed properties to be managed by the |
| 1355 | * embedding layer. Access to the indexed properties will follow the rules |
| 1356 | * spelled out for the CanvasArray subtypes in the WebGL specification. |
| 1357 | * Note: The embedding program still owns the data and needs to ensure that |
| 1358 | * the backing store is preserved while V8 has a reference. |
| 1359 | */ |
| 1360 | void SetIndexedPropertiesToExternalArrayData(void* data, |
| 1361 | ExternalArrayType array_type, |
| 1362 | int number_of_elements); |
| 1363 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1364 | static Local<Object> New(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1365 | static inline Object* Cast(Value* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1366 | private: |
| 1367 | Object(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1368 | static void CheckCast(Value* obj); |
| 1369 | Local<Value> CheckedGetInternalField(int index); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1370 | void* SlowGetPointerFromInternalField(int index); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1371 | |
| 1372 | /** |
| 1373 | * If quick access to the internal field is possible this method |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1374 | * returns the value. Otherwise an empty handle is returned. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1375 | */ |
| 1376 | inline Local<Value> UncheckedGetInternalField(int index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1377 | }; |
| 1378 | |
| 1379 | |
| 1380 | /** |
| 1381 | * An instance of the built-in array constructor (ECMA-262, 15.4.2). |
| 1382 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1383 | class V8EXPORT Array : public Object { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1384 | public: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1385 | uint32_t Length() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1386 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 1387 | /** |
| 1388 | * Clones an element at index |index|. Returns an empty |
| 1389 | * handle if cloning fails (for any reason). |
| 1390 | */ |
| 1391 | Local<Object> CloneElementAt(uint32_t index); |
| 1392 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1393 | static Local<Array> New(int length = 0); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1394 | static inline Array* Cast(Value* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1395 | private: |
| 1396 | Array(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1397 | static void CheckCast(Value* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1398 | }; |
| 1399 | |
| 1400 | |
| 1401 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1402 | * A JavaScript function object (ECMA-262, 15.3). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1403 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1404 | class V8EXPORT Function : public Object { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1405 | public: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1406 | Local<Object> NewInstance() const; |
| 1407 | Local<Object> NewInstance(int argc, Handle<Value> argv[]) const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1408 | Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]); |
| 1409 | void SetName(Handle<String> name); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1410 | Handle<Value> GetName() const; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1411 | |
| 1412 | /** |
| 1413 | * Returns zero based line number of function body and |
| 1414 | * kLineOffsetNotFound if no information available. |
| 1415 | */ |
| 1416 | int GetScriptLineNumber() const; |
| 1417 | ScriptOrigin GetScriptOrigin() const; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1418 | static inline Function* Cast(Value* obj); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1419 | static const int kLineOffsetNotFound; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1420 | private: |
| 1421 | Function(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1422 | static void CheckCast(Value* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1423 | }; |
| 1424 | |
| 1425 | |
| 1426 | /** |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1427 | * A JavaScript value that wraps a C++ void*. This type of value is |
| 1428 | * mainly used to associate C++ data structures with JavaScript |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1429 | * objects. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1430 | * |
| 1431 | * The Wrap function V8 will return the most optimal Value object wrapping the |
| 1432 | * C++ void*. The type of the value is not guaranteed to be an External object |
| 1433 | * and no assumptions about its type should be made. To access the wrapped |
| 1434 | * value Unwrap should be used, all other operations on that object will lead |
| 1435 | * to unpredictable results. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1436 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1437 | class V8EXPORT External : public Value { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1438 | public: |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1439 | static Local<Value> Wrap(void* data); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1440 | static inline void* Unwrap(Handle<Value> obj); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1441 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1442 | static Local<External> New(void* value); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1443 | static inline External* Cast(Value* obj); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1444 | void* Value() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1445 | private: |
| 1446 | External(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1447 | static void CheckCast(v8::Value* obj); |
| 1448 | static inline void* QuickUnwrap(Handle<v8::Value> obj); |
| 1449 | static void* FullUnwrap(Handle<v8::Value> obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1450 | }; |
| 1451 | |
| 1452 | |
| 1453 | // --- T e m p l a t e s --- |
| 1454 | |
| 1455 | |
| 1456 | /** |
| 1457 | * The superclass of object and function templates. |
| 1458 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1459 | class V8EXPORT Template : public Data { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1460 | public: |
| 1461 | /** Adds a property to each instance created by this template.*/ |
| 1462 | void Set(Handle<String> name, Handle<Data> value, |
| 1463 | PropertyAttribute attributes = None); |
| 1464 | inline void Set(const char* name, Handle<Data> value); |
| 1465 | private: |
| 1466 | Template(); |
| 1467 | |
| 1468 | friend class ObjectTemplate; |
| 1469 | friend class FunctionTemplate; |
| 1470 | }; |
| 1471 | |
| 1472 | |
| 1473 | /** |
| 1474 | * The argument information given to function call callbacks. This |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1475 | * class provides access to information about the context of the call, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1476 | * including the receiver, the number and values of arguments, and |
| 1477 | * the holder of the function. |
| 1478 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1479 | class V8EXPORT Arguments { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1480 | public: |
| 1481 | inline int Length() const; |
| 1482 | inline Local<Value> operator[](int i) const; |
| 1483 | inline Local<Function> Callee() const; |
| 1484 | inline Local<Object> This() const; |
| 1485 | inline Local<Object> Holder() const; |
| 1486 | inline bool IsConstructCall() const; |
| 1487 | inline Local<Value> Data() const; |
| 1488 | private: |
| 1489 | Arguments(); |
| 1490 | friend class ImplementationUtilities; |
| 1491 | inline Arguments(Local<Value> data, |
| 1492 | Local<Object> holder, |
| 1493 | Local<Function> callee, |
| 1494 | bool is_construct_call, |
| 1495 | void** values, int length); |
| 1496 | Local<Value> data_; |
| 1497 | Local<Object> holder_; |
| 1498 | Local<Function> callee_; |
| 1499 | bool is_construct_call_; |
| 1500 | void** values_; |
| 1501 | int length_; |
| 1502 | }; |
| 1503 | |
| 1504 | |
| 1505 | /** |
| 1506 | * The information passed to an accessor callback about the context |
| 1507 | * of the property access. |
| 1508 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1509 | class V8EXPORT AccessorInfo { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1510 | public: |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1511 | inline AccessorInfo(internal::Object** args) |
| 1512 | : args_(args) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1513 | inline Local<Value> Data() const; |
| 1514 | inline Local<Object> This() const; |
| 1515 | inline Local<Object> Holder() const; |
| 1516 | private: |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1517 | internal::Object** args_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1518 | }; |
| 1519 | |
| 1520 | |
| 1521 | typedef Handle<Value> (*InvocationCallback)(const Arguments& args); |
| 1522 | |
| 1523 | typedef int (*LookupCallback)(Local<Object> self, Local<String> name); |
| 1524 | |
| 1525 | /** |
| 1526 | * Accessor[Getter|Setter] are used as callback functions when |
| 1527 | * setting|getting a particular property. See objectTemplate::SetAccessor. |
| 1528 | */ |
| 1529 | typedef Handle<Value> (*AccessorGetter)(Local<String> property, |
| 1530 | const AccessorInfo& info); |
| 1531 | |
| 1532 | |
| 1533 | typedef void (*AccessorSetter)(Local<String> property, |
| 1534 | Local<Value> value, |
| 1535 | const AccessorInfo& info); |
| 1536 | |
| 1537 | |
| 1538 | /** |
| 1539 | * NamedProperty[Getter|Setter] are used as interceptors on object. |
| 1540 | * See ObjectTemplate::SetNamedPropertyHandler. |
| 1541 | */ |
| 1542 | typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property, |
| 1543 | const AccessorInfo& info); |
| 1544 | |
| 1545 | |
| 1546 | /** |
| 1547 | * Returns the value if the setter intercepts the request. |
| 1548 | * Otherwise, returns an empty handle. |
| 1549 | */ |
| 1550 | typedef Handle<Value> (*NamedPropertySetter)(Local<String> property, |
| 1551 | Local<Value> value, |
| 1552 | const AccessorInfo& info); |
| 1553 | |
| 1554 | |
| 1555 | /** |
| 1556 | * Returns a non-empty handle if the interceptor intercepts the request. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1557 | * The result is true if the property exists and false otherwise. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1558 | */ |
| 1559 | typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property, |
| 1560 | const AccessorInfo& info); |
| 1561 | |
| 1562 | |
| 1563 | /** |
| 1564 | * Returns a non-empty handle if the deleter intercepts the request. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1565 | * The return value is true if the property could be deleted and false |
| 1566 | * otherwise. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1567 | */ |
| 1568 | typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property, |
| 1569 | const AccessorInfo& info); |
| 1570 | |
| 1571 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1572 | * Returns an array containing the names of the properties the named |
| 1573 | * property getter intercepts. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1574 | */ |
| 1575 | typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info); |
| 1576 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1577 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1578 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1579 | * Returns the value of the property if the getter intercepts the |
| 1580 | * request. Otherwise, returns an empty handle. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1581 | */ |
| 1582 | typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index, |
| 1583 | const AccessorInfo& info); |
| 1584 | |
| 1585 | |
| 1586 | /** |
| 1587 | * Returns the value if the setter intercepts the request. |
| 1588 | * Otherwise, returns an empty handle. |
| 1589 | */ |
| 1590 | typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index, |
| 1591 | Local<Value> value, |
| 1592 | const AccessorInfo& info); |
| 1593 | |
| 1594 | |
| 1595 | /** |
| 1596 | * Returns a non-empty handle if the interceptor intercepts the request. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1597 | * The result is true if the property exists and false otherwise. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1598 | */ |
| 1599 | typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index, |
| 1600 | const AccessorInfo& info); |
| 1601 | |
| 1602 | /** |
| 1603 | * Returns a non-empty handle if the deleter intercepts the request. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1604 | * The return value is true if the property could be deleted and false |
| 1605 | * otherwise. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1606 | */ |
| 1607 | typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index, |
| 1608 | const AccessorInfo& info); |
| 1609 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1610 | /** |
| 1611 | * Returns an array containing the indices of the properties the |
| 1612 | * indexed property getter intercepts. |
| 1613 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1614 | typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info); |
| 1615 | |
| 1616 | |
| 1617 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1618 | * Access control specifications. |
| 1619 | * |
| 1620 | * Some accessors should be accessible across contexts. These |
| 1621 | * accessors have an explicit access control parameter which specifies |
| 1622 | * the kind of cross-context access that should be allowed. |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1623 | * |
| 1624 | * Additionally, for security, accessors can prohibit overwriting by |
| 1625 | * accessors defined in JavaScript. For objects that have such |
| 1626 | * accessors either locally or in their prototype chain it is not |
| 1627 | * possible to overwrite the accessor by using __defineGetter__ or |
| 1628 | * __defineSetter__ from JavaScript code. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1629 | */ |
| 1630 | enum AccessControl { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1631 | DEFAULT = 0, |
| 1632 | ALL_CAN_READ = 1, |
| 1633 | ALL_CAN_WRITE = 1 << 1, |
| 1634 | PROHIBITS_OVERWRITING = 1 << 2 |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1635 | }; |
| 1636 | |
| 1637 | |
| 1638 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1639 | * Access type specification. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1640 | */ |
| 1641 | enum AccessType { |
| 1642 | ACCESS_GET, |
| 1643 | ACCESS_SET, |
| 1644 | ACCESS_HAS, |
| 1645 | ACCESS_DELETE, |
| 1646 | ACCESS_KEYS |
| 1647 | }; |
| 1648 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1649 | |
| 1650 | /** |
| 1651 | * Returns true if cross-context access should be allowed to the named |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 1652 | * property with the given key on the host object. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1653 | */ |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 1654 | typedef bool (*NamedSecurityCallback)(Local<Object> host, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1655 | Local<Value> key, |
| 1656 | AccessType type, |
| 1657 | Local<Value> data); |
| 1658 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1659 | |
| 1660 | /** |
| 1661 | * Returns true if cross-context access should be allowed to the indexed |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 1662 | * property with the given index on the host object. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1663 | */ |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 1664 | typedef bool (*IndexedSecurityCallback)(Local<Object> host, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1665 | uint32_t index, |
| 1666 | AccessType type, |
| 1667 | Local<Value> data); |
| 1668 | |
| 1669 | |
| 1670 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1671 | * A FunctionTemplate is used to create functions at runtime. There |
| 1672 | * can only be one function created from a FunctionTemplate in a |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1673 | * context. The lifetime of the created function is equal to the |
| 1674 | * lifetime of the context. So in case the embedder needs to create |
| 1675 | * temporary functions that can be collected using Scripts is |
| 1676 | * preferred. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1677 | * |
| 1678 | * A FunctionTemplate can have properties, these properties are added to the |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1679 | * function object when it is created. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1680 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1681 | * A FunctionTemplate has a corresponding instance template which is |
| 1682 | * used to create object instances when the function is used as a |
| 1683 | * constructor. Properties added to the instance template are added to |
| 1684 | * each object instance. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1685 | * |
| 1686 | * A FunctionTemplate can have a prototype template. The prototype template |
| 1687 | * is used to create the prototype object of the function. |
| 1688 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1689 | * The following example shows how to use a FunctionTemplate: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1690 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1691 | * \code |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1692 | * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 1693 | * t->Set("func_property", v8::Number::New(1)); |
| 1694 | * |
| 1695 | * v8::Local<v8::Template> proto_t = t->PrototypeTemplate(); |
| 1696 | * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback)); |
| 1697 | * proto_t->Set("proto_const", v8::Number::New(2)); |
| 1698 | * |
| 1699 | * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate(); |
| 1700 | * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback); |
| 1701 | * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...); |
| 1702 | * instance_t->Set("instance_property", Number::New(3)); |
| 1703 | * |
| 1704 | * v8::Local<v8::Function> function = t->GetFunction(); |
| 1705 | * v8::Local<v8::Object> instance = function->NewInstance(); |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1706 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1707 | * |
| 1708 | * 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] | 1709 | * and "instance" for the instance object created above. The function |
| 1710 | * and the instance will have the following properties: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1711 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1712 | * \code |
| 1713 | * func_property in function == true; |
| 1714 | * function.func_property == 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1715 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1716 | * function.prototype.proto_method() invokes 'InvokeCallback' |
| 1717 | * function.prototype.proto_const == 2; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1718 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1719 | * instance instanceof function == true; |
| 1720 | * instance.instance_accessor calls 'InstanceAccessorCallback' |
| 1721 | * instance.instance_property == 3; |
| 1722 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1723 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1724 | * A FunctionTemplate can inherit from another one by calling the |
| 1725 | * FunctionTemplate::Inherit method. The following graph illustrates |
| 1726 | * the semantics of inheritance: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1727 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1728 | * \code |
| 1729 | * FunctionTemplate Parent -> Parent() . prototype -> { } |
| 1730 | * ^ ^ |
| 1731 | * | Inherit(Parent) | .__proto__ |
| 1732 | * | | |
| 1733 | * FunctionTemplate Child -> Child() . prototype -> { } |
| 1734 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1735 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1736 | * A FunctionTemplate 'Child' inherits from 'Parent', the prototype |
| 1737 | * object of the Child() function has __proto__ pointing to the |
| 1738 | * Parent() function's prototype object. An instance of the Child |
| 1739 | * function has all properties on Parent's instance templates. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1740 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1741 | * Let Parent be the FunctionTemplate initialized in the previous |
| 1742 | * section and create a Child FunctionTemplate by: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1743 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1744 | * \code |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1745 | * Local<FunctionTemplate> parent = t; |
| 1746 | * Local<FunctionTemplate> child = FunctionTemplate::New(); |
| 1747 | * child->Inherit(parent); |
| 1748 | * |
| 1749 | * Local<Function> child_function = child->GetFunction(); |
| 1750 | * Local<Object> child_instance = child_function->NewInstance(); |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1751 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1752 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1753 | * The Child function and Child instance will have the following |
| 1754 | * properties: |
| 1755 | * |
| 1756 | * \code |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1757 | * child_func.prototype.__proto__ == function.prototype; |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1758 | * child_instance.instance_accessor calls 'InstanceAccessorCallback' |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1759 | * child_instance.instance_property == 3; |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1760 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1761 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1762 | class V8EXPORT FunctionTemplate : public Template { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1763 | public: |
| 1764 | /** Creates a function template.*/ |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1765 | static Local<FunctionTemplate> New( |
| 1766 | InvocationCallback callback = 0, |
| 1767 | Handle<Value> data = Handle<Value>(), |
| 1768 | Handle<Signature> signature = Handle<Signature>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1769 | /** Returns the unique function instance in the current execution context.*/ |
| 1770 | Local<Function> GetFunction(); |
| 1771 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1772 | /** |
| 1773 | * Set the call-handler callback for a FunctionTemplate. This |
| 1774 | * callback is called whenever the function created from this |
| 1775 | * FunctionTemplate is called. |
| 1776 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1777 | void SetCallHandler(InvocationCallback callback, |
| 1778 | Handle<Value> data = Handle<Value>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1779 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1780 | /** Get the InstanceTemplate. */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1781 | Local<ObjectTemplate> InstanceTemplate(); |
| 1782 | |
| 1783 | /** Causes the function template to inherit from a parent function template.*/ |
| 1784 | void Inherit(Handle<FunctionTemplate> parent); |
| 1785 | |
| 1786 | /** |
| 1787 | * A PrototypeTemplate is the template used to create the prototype object |
| 1788 | * of the function created by this template. |
| 1789 | */ |
| 1790 | Local<ObjectTemplate> PrototypeTemplate(); |
| 1791 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1792 | |
| 1793 | /** |
| 1794 | * Set the class name of the FunctionTemplate. This is used for |
| 1795 | * printing objects created with the function created from the |
| 1796 | * FunctionTemplate as its constructor. |
| 1797 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1798 | void SetClassName(Handle<String> name); |
| 1799 | |
| 1800 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1801 | * Determines whether the __proto__ accessor ignores instances of |
| 1802 | * the function template. If instances of the function template are |
| 1803 | * ignored, __proto__ skips all instances and instead returns the |
| 1804 | * next object in the prototype chain. |
| 1805 | * |
| 1806 | * Call with a value of true to make the __proto__ accessor ignore |
| 1807 | * instances of the function template. Call with a value of false |
| 1808 | * to make the __proto__ accessor not ignore instances of the |
| 1809 | * function template. By default, instances of a function template |
| 1810 | * are not ignored. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1811 | */ |
| 1812 | void SetHiddenPrototype(bool value); |
| 1813 | |
| 1814 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1815 | * Returns true if the given object is an instance of this function |
| 1816 | * template. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1817 | */ |
| 1818 | bool HasInstance(Handle<Value> object); |
| 1819 | |
| 1820 | private: |
| 1821 | FunctionTemplate(); |
| 1822 | void AddInstancePropertyAccessor(Handle<String> name, |
| 1823 | AccessorGetter getter, |
| 1824 | AccessorSetter setter, |
| 1825 | Handle<Value> data, |
| 1826 | AccessControl settings, |
| 1827 | PropertyAttribute attributes); |
| 1828 | void SetNamedInstancePropertyHandler(NamedPropertyGetter getter, |
| 1829 | NamedPropertySetter setter, |
| 1830 | NamedPropertyQuery query, |
| 1831 | NamedPropertyDeleter remover, |
| 1832 | NamedPropertyEnumerator enumerator, |
| 1833 | Handle<Value> data); |
| 1834 | void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter, |
| 1835 | IndexedPropertySetter setter, |
| 1836 | IndexedPropertyQuery query, |
| 1837 | IndexedPropertyDeleter remover, |
| 1838 | IndexedPropertyEnumerator enumerator, |
| 1839 | Handle<Value> data); |
| 1840 | void SetInstanceCallAsFunctionHandler(InvocationCallback callback, |
| 1841 | Handle<Value> data); |
| 1842 | |
| 1843 | friend class Context; |
| 1844 | friend class ObjectTemplate; |
| 1845 | }; |
| 1846 | |
| 1847 | |
| 1848 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1849 | * An ObjectTemplate is used to create objects at runtime. |
| 1850 | * |
| 1851 | * Properties added to an ObjectTemplate are added to each object |
| 1852 | * created from the ObjectTemplate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1853 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1854 | class V8EXPORT ObjectTemplate : public Template { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1855 | public: |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1856 | /** Creates an ObjectTemplate. */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1857 | static Local<ObjectTemplate> New(); |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1858 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1859 | /** Creates a new instance of this template.*/ |
| 1860 | Local<Object> NewInstance(); |
| 1861 | |
| 1862 | /** |
| 1863 | * Sets an accessor on the object template. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1864 | * |
| 1865 | * Whenever the property with the given name is accessed on objects |
| 1866 | * created from this ObjectTemplate the getter and setter callbacks |
| 1867 | * are called instead of getting and setting the property directly |
| 1868 | * on the JavaScript object. |
| 1869 | * |
| 1870 | * \param name The name of the property for which an accessor is added. |
| 1871 | * \param getter The callback to invoke when getting the property. |
| 1872 | * \param setter The callback to invoke when setting the property. |
| 1873 | * \param data A piece of data that will be passed to the getter and setter |
| 1874 | * callbacks whenever they are invoked. |
| 1875 | * \param settings Access control settings for the accessor. This is a bit |
| 1876 | * field consisting of one of more of |
| 1877 | * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2. |
| 1878 | * The default is to not allow cross-context access. |
| 1879 | * ALL_CAN_READ means that all cross-context reads are allowed. |
| 1880 | * ALL_CAN_WRITE means that all cross-context writes are allowed. |
| 1881 | * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all |
| 1882 | * cross-context access. |
| 1883 | * \param attribute The attributes of the property for which an accessor |
| 1884 | * is added. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1885 | */ |
| 1886 | void SetAccessor(Handle<String> name, |
| 1887 | AccessorGetter getter, |
| 1888 | AccessorSetter setter = 0, |
| 1889 | Handle<Value> data = Handle<Value>(), |
| 1890 | AccessControl settings = DEFAULT, |
| 1891 | PropertyAttribute attribute = None); |
| 1892 | |
| 1893 | /** |
| 1894 | * Sets a named property handler on the object template. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1895 | * |
| 1896 | * Whenever a named property is accessed on objects created from |
| 1897 | * this object template, the provided callback is invoked instead of |
| 1898 | * accessing the property directly on the JavaScript object. |
| 1899 | * |
| 1900 | * \param getter The callback to invoke when getting a property. |
| 1901 | * \param setter The callback to invoke when setting a property. |
| 1902 | * \param query The callback to invoke to check is an object has a property. |
| 1903 | * \param deleter The callback to invoke when deleting a property. |
| 1904 | * \param enumerator The callback to invoke to enumerate all the named |
| 1905 | * properties of an object. |
| 1906 | * \param data A piece of data that will be passed to the callbacks |
| 1907 | * whenever they are invoked. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1908 | */ |
| 1909 | void SetNamedPropertyHandler(NamedPropertyGetter getter, |
| 1910 | NamedPropertySetter setter = 0, |
| 1911 | NamedPropertyQuery query = 0, |
| 1912 | NamedPropertyDeleter deleter = 0, |
| 1913 | NamedPropertyEnumerator enumerator = 0, |
| 1914 | Handle<Value> data = Handle<Value>()); |
| 1915 | |
| 1916 | /** |
| 1917 | * Sets an indexed property handler on the object template. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1918 | * |
| 1919 | * Whenever an indexed property is accessed on objects created from |
| 1920 | * this object template, the provided callback is invoked instead of |
| 1921 | * accessing the property directly on the JavaScript object. |
| 1922 | * |
| 1923 | * \param getter The callback to invoke when getting a property. |
| 1924 | * \param setter The callback to invoke when setting a property. |
| 1925 | * \param query The callback to invoke to check is an object has a property. |
| 1926 | * \param deleter The callback to invoke when deleting a property. |
| 1927 | * \param enumerator The callback to invoke to enumerate all the indexed |
| 1928 | * properties of an object. |
| 1929 | * \param data A piece of data that will be passed to the callbacks |
| 1930 | * whenever they are invoked. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1931 | */ |
| 1932 | void SetIndexedPropertyHandler(IndexedPropertyGetter getter, |
| 1933 | IndexedPropertySetter setter = 0, |
| 1934 | IndexedPropertyQuery query = 0, |
| 1935 | IndexedPropertyDeleter deleter = 0, |
| 1936 | IndexedPropertyEnumerator enumerator = 0, |
| 1937 | Handle<Value> data = Handle<Value>()); |
| 1938 | /** |
| 1939 | * Sets the callback to be used when calling instances created from |
| 1940 | * this template as a function. If no callback is set, instances |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1941 | * behave like normal JavaScript objects that cannot be called as a |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1942 | * function. |
| 1943 | */ |
| 1944 | void SetCallAsFunctionHandler(InvocationCallback callback, |
| 1945 | Handle<Value> data = Handle<Value>()); |
| 1946 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1947 | /** |
| 1948 | * Mark object instances of the template as undetectable. |
| 1949 | * |
| 1950 | * In many ways, undetectable objects behave as though they are not |
| 1951 | * there. They behave like 'undefined' in conditionals and when |
| 1952 | * printed. However, properties can be accessed and called as on |
| 1953 | * normal objects. |
| 1954 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1955 | void MarkAsUndetectable(); |
| 1956 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1957 | /** |
| 1958 | * Sets access check callbacks on the object template. |
| 1959 | * |
| 1960 | * When accessing properties on instances of this object template, |
| 1961 | * the access check callback will be called to determine whether or |
| 1962 | * not to allow cross-context access to the properties. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1963 | * The last parameter specifies whether access checks are turned |
| 1964 | * on by default on instances. If access checks are off by default, |
| 1965 | * they can be turned on on individual instances by calling |
| 1966 | * Object::TurnOnAccessCheck(). |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1967 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1968 | void SetAccessCheckCallbacks(NamedSecurityCallback named_handler, |
| 1969 | IndexedSecurityCallback indexed_handler, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1970 | Handle<Value> data = Handle<Value>(), |
| 1971 | bool turned_on_by_default = true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1972 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1973 | /** |
| 1974 | * Gets the number of internal fields for objects generated from |
| 1975 | * this template. |
| 1976 | */ |
| 1977 | int InternalFieldCount(); |
| 1978 | |
| 1979 | /** |
| 1980 | * Sets the number of internal fields for objects generated from |
| 1981 | * this template. |
| 1982 | */ |
| 1983 | void SetInternalFieldCount(int value); |
| 1984 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1985 | private: |
| 1986 | ObjectTemplate(); |
| 1987 | static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor); |
| 1988 | friend class FunctionTemplate; |
| 1989 | }; |
| 1990 | |
| 1991 | |
| 1992 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 1993 | * A Signature specifies which receivers and arguments a function can |
| 1994 | * legally be called with. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1995 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1996 | class V8EXPORT Signature : public Data { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1997 | public: |
| 1998 | static Local<Signature> New(Handle<FunctionTemplate> receiver = |
| 1999 | Handle<FunctionTemplate>(), |
| 2000 | int argc = 0, |
| 2001 | Handle<FunctionTemplate> argv[] = 0); |
| 2002 | private: |
| 2003 | Signature(); |
| 2004 | }; |
| 2005 | |
| 2006 | |
| 2007 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2008 | * A utility for determining the type of objects based on the template |
| 2009 | * they were constructed from. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2010 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2011 | class V8EXPORT TypeSwitch : public Data { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2012 | public: |
| 2013 | static Local<TypeSwitch> New(Handle<FunctionTemplate> type); |
| 2014 | static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]); |
| 2015 | int match(Handle<Value> value); |
| 2016 | private: |
| 2017 | TypeSwitch(); |
| 2018 | }; |
| 2019 | |
| 2020 | |
| 2021 | // --- E x t e n s i o n s --- |
| 2022 | |
| 2023 | |
| 2024 | /** |
| 2025 | * Ignore |
| 2026 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2027 | class V8EXPORT Extension { // NOLINT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2028 | public: |
| 2029 | Extension(const char* name, |
| 2030 | const char* source = 0, |
| 2031 | int dep_count = 0, |
| 2032 | const char** deps = 0); |
| 2033 | virtual ~Extension() { } |
| 2034 | virtual v8::Handle<v8::FunctionTemplate> |
| 2035 | GetNativeFunction(v8::Handle<v8::String> name) { |
| 2036 | return v8::Handle<v8::FunctionTemplate>(); |
| 2037 | } |
| 2038 | |
| 2039 | const char* name() { return name_; } |
| 2040 | const char* source() { return source_; } |
| 2041 | int dependency_count() { return dep_count_; } |
| 2042 | const char** dependencies() { return deps_; } |
| 2043 | void set_auto_enable(bool value) { auto_enable_ = value; } |
| 2044 | bool auto_enable() { return auto_enable_; } |
| 2045 | |
| 2046 | private: |
| 2047 | const char* name_; |
| 2048 | const char* source_; |
| 2049 | int dep_count_; |
| 2050 | const char** deps_; |
| 2051 | bool auto_enable_; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2052 | |
| 2053 | // Disallow copying and assigning. |
| 2054 | Extension(const Extension&); |
| 2055 | void operator=(const Extension&); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2056 | }; |
| 2057 | |
| 2058 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2059 | void V8EXPORT RegisterExtension(Extension* extension); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2060 | |
| 2061 | |
| 2062 | /** |
| 2063 | * Ignore |
| 2064 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2065 | class V8EXPORT DeclareExtension { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2066 | public: |
| 2067 | inline DeclareExtension(Extension* extension) { |
| 2068 | RegisterExtension(extension); |
| 2069 | } |
| 2070 | }; |
| 2071 | |
| 2072 | |
| 2073 | // --- S t a t i c s --- |
| 2074 | |
| 2075 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2076 | Handle<Primitive> V8EXPORT Undefined(); |
| 2077 | Handle<Primitive> V8EXPORT Null(); |
| 2078 | Handle<Boolean> V8EXPORT True(); |
| 2079 | Handle<Boolean> V8EXPORT False(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2080 | |
| 2081 | |
| 2082 | /** |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2083 | * A set of constraints that specifies the limits of the runtime's memory use. |
| 2084 | * You must set the heap size before initializing the VM - the size cannot be |
| 2085 | * adjusted after the VM is initialized. |
| 2086 | * |
| 2087 | * If you are using threads then you should hold the V8::Locker lock while |
| 2088 | * setting the stack limit and you must set a non-default stack limit separately |
| 2089 | * for each thread. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2090 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2091 | class V8EXPORT ResourceConstraints { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2092 | public: |
| 2093 | ResourceConstraints(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2094 | int max_young_space_size() const { return max_young_space_size_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2095 | 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] | 2096 | int max_old_space_size() const { return max_old_space_size_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2097 | 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] | 2098 | uint32_t* stack_limit() const { return stack_limit_; } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2099 | // Sets an address beyond which the VM's stack may not grow. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2100 | void set_stack_limit(uint32_t* value) { stack_limit_ = value; } |
| 2101 | private: |
| 2102 | int max_young_space_size_; |
| 2103 | int max_old_space_size_; |
| 2104 | uint32_t* stack_limit_; |
| 2105 | }; |
| 2106 | |
| 2107 | |
| 2108 | bool SetResourceConstraints(ResourceConstraints* constraints); |
| 2109 | |
| 2110 | |
| 2111 | // --- E x c e p t i o n s --- |
| 2112 | |
| 2113 | |
| 2114 | typedef void (*FatalErrorCallback)(const char* location, const char* message); |
| 2115 | |
| 2116 | |
| 2117 | typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data); |
| 2118 | |
| 2119 | |
| 2120 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2121 | * Schedules an exception to be thrown when returning to JavaScript. When an |
| 2122 | * exception has been scheduled it is illegal to invoke any JavaScript |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2123 | * operation; the caller must return immediately and only after the exception |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2124 | * has been handled does it become legal to invoke JavaScript operations. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2125 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2126 | Handle<Value> V8EXPORT ThrowException(Handle<Value> exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2127 | |
| 2128 | /** |
| 2129 | * Create new error objects by calling the corresponding error object |
| 2130 | * constructor with the message. |
| 2131 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2132 | class V8EXPORT Exception { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2133 | public: |
| 2134 | static Local<Value> RangeError(Handle<String> message); |
| 2135 | static Local<Value> ReferenceError(Handle<String> message); |
| 2136 | static Local<Value> SyntaxError(Handle<String> message); |
| 2137 | static Local<Value> TypeError(Handle<String> message); |
| 2138 | static Local<Value> Error(Handle<String> message); |
| 2139 | }; |
| 2140 | |
| 2141 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2142 | // --- 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] | 2143 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2144 | typedef int* (*CounterLookupCallback)(const char* name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2145 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2146 | typedef void* (*CreateHistogramCallback)(const char* name, |
| 2147 | int min, |
| 2148 | int max, |
| 2149 | size_t buckets); |
| 2150 | |
| 2151 | typedef void (*AddHistogramSampleCallback)(void* histogram, int sample); |
| 2152 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2153 | // --- 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 --- |
| 2154 | typedef void (*FailedAccessCheckCallback)(Local<Object> target, |
| 2155 | AccessType type, |
| 2156 | Local<Value> data); |
| 2157 | |
| 2158 | // --- 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 |
| 2159 | |
| 2160 | /** |
| 2161 | * Applications can register a callback function which is called |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2162 | * before and after a major garbage collection. Allocations are not |
| 2163 | * allowed in the callback function, you therefore cannot manipulate |
| 2164 | * objects (set or delete properties for example) since it is possible |
| 2165 | * such operations will result in the allocation of objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2166 | */ |
| 2167 | typedef void (*GCCallback)(); |
| 2168 | |
| 2169 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2170 | // --- 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] | 2171 | |
| 2172 | /** |
| 2173 | * Applications must provide a callback function which is called to generate |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2174 | * a context if a context was not deserialized from the snapshot. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2175 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2176 | typedef Persistent<Context> (*ContextGenerator)(); |
| 2177 | |
| 2178 | |
| 2179 | /** |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2180 | * Profiler modules. |
| 2181 | * |
| 2182 | * In V8, profiler consists of several modules: CPU profiler, and different |
| 2183 | * kinds of heap profiling. Each can be turned on / off independently. |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 2184 | * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx, |
| 2185 | * 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] | 2186 | */ |
| 2187 | enum ProfilerModules { |
| 2188 | PROFILER_MODULE_NONE = 0, |
| 2189 | PROFILER_MODULE_CPU = 1, |
| 2190 | PROFILER_MODULE_HEAP_STATS = 1 << 1, |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 2191 | PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2, |
| 2192 | PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16 |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2193 | }; |
| 2194 | |
| 2195 | |
| 2196 | /** |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2197 | * Collection of V8 heap information. |
| 2198 | * |
| 2199 | * Instances of this class can be passed to v8::V8::HeapStatistics to |
| 2200 | * get heap statistics from V8. |
| 2201 | */ |
| 2202 | class V8EXPORT HeapStatistics { |
| 2203 | public: |
| 2204 | HeapStatistics(); |
| 2205 | size_t total_heap_size() { return total_heap_size_; } |
| 2206 | size_t used_heap_size() { return used_heap_size_; } |
| 2207 | |
| 2208 | private: |
| 2209 | void set_total_heap_size(size_t size) { total_heap_size_ = size; } |
| 2210 | void set_used_heap_size(size_t size) { used_heap_size_ = size; } |
| 2211 | |
| 2212 | size_t total_heap_size_; |
| 2213 | size_t used_heap_size_; |
| 2214 | |
| 2215 | friend class V8; |
| 2216 | }; |
| 2217 | |
| 2218 | |
| 2219 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2220 | * Container class for static utility functions. |
| 2221 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2222 | class V8EXPORT V8 { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2223 | public: |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2224 | /** Set the callback to invoke in case of fatal errors. */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2225 | static void SetFatalErrorHandler(FatalErrorCallback that); |
| 2226 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2227 | /** |
| 2228 | * Ignore out-of-memory exceptions. |
| 2229 | * |
| 2230 | * V8 running out of memory is treated as a fatal error by default. |
| 2231 | * This means that the fatal error handler is called and that V8 is |
| 2232 | * terminated. |
| 2233 | * |
| 2234 | * IgnoreOutOfMemoryException can be used to not treat a |
| 2235 | * out-of-memory situation as a fatal error. This way, the contexts |
| 2236 | * that did not cause the out of memory problem might be able to |
| 2237 | * continue execution. |
| 2238 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2239 | static void IgnoreOutOfMemoryException(); |
| 2240 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2241 | /** |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2242 | * 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] | 2243 | * fatal errors such as out-of-memory situations. |
| 2244 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2245 | static bool IsDead(); |
| 2246 | |
| 2247 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2248 | * Adds a message listener. |
| 2249 | * |
| 2250 | * The same message listener can be added more than once and it that |
| 2251 | * case it will be called more than once for each message. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2252 | */ |
| 2253 | static bool AddMessageListener(MessageCallback that, |
| 2254 | Handle<Value> data = Handle<Value>()); |
| 2255 | |
| 2256 | /** |
| 2257 | * Remove all message listeners from the specified callback function. |
| 2258 | */ |
| 2259 | static void RemoveMessageListeners(MessageCallback that); |
| 2260 | |
| 2261 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2262 | * Sets V8 flags from a string. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2263 | */ |
| 2264 | static void SetFlagsFromString(const char* str, int length); |
| 2265 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2266 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2267 | * Sets V8 flags from the command line. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2268 | */ |
| 2269 | static void SetFlagsFromCommandLine(int* argc, |
| 2270 | char** argv, |
| 2271 | bool remove_flags); |
| 2272 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2273 | /** Get the version string. */ |
| 2274 | static const char* GetVersion(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2275 | |
| 2276 | /** |
| 2277 | * Enables the host application to provide a mechanism for recording |
| 2278 | * statistics counters. |
| 2279 | */ |
| 2280 | static void SetCounterFunction(CounterLookupCallback); |
| 2281 | |
| 2282 | /** |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2283 | * Enables the host application to provide a mechanism for recording |
| 2284 | * histograms. The CreateHistogram function returns a |
| 2285 | * histogram which will later be passed to the AddHistogramSample |
| 2286 | * function. |
| 2287 | */ |
| 2288 | static void SetCreateHistogramFunction(CreateHistogramCallback); |
| 2289 | static void SetAddHistogramSampleFunction(AddHistogramSampleCallback); |
| 2290 | |
| 2291 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2292 | * Enables the computation of a sliding window of states. The sliding |
| 2293 | * window information is recorded in statistics counters. |
| 2294 | */ |
| 2295 | static void EnableSlidingStateWindow(); |
| 2296 | |
| 2297 | /** Callback function for reporting failed access checks.*/ |
| 2298 | static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback); |
| 2299 | |
| 2300 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2301 | * Enables the host application to receive a notification before a |
| 2302 | * major garbage colletion. Allocations are not allowed in the |
| 2303 | * callback function, you therefore cannot manipulate objects (set |
| 2304 | * or delete properties for example) since it is possible such |
| 2305 | * operations will result in the allocation of objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2306 | */ |
| 2307 | static void SetGlobalGCPrologueCallback(GCCallback); |
| 2308 | |
| 2309 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2310 | * Enables the host application to receive a notification after a |
| 2311 | * major garbage collection. Allocations are not allowed in the |
| 2312 | * callback function, you therefore cannot manipulate objects (set |
| 2313 | * or delete properties for example) since it is possible such |
| 2314 | * operations will result in the allocation of objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2315 | */ |
| 2316 | static void SetGlobalGCEpilogueCallback(GCCallback); |
| 2317 | |
| 2318 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2319 | * Allows the host application to group objects together. If one |
| 2320 | * object in the group is alive, all objects in the group are alive. |
| 2321 | * After each garbage collection, object groups are removed. It is |
| 2322 | * intended to be used in the before-garbage-collection callback |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 2323 | * function, for instance to simulate DOM tree connections among JS |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2324 | * wrapper objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2325 | */ |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 2326 | static void AddObjectGroup(Persistent<Value>* objects, size_t length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2327 | |
| 2328 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2329 | * Initializes from snapshot if possible. Otherwise, attempts to |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2330 | * initialize from scratch. This function is called implicitly if |
| 2331 | * you use the API without calling it first. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2332 | */ |
| 2333 | static bool Initialize(); |
| 2334 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2335 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2336 | * Adjusts the amount of registered external memory. Used to give |
| 2337 | * V8 an indication of the amount of externally allocated memory |
| 2338 | * that is kept alive by JavaScript objects. V8 uses this to decide |
| 2339 | * when to perform global garbage collections. Registering |
| 2340 | * externally allocated memory will trigger global garbage |
| 2341 | * collections more often than otherwise in an attempt to garbage |
| 2342 | * collect the JavaScript objects keeping the externally allocated |
| 2343 | * memory alive. |
| 2344 | * |
| 2345 | * \param change_in_bytes the change in externally allocated memory |
| 2346 | * that is kept alive by JavaScript objects. |
| 2347 | * \returns the adjusted value. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2348 | */ |
| 2349 | static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes); |
| 2350 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2351 | /** |
| 2352 | * Suspends recording of tick samples in the profiler. |
| 2353 | * When the V8 profiling mode is enabled (usually via command line |
| 2354 | * switches) this function suspends recording of tick samples. |
| 2355 | * Profiling ticks are discarded until ResumeProfiler() is called. |
| 2356 | * |
| 2357 | * See also the --prof and --prof_auto command line switches to |
| 2358 | * enable V8 profiling. |
| 2359 | */ |
| 2360 | static void PauseProfiler(); |
| 2361 | |
| 2362 | /** |
| 2363 | * Resumes recording of tick samples in the profiler. |
| 2364 | * See also PauseProfiler(). |
| 2365 | */ |
| 2366 | static void ResumeProfiler(); |
| 2367 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 2368 | /** |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2369 | * Return whether profiler is currently paused. |
| 2370 | */ |
| 2371 | static bool IsProfilerPaused(); |
| 2372 | |
| 2373 | /** |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2374 | * Resumes specified profiler modules. Can be called several times to |
| 2375 | * mark the opening of a profiler events block with the given tag. |
| 2376 | * |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2377 | * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)". |
| 2378 | * See ProfilerModules enum. |
| 2379 | * |
| 2380 | * \param flags Flags specifying profiler modules. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2381 | * \param tag Profile tag. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2382 | */ |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2383 | static void ResumeProfilerEx(int flags, int tag = 0); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2384 | |
| 2385 | /** |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2386 | * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes |
| 2387 | * a block of profiler events opened by a call to "ResumeProfilerEx" with the |
| 2388 | * same tag value. There is no need for blocks to be properly nested. |
| 2389 | * The profiler is paused when the last opened block is closed. |
| 2390 | * |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2391 | * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)". |
| 2392 | * See ProfilerModules enum. |
| 2393 | * |
| 2394 | * \param flags Flags specifying profiler modules. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2395 | * \param tag Profile tag. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2396 | */ |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2397 | static void PauseProfilerEx(int flags, int tag = 0); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2398 | |
| 2399 | /** |
| 2400 | * Returns active (resumed) profiler modules. |
| 2401 | * See ProfilerModules enum. |
| 2402 | * |
| 2403 | * \returns active profiler modules. |
| 2404 | */ |
| 2405 | static int GetActiveProfilerModules(); |
| 2406 | |
| 2407 | /** |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 2408 | * If logging is performed into a memory buffer (via --logfile=*), allows to |
| 2409 | * retrieve previously written messages. This can be used for retrieving |
| 2410 | * profiler log data in the application. This function is thread-safe. |
| 2411 | * |
| 2412 | * Caller provides a destination buffer that must exist during GetLogLines |
| 2413 | * call. Only whole log lines are copied into the buffer. |
| 2414 | * |
| 2415 | * \param from_pos specified a point in a buffer to read from, 0 is the |
| 2416 | * beginning of a buffer. It is assumed that caller updates its current |
| 2417 | * position using returned size value from the previous call. |
| 2418 | * \param dest_buf destination buffer for log data. |
| 2419 | * \param max_size size of the destination buffer. |
| 2420 | * \returns actual size of log data copied into buffer. |
| 2421 | */ |
| 2422 | static int GetLogLines(int from_pos, char* dest_buf, int max_size); |
| 2423 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 2424 | /** |
| 2425 | * Retrieve the V8 thread id of the calling thread. |
| 2426 | * |
| 2427 | * The thread id for a thread should only be retrieved after the V8 |
| 2428 | * lock has been acquired with a Locker object with that thread. |
| 2429 | */ |
| 2430 | static int GetCurrentThreadId(); |
| 2431 | |
| 2432 | /** |
| 2433 | * Forcefully terminate execution of a JavaScript thread. This can |
| 2434 | * be used to terminate long-running scripts. |
| 2435 | * |
| 2436 | * TerminateExecution should only be called when then V8 lock has |
| 2437 | * been acquired with a Locker object. Therefore, in order to be |
| 2438 | * able to terminate long-running threads, preemption must be |
| 2439 | * enabled to allow the user of TerminateExecution to acquire the |
| 2440 | * lock. |
| 2441 | * |
| 2442 | * The termination is achieved by throwing an exception that is |
| 2443 | * uncatchable by JavaScript exception handlers. Termination |
| 2444 | * exceptions act as if they were caught by a C++ TryCatch exception |
| 2445 | * handlers. If forceful termination is used, any C++ TryCatch |
| 2446 | * exception handler that catches an exception should check if that |
| 2447 | * exception is a termination exception and immediately return if |
| 2448 | * that is the case. Returning immediately in that case will |
| 2449 | * continue the propagation of the termination exception if needed. |
| 2450 | * |
| 2451 | * The thread id passed to TerminateExecution must have been |
| 2452 | * obtained by calling GetCurrentThreadId on the thread in question. |
| 2453 | * |
| 2454 | * \param thread_id The thread id of the thread to terminate. |
| 2455 | */ |
| 2456 | static void TerminateExecution(int thread_id); |
| 2457 | |
| 2458 | /** |
| 2459 | * Forcefully terminate the current thread of JavaScript execution. |
| 2460 | * |
| 2461 | * This method can be used by any thread even if that thread has not |
| 2462 | * acquired the V8 lock with a Locker object. |
| 2463 | */ |
| 2464 | static void TerminateExecution(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 2465 | |
| 2466 | /** |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame] | 2467 | * Is V8 terminating JavaScript execution. |
| 2468 | * |
| 2469 | * Returns true if JavaScript execution is currently terminating |
| 2470 | * because of a call to TerminateExecution. In that case there are |
| 2471 | * still JavaScript frames on the stack and the termination |
| 2472 | * exception is still active. |
| 2473 | */ |
| 2474 | static bool IsExecutionTerminating(); |
| 2475 | |
| 2476 | /** |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 2477 | * Releases any resources used by v8 and stops any utility threads |
| 2478 | * that may be running. Note that disposing v8 is permanent, it |
| 2479 | * cannot be reinitialized. |
| 2480 | * |
| 2481 | * It should generally not be necessary to dispose v8 before exiting |
| 2482 | * a process, this should happen automatically. It is only necessary |
| 2483 | * to use if the process needs the resources taken up by v8. |
| 2484 | */ |
| 2485 | static bool Dispose(); |
| 2486 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2487 | /** |
| 2488 | * Get statistics about the heap memory usage. |
| 2489 | */ |
| 2490 | static void GetHeapStatistics(HeapStatistics* heap_statistics); |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 2491 | |
| 2492 | /** |
| 2493 | * Optional notification that the embedder is idle. |
| 2494 | * V8 uses the notification to reduce memory footprint. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2495 | * This call can be used repeatedly if the embedder remains idle. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2496 | * Returns true if the embedder should stop calling IdleNotification |
| 2497 | * until real work has been done. This indicates that V8 has done |
| 2498 | * as much cleanup as it will be able to do. |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 2499 | */ |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2500 | static bool IdleNotification(); |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 2501 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 2502 | /** |
| 2503 | * Optional notification that the system is running low on memory. |
| 2504 | * V8 uses these notifications to attempt to free memory. |
| 2505 | */ |
| 2506 | static void LowMemoryNotification(); |
| 2507 | |
kasperl@chromium.org | 8b2bb26 | 2010-03-01 09:46:28 +0000 | [diff] [blame] | 2508 | /** |
| 2509 | * Optional notification that a context has been disposed. V8 uses |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame^] | 2510 | * these notifications to guide the GC heuristic. Returns the number |
| 2511 | * of context disposals - including this one - since the last time |
| 2512 | * V8 had a chance to clean up. |
kasperl@chromium.org | 8b2bb26 | 2010-03-01 09:46:28 +0000 | [diff] [blame] | 2513 | */ |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame^] | 2514 | static int ContextDisposedNotification(); |
kasperl@chromium.org | 8b2bb26 | 2010-03-01 09:46:28 +0000 | [diff] [blame] | 2515 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2516 | private: |
| 2517 | V8(); |
| 2518 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2519 | static internal::Object** GlobalizeReference(internal::Object** handle); |
| 2520 | static void DisposeGlobal(internal::Object** global_handle); |
| 2521 | static void MakeWeak(internal::Object** global_handle, |
| 2522 | void* data, |
| 2523 | WeakReferenceCallback); |
| 2524 | static void ClearWeak(internal::Object** global_handle); |
| 2525 | static bool IsGlobalNearDeath(internal::Object** global_handle); |
| 2526 | static bool IsGlobalWeak(internal::Object** global_handle); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2527 | |
| 2528 | template <class T> friend class Handle; |
| 2529 | template <class T> friend class Local; |
| 2530 | template <class T> friend class Persistent; |
| 2531 | friend class Context; |
| 2532 | }; |
| 2533 | |
| 2534 | |
| 2535 | /** |
| 2536 | * An external exception handler. |
| 2537 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2538 | class V8EXPORT TryCatch { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2539 | public: |
| 2540 | |
| 2541 | /** |
| 2542 | * Creates a new try/catch block and registers it with v8. |
| 2543 | */ |
| 2544 | TryCatch(); |
| 2545 | |
| 2546 | /** |
| 2547 | * Unregisters and deletes this try/catch block. |
| 2548 | */ |
| 2549 | ~TryCatch(); |
| 2550 | |
| 2551 | /** |
| 2552 | * Returns true if an exception has been caught by this try/catch block. |
| 2553 | */ |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2554 | bool HasCaught() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2555 | |
| 2556 | /** |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 2557 | * For certain types of exceptions, it makes no sense to continue |
| 2558 | * execution. |
| 2559 | * |
| 2560 | * Currently, the only type of exception that can be caught by a |
| 2561 | * TryCatch handler and for which it does not make sense to continue |
| 2562 | * is termination exception. Such exceptions are thrown when the |
| 2563 | * TerminateExecution methods are called to terminate a long-running |
| 2564 | * script. |
| 2565 | * |
| 2566 | * If CanContinue returns false, the correct action is to perform |
| 2567 | * any C++ cleanup needed and then return. |
| 2568 | */ |
| 2569 | bool CanContinue() const; |
| 2570 | |
| 2571 | /** |
christian.plesner.hansen@gmail.com | b9ce637 | 2009-11-03 11:38:18 +0000 | [diff] [blame] | 2572 | * Throws the exception caught by this TryCatch in a way that avoids |
| 2573 | * it being caught again by this same TryCatch. As with ThrowException |
| 2574 | * it is illegal to execute any JavaScript operations after calling |
| 2575 | * ReThrow; the caller must return immediately to where the exception |
| 2576 | * is caught. |
| 2577 | */ |
| 2578 | Handle<Value> ReThrow(); |
| 2579 | |
| 2580 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2581 | * Returns the exception caught by this try/catch block. If no exception has |
| 2582 | * been caught an empty handle is returned. |
| 2583 | * |
| 2584 | * The returned handle is valid until this TryCatch block has been destroyed. |
| 2585 | */ |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2586 | Local<Value> Exception() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2587 | |
| 2588 | /** |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2589 | * Returns the .stack property of the thrown object. If no .stack |
| 2590 | * property is present an empty handle is returned. |
| 2591 | */ |
| 2592 | Local<Value> StackTrace() const; |
| 2593 | |
| 2594 | /** |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2595 | * Returns the message associated with this exception. If there is |
| 2596 | * no message associated an empty handle is returned. |
| 2597 | * |
| 2598 | * The returned handle is valid until this TryCatch block has been |
| 2599 | * destroyed. |
| 2600 | */ |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2601 | Local<v8::Message> Message() const; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2602 | |
| 2603 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2604 | * Clears any exceptions that may have been caught by this try/catch block. |
| 2605 | * After this method has been called, HasCaught() will return false. |
| 2606 | * |
| 2607 | * It is not necessary to clear a try/catch block before using it again; if |
| 2608 | * another exception is thrown the previously caught exception will just be |
| 2609 | * overwritten. However, it is often a good idea since it makes it easier |
| 2610 | * to determine which operation threw a given exception. |
| 2611 | */ |
| 2612 | void Reset(); |
| 2613 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2614 | /** |
| 2615 | * Set verbosity of the external exception handler. |
| 2616 | * |
| 2617 | * By default, exceptions that are caught by an external exception |
| 2618 | * handler are not reported. Call SetVerbose with true on an |
| 2619 | * external exception handler to have exceptions caught by the |
| 2620 | * handler reported as if they were not caught. |
| 2621 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2622 | void SetVerbose(bool value); |
| 2623 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2624 | /** |
| 2625 | * Set whether or not this TryCatch should capture a Message object |
| 2626 | * which holds source information about where the exception |
| 2627 | * occurred. True by default. |
| 2628 | */ |
| 2629 | void SetCaptureMessage(bool value); |
| 2630 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2631 | private: |
| 2632 | void* next_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2633 | void* exception_; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2634 | void* message_; |
christian.plesner.hansen@gmail.com | b9ce637 | 2009-11-03 11:38:18 +0000 | [diff] [blame] | 2635 | bool is_verbose_ : 1; |
| 2636 | bool can_continue_ : 1; |
| 2637 | bool capture_message_ : 1; |
| 2638 | bool rethrow_ : 1; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2639 | |
| 2640 | friend class v8::internal::Top; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2641 | }; |
| 2642 | |
| 2643 | |
| 2644 | // --- C o n t e x t --- |
| 2645 | |
| 2646 | |
| 2647 | /** |
| 2648 | * Ignore |
| 2649 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2650 | class V8EXPORT ExtensionConfiguration { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2651 | public: |
| 2652 | ExtensionConfiguration(int name_count, const char* names[]) |
| 2653 | : name_count_(name_count), names_(names) { } |
| 2654 | private: |
| 2655 | friend class ImplementationUtilities; |
| 2656 | int name_count_; |
| 2657 | const char** names_; |
| 2658 | }; |
| 2659 | |
| 2660 | |
| 2661 | /** |
| 2662 | * A sandboxed execution context with its own set of built-in objects |
| 2663 | * and functions. |
| 2664 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2665 | class V8EXPORT Context { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2666 | public: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2667 | /** Returns the global object of the context. */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2668 | Local<Object> Global(); |
| 2669 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2670 | /** |
| 2671 | * Detaches the global object from its context before |
| 2672 | * the global object can be reused to create a new context. |
| 2673 | */ |
| 2674 | void DetachGlobal(); |
| 2675 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2676 | /** Creates a new context. */ |
| 2677 | static Persistent<Context> New( |
| 2678 | ExtensionConfiguration* extensions = 0, |
| 2679 | Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(), |
| 2680 | Handle<Value> global_object = Handle<Value>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2681 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 2682 | /** Returns the last entered context. */ |
| 2683 | static Local<Context> GetEntered(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2684 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 2685 | /** Returns the context that is on the top of the stack. */ |
| 2686 | static Local<Context> GetCurrent(); |
| 2687 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2688 | /** |
ager@chromium.org | 1bf0cd0 | 2009-05-20 11:34:19 +0000 | [diff] [blame] | 2689 | * Returns the context of the calling JavaScript code. That is the |
| 2690 | * context of the top-most JavaScript frame. If there are no |
| 2691 | * JavaScript frames an empty handle is returned. |
| 2692 | */ |
| 2693 | static Local<Context> GetCalling(); |
| 2694 | |
| 2695 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2696 | * Sets the security token for the context. To access an object in |
| 2697 | * another context, the security tokens must match. |
| 2698 | */ |
| 2699 | void SetSecurityToken(Handle<Value> token); |
| 2700 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2701 | /** Restores the security token to the default value. */ |
| 2702 | void UseDefaultSecurityToken(); |
| 2703 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2704 | /** Returns the security token of this context.*/ |
| 2705 | Handle<Value> GetSecurityToken(); |
| 2706 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2707 | /** |
| 2708 | * Enter this context. After entering a context, all code compiled |
| 2709 | * and run is compiled and run in this context. If another context |
| 2710 | * is already entered, this old context is saved so it can be |
| 2711 | * restored when the new context is exited. |
| 2712 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2713 | void Enter(); |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2714 | |
| 2715 | /** |
| 2716 | * Exit this context. Exiting the current context restores the |
| 2717 | * context that was in place when entering the current context. |
| 2718 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2719 | void Exit(); |
| 2720 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2721 | /** 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] | 2722 | bool HasOutOfMemoryException(); |
| 2723 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2724 | /** Returns true if V8 has a current context. */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2725 | static bool InContext(); |
| 2726 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2727 | /** |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 2728 | * Associate an additional data object with the context. This is mainly used |
| 2729 | * with the debugger to provide additional information on the context through |
| 2730 | * the debugger API. |
| 2731 | */ |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 2732 | void SetData(Handle<String> data); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 2733 | Local<Value> GetData(); |
| 2734 | |
| 2735 | /** |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2736 | * Stack-allocated class which sets the execution context for all |
| 2737 | * operations executed within a local scope. |
| 2738 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2739 | class V8EXPORT Scope { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2740 | public: |
| 2741 | inline Scope(Handle<Context> context) : context_(context) { |
| 2742 | context_->Enter(); |
| 2743 | } |
| 2744 | inline ~Scope() { context_->Exit(); } |
| 2745 | private: |
| 2746 | Handle<Context> context_; |
| 2747 | }; |
| 2748 | |
| 2749 | private: |
| 2750 | friend class Value; |
| 2751 | friend class Script; |
| 2752 | friend class Object; |
| 2753 | friend class Function; |
| 2754 | }; |
| 2755 | |
| 2756 | |
| 2757 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2758 | * Multiple threads in V8 are allowed, but only one thread at a time |
| 2759 | * is allowed to use V8. The definition of 'using V8' includes |
| 2760 | * accessing handles or holding onto object pointers obtained from V8 |
| 2761 | * handles. It is up to the user of V8 to ensure (perhaps with |
| 2762 | * locking) that this constraint is not violated. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2763 | * |
| 2764 | * If you wish to start using V8 in a thread you can do this by constructing |
| 2765 | * a v8::Locker object. After the code using V8 has completed for the |
| 2766 | * current thread you can call the destructor. This can be combined |
| 2767 | * with C++ scope-based construction as follows: |
| 2768 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2769 | * \code |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2770 | * ... |
| 2771 | * { |
| 2772 | * v8::Locker locker; |
| 2773 | * ... |
| 2774 | * // Code using V8 goes here. |
| 2775 | * ... |
| 2776 | * } // Destructor called here |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2777 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2778 | * |
| 2779 | * If you wish to stop using V8 in a thread A you can do this by either |
| 2780 | * by destroying the v8::Locker object as above or by constructing a |
| 2781 | * v8::Unlocker object: |
| 2782 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2783 | * \code |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2784 | * { |
| 2785 | * v8::Unlocker unlocker; |
| 2786 | * ... |
| 2787 | * // Code not using V8 goes here while V8 can run in another thread. |
| 2788 | * ... |
| 2789 | * } // Destructor called here. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2790 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2791 | * |
| 2792 | * The Unlocker object is intended for use in a long-running callback |
| 2793 | * from V8, where you want to release the V8 lock for other threads to |
| 2794 | * use. |
| 2795 | * |
| 2796 | * The v8::Locker is a recursive lock. That is, you can lock more than |
| 2797 | * once in a given thread. This can be useful if you have code that can |
| 2798 | * be called either from code that holds the lock or from code that does |
| 2799 | * not. The Unlocker is not recursive so you can not have several |
| 2800 | * Unlockers on the stack at once, and you can not use an Unlocker in a |
| 2801 | * thread that is not inside a Locker's scope. |
| 2802 | * |
| 2803 | * An unlocker will unlock several lockers if it has to and reinstate |
| 2804 | * the correct depth of locking on its destruction. eg.: |
| 2805 | * |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2806 | * \code |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2807 | * // V8 not locked. |
| 2808 | * { |
| 2809 | * v8::Locker locker; |
| 2810 | * // V8 locked. |
| 2811 | * { |
| 2812 | * v8::Locker another_locker; |
| 2813 | * // V8 still locked (2 levels). |
| 2814 | * { |
| 2815 | * v8::Unlocker unlocker; |
| 2816 | * // V8 not locked. |
| 2817 | * } |
| 2818 | * // V8 locked again (2 levels). |
| 2819 | * } |
| 2820 | * // V8 still locked (1 level). |
| 2821 | * } |
| 2822 | * // V8 Now no longer locked. |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2823 | * \endcode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2824 | */ |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2825 | class V8EXPORT Unlocker { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2826 | public: |
| 2827 | Unlocker(); |
| 2828 | ~Unlocker(); |
| 2829 | }; |
| 2830 | |
| 2831 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2832 | class V8EXPORT Locker { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2833 | public: |
| 2834 | Locker(); |
| 2835 | ~Locker(); |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 2836 | |
| 2837 | /** |
| 2838 | * Start preemption. |
| 2839 | * |
| 2840 | * When preemption is started, a timer is fired every n milli seconds |
| 2841 | * that will switch between multiple threads that are in contention |
| 2842 | * for the V8 lock. |
| 2843 | */ |
| 2844 | static void StartPreemption(int every_n_ms); |
| 2845 | |
| 2846 | /** |
| 2847 | * Stop preemption. |
| 2848 | */ |
| 2849 | static void StopPreemption(); |
| 2850 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2851 | /** |
| 2852 | * Returns whether or not the locker is locked by the current thread. |
| 2853 | */ |
| 2854 | static bool IsLocked(); |
| 2855 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 2856 | /** |
| 2857 | * Returns whether v8::Locker is being used by this V8 instance. |
| 2858 | */ |
| 2859 | static bool IsActive() { return active_; } |
| 2860 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2861 | private: |
| 2862 | bool has_lock_; |
| 2863 | bool top_level_; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2864 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 2865 | static bool active_; |
| 2866 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2867 | // Disallow copying and assigning. |
| 2868 | Locker(const Locker&); |
| 2869 | void operator=(const Locker&); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2870 | }; |
| 2871 | |
| 2872 | |
| 2873 | |
| 2874 | // --- I m p l e m e n t a t i o n --- |
| 2875 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2876 | |
| 2877 | namespace internal { |
| 2878 | |
| 2879 | |
| 2880 | // Tag information for HeapObject. |
| 2881 | const int kHeapObjectTag = 1; |
| 2882 | const int kHeapObjectTagSize = 2; |
| 2883 | const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1; |
| 2884 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2885 | // Tag information for Smi. |
| 2886 | const int kSmiTag = 0; |
| 2887 | const int kSmiTagSize = 1; |
| 2888 | const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1; |
| 2889 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2890 | template <size_t ptr_size> struct SmiConstants; |
| 2891 | |
| 2892 | // Smi constants for 32-bit systems. |
| 2893 | template <> struct SmiConstants<4> { |
| 2894 | static const int kSmiShiftSize = 0; |
| 2895 | static const int kSmiValueSize = 31; |
| 2896 | static inline int SmiToInt(internal::Object* value) { |
| 2897 | int shift_bits = kSmiTagSize + kSmiShiftSize; |
| 2898 | // Throw away top 32 bits and shift down (requires >> to be sign extending). |
| 2899 | return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits; |
| 2900 | } |
| 2901 | }; |
| 2902 | |
| 2903 | // Smi constants for 64-bit systems. |
| 2904 | template <> struct SmiConstants<8> { |
| 2905 | static const int kSmiShiftSize = 31; |
| 2906 | static const int kSmiValueSize = 32; |
| 2907 | static inline int SmiToInt(internal::Object* value) { |
| 2908 | int shift_bits = kSmiTagSize + kSmiShiftSize; |
| 2909 | // Shift down and throw away top 32 bits. |
| 2910 | return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits); |
| 2911 | } |
| 2912 | }; |
| 2913 | |
| 2914 | const int kSmiShiftSize = SmiConstants<sizeof(void*)>::kSmiShiftSize; |
| 2915 | const int kSmiValueSize = SmiConstants<sizeof(void*)>::kSmiValueSize; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2916 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2917 | template <size_t ptr_size> struct InternalConstants; |
| 2918 | |
| 2919 | // Internal constants for 32-bit systems. |
| 2920 | template <> struct InternalConstants<4> { |
| 2921 | static const int kStringResourceOffset = 3 * sizeof(void*); |
| 2922 | }; |
| 2923 | |
| 2924 | // Internal constants for 64-bit systems. |
| 2925 | template <> struct InternalConstants<8> { |
| 2926 | static const int kStringResourceOffset = 2 * sizeof(void*); |
| 2927 | }; |
| 2928 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2929 | /** |
| 2930 | * This class exports constants and functionality from within v8 that |
| 2931 | * is necessary to implement inline functions in the v8 api. Don't |
| 2932 | * depend on functions and constants defined here. |
| 2933 | */ |
| 2934 | class Internals { |
| 2935 | public: |
| 2936 | |
| 2937 | // These values match non-compiler-dependent values defined within |
| 2938 | // the implementation of v8. |
| 2939 | static const int kHeapObjectMapOffset = 0; |
| 2940 | static const int kMapInstanceTypeOffset = sizeof(void*) + sizeof(int); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2941 | static const int kStringResourceOffset = |
| 2942 | InternalConstants<sizeof(void*)>::kStringResourceOffset; |
| 2943 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2944 | static const int kProxyProxyOffset = sizeof(void*); |
| 2945 | static const int kJSObjectHeaderSize = 3 * sizeof(void*); |
| 2946 | static const int kFullStringRepresentationMask = 0x07; |
| 2947 | static const int kExternalTwoByteRepresentationTag = 0x03; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2948 | |
| 2949 | // These constants are compiler dependent so their values must be |
| 2950 | // defined within the implementation. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 2951 | V8EXPORT static int kJSObjectType; |
| 2952 | V8EXPORT static int kFirstNonstringType; |
| 2953 | V8EXPORT static int kProxyType; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2954 | |
| 2955 | static inline bool HasHeapObjectTag(internal::Object* value) { |
| 2956 | return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) == |
| 2957 | kHeapObjectTag); |
| 2958 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2959 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2960 | static inline bool HasSmiTag(internal::Object* value) { |
| 2961 | return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag); |
| 2962 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2963 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2964 | static inline int SmiValue(internal::Object* value) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2965 | return SmiConstants<sizeof(void*)>::SmiToInt(value); |
| 2966 | } |
| 2967 | |
| 2968 | static inline int GetInstanceType(internal::Object* obj) { |
| 2969 | typedef internal::Object O; |
| 2970 | O* map = ReadField<O*>(obj, kHeapObjectMapOffset); |
| 2971 | return ReadField<uint8_t>(map, kMapInstanceTypeOffset); |
| 2972 | } |
| 2973 | |
| 2974 | static inline void* GetExternalPointer(internal::Object* obj) { |
| 2975 | if (HasSmiTag(obj)) { |
| 2976 | return obj; |
| 2977 | } else if (GetInstanceType(obj) == kProxyType) { |
| 2978 | return ReadField<void*>(obj, kProxyProxyOffset); |
| 2979 | } else { |
| 2980 | return NULL; |
| 2981 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2982 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2983 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2984 | static inline bool IsExternalTwoByteString(int instance_type) { |
| 2985 | int representation = (instance_type & kFullStringRepresentationMask); |
| 2986 | return representation == kExternalTwoByteRepresentationTag; |
| 2987 | } |
| 2988 | |
| 2989 | template <typename T> |
| 2990 | static inline T ReadField(Object* ptr, int offset) { |
| 2991 | uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag; |
| 2992 | return *reinterpret_cast<T*>(addr); |
| 2993 | } |
| 2994 | |
| 2995 | }; |
| 2996 | |
| 2997 | } |
| 2998 | |
| 2999 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3000 | template <class T> |
| 3001 | Handle<T>::Handle() : val_(0) { } |
| 3002 | |
| 3003 | |
| 3004 | template <class T> |
| 3005 | Local<T>::Local() : Handle<T>() { } |
| 3006 | |
| 3007 | |
| 3008 | template <class T> |
| 3009 | Local<T> Local<T>::New(Handle<T> that) { |
| 3010 | if (that.IsEmpty()) return Local<T>(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3011 | internal::Object** p = reinterpret_cast<internal::Object**>(*that); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3012 | return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p))); |
| 3013 | } |
| 3014 | |
| 3015 | |
| 3016 | template <class T> |
| 3017 | Persistent<T> Persistent<T>::New(Handle<T> that) { |
| 3018 | if (that.IsEmpty()) return Persistent<T>(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3019 | internal::Object** p = reinterpret_cast<internal::Object**>(*that); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3020 | return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p))); |
| 3021 | } |
| 3022 | |
| 3023 | |
| 3024 | template <class T> |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3025 | bool Persistent<T>::IsNearDeath() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3026 | if (this->IsEmpty()) return false; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3027 | return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | |
| 3031 | template <class T> |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3032 | bool Persistent<T>::IsWeak() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3033 | if (this->IsEmpty()) return false; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3034 | return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | |
| 3038 | template <class T> |
| 3039 | void Persistent<T>::Dispose() { |
| 3040 | if (this->IsEmpty()) return; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3041 | V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | |
| 3045 | template <class T> |
| 3046 | Persistent<T>::Persistent() : Handle<T>() { } |
| 3047 | |
| 3048 | template <class T> |
| 3049 | void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3050 | V8::MakeWeak(reinterpret_cast<internal::Object**>(**this), |
| 3051 | parameters, |
| 3052 | callback); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
| 3055 | template <class T> |
| 3056 | void Persistent<T>::ClearWeak() { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3057 | V8::ClearWeak(reinterpret_cast<internal::Object**>(**this)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3060 | Local<Value> Arguments::operator[](int i) const { |
| 3061 | if (i < 0 || length_ <= i) return Local<Value>(*Undefined()); |
| 3062 | return Local<Value>(reinterpret_cast<Value*>(values_ - i)); |
| 3063 | } |
| 3064 | |
| 3065 | |
| 3066 | Local<Function> Arguments::Callee() const { |
| 3067 | return callee_; |
| 3068 | } |
| 3069 | |
| 3070 | |
| 3071 | Local<Object> Arguments::This() const { |
| 3072 | return Local<Object>(reinterpret_cast<Object*>(values_ + 1)); |
| 3073 | } |
| 3074 | |
| 3075 | |
| 3076 | Local<Object> Arguments::Holder() const { |
| 3077 | return holder_; |
| 3078 | } |
| 3079 | |
| 3080 | |
| 3081 | Local<Value> Arguments::Data() const { |
| 3082 | return data_; |
| 3083 | } |
| 3084 | |
| 3085 | |
| 3086 | bool Arguments::IsConstructCall() const { |
| 3087 | return is_construct_call_; |
| 3088 | } |
| 3089 | |
| 3090 | |
| 3091 | int Arguments::Length() const { |
| 3092 | return length_; |
| 3093 | } |
| 3094 | |
| 3095 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3096 | template <class T> |
| 3097 | Local<T> HandleScope::Close(Handle<T> value) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3098 | internal::Object** before = reinterpret_cast<internal::Object**>(*value); |
| 3099 | internal::Object** after = RawClose(before); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3100 | return Local<T>(reinterpret_cast<T*>(after)); |
| 3101 | } |
| 3102 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3103 | Handle<Value> ScriptOrigin::ResourceName() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3104 | return resource_name_; |
| 3105 | } |
| 3106 | |
| 3107 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3108 | Handle<Integer> ScriptOrigin::ResourceLineOffset() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3109 | return resource_line_offset_; |
| 3110 | } |
| 3111 | |
| 3112 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3113 | Handle<Integer> ScriptOrigin::ResourceColumnOffset() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3114 | return resource_column_offset_; |
| 3115 | } |
| 3116 | |
| 3117 | |
| 3118 | Handle<Boolean> Boolean::New(bool value) { |
| 3119 | return value ? True() : False(); |
| 3120 | } |
| 3121 | |
| 3122 | |
| 3123 | void Template::Set(const char* name, v8::Handle<Data> value) { |
| 3124 | Set(v8::String::New(name), value); |
| 3125 | } |
| 3126 | |
| 3127 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3128 | Local<Value> Object::GetInternalField(int index) { |
| 3129 | #ifndef V8_ENABLE_CHECKS |
| 3130 | Local<Value> quick_result = UncheckedGetInternalField(index); |
| 3131 | if (!quick_result.IsEmpty()) return quick_result; |
| 3132 | #endif |
| 3133 | return CheckedGetInternalField(index); |
| 3134 | } |
| 3135 | |
| 3136 | |
| 3137 | Local<Value> Object::UncheckedGetInternalField(int index) { |
| 3138 | typedef internal::Object O; |
| 3139 | typedef internal::Internals I; |
| 3140 | O* obj = *reinterpret_cast<O**>(this); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 3141 | if (I::GetInstanceType(obj) == I::kJSObjectType) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3142 | // If the object is a plain JSObject, which is the common case, |
| 3143 | // we know where to find the internal fields and can return the |
| 3144 | // value directly. |
| 3145 | int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index); |
| 3146 | O* value = I::ReadField<O*>(obj, offset); |
| 3147 | O** result = HandleScope::CreateHandle(value); |
| 3148 | return Local<Value>(reinterpret_cast<Value*>(result)); |
| 3149 | } else { |
| 3150 | return Local<Value>(); |
| 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | |
| 3155 | void* External::Unwrap(Handle<v8::Value> obj) { |
| 3156 | #ifdef V8_ENABLE_CHECKS |
| 3157 | return FullUnwrap(obj); |
| 3158 | #else |
| 3159 | return QuickUnwrap(obj); |
| 3160 | #endif |
| 3161 | } |
| 3162 | |
| 3163 | |
| 3164 | void* External::QuickUnwrap(Handle<v8::Value> wrapper) { |
| 3165 | typedef internal::Object O; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3166 | O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper)); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 3167 | return internal::Internals::GetExternalPointer(obj); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
| 3170 | |
| 3171 | void* Object::GetPointerFromInternalField(int index) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 3172 | typedef internal::Object O; |
| 3173 | typedef internal::Internals I; |
| 3174 | |
| 3175 | O* obj = *reinterpret_cast<O**>(this); |
| 3176 | |
| 3177 | if (I::GetInstanceType(obj) == I::kJSObjectType) { |
| 3178 | // If the object is a plain JSObject, which is the common case, |
| 3179 | // we know where to find the internal fields and can return the |
| 3180 | // value directly. |
| 3181 | int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index); |
| 3182 | O* value = I::ReadField<O*>(obj, offset); |
| 3183 | return I::GetExternalPointer(value); |
| 3184 | } |
| 3185 | |
| 3186 | return SlowGetPointerFromInternalField(index); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | |
| 3190 | String* String::Cast(v8::Value* value) { |
| 3191 | #ifdef V8_ENABLE_CHECKS |
| 3192 | CheckCast(value); |
| 3193 | #endif |
| 3194 | return static_cast<String*>(value); |
| 3195 | } |
| 3196 | |
| 3197 | |
| 3198 | String::ExternalStringResource* String::GetExternalStringResource() const { |
| 3199 | typedef internal::Object O; |
| 3200 | typedef internal::Internals I; |
| 3201 | O* obj = *reinterpret_cast<O**>(const_cast<String*>(this)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3202 | String::ExternalStringResource* result; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 3203 | if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3204 | void* value = I::ReadField<void*>(obj, I::kStringResourceOffset); |
| 3205 | result = reinterpret_cast<String::ExternalStringResource*>(value); |
| 3206 | } else { |
| 3207 | result = NULL; |
| 3208 | } |
| 3209 | #ifdef V8_ENABLE_CHECKS |
| 3210 | VerifyExternalStringResource(result); |
| 3211 | #endif |
| 3212 | return result; |
| 3213 | } |
| 3214 | |
| 3215 | |
| 3216 | bool Value::IsString() const { |
| 3217 | #ifdef V8_ENABLE_CHECKS |
| 3218 | return FullIsString(); |
| 3219 | #else |
| 3220 | return QuickIsString(); |
| 3221 | #endif |
| 3222 | } |
| 3223 | |
| 3224 | bool Value::QuickIsString() const { |
| 3225 | typedef internal::Object O; |
| 3226 | typedef internal::Internals I; |
| 3227 | O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this)); |
| 3228 | if (!I::HasHeapObjectTag(obj)) return false; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 3229 | return (I::GetInstanceType(obj) < I::kFirstNonstringType); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
| 3232 | |
| 3233 | Number* Number::Cast(v8::Value* value) { |
| 3234 | #ifdef V8_ENABLE_CHECKS |
| 3235 | CheckCast(value); |
| 3236 | #endif |
| 3237 | return static_cast<Number*>(value); |
| 3238 | } |
| 3239 | |
| 3240 | |
| 3241 | Integer* Integer::Cast(v8::Value* value) { |
| 3242 | #ifdef V8_ENABLE_CHECKS |
| 3243 | CheckCast(value); |
| 3244 | #endif |
| 3245 | return static_cast<Integer*>(value); |
| 3246 | } |
| 3247 | |
| 3248 | |
| 3249 | Date* Date::Cast(v8::Value* value) { |
| 3250 | #ifdef V8_ENABLE_CHECKS |
| 3251 | CheckCast(value); |
| 3252 | #endif |
| 3253 | return static_cast<Date*>(value); |
| 3254 | } |
| 3255 | |
| 3256 | |
| 3257 | Object* Object::Cast(v8::Value* value) { |
| 3258 | #ifdef V8_ENABLE_CHECKS |
| 3259 | CheckCast(value); |
| 3260 | #endif |
| 3261 | return static_cast<Object*>(value); |
| 3262 | } |
| 3263 | |
| 3264 | |
| 3265 | Array* Array::Cast(v8::Value* value) { |
| 3266 | #ifdef V8_ENABLE_CHECKS |
| 3267 | CheckCast(value); |
| 3268 | #endif |
| 3269 | return static_cast<Array*>(value); |
| 3270 | } |
| 3271 | |
| 3272 | |
| 3273 | Function* Function::Cast(v8::Value* value) { |
| 3274 | #ifdef V8_ENABLE_CHECKS |
| 3275 | CheckCast(value); |
| 3276 | #endif |
| 3277 | return static_cast<Function*>(value); |
| 3278 | } |
| 3279 | |
| 3280 | |
| 3281 | External* External::Cast(v8::Value* value) { |
| 3282 | #ifdef V8_ENABLE_CHECKS |
| 3283 | CheckCast(value); |
| 3284 | #endif |
| 3285 | return static_cast<External*>(value); |
| 3286 | } |
| 3287 | |
| 3288 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3289 | Local<Value> AccessorInfo::Data() const { |
| 3290 | return Local<Value>(reinterpret_cast<Value*>(&args_[-3])); |
| 3291 | } |
| 3292 | |
| 3293 | |
| 3294 | Local<Object> AccessorInfo::This() const { |
| 3295 | return Local<Object>(reinterpret_cast<Object*>(&args_[0])); |
| 3296 | } |
| 3297 | |
| 3298 | |
| 3299 | Local<Object> AccessorInfo::Holder() const { |
| 3300 | return Local<Object>(reinterpret_cast<Object*>(&args_[-1])); |
| 3301 | } |
| 3302 | |
| 3303 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3304 | /** |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 3305 | * \example shell.cc |
| 3306 | * A simple shell that takes a list of expressions on the |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3307 | * command-line and executes them. |
| 3308 | */ |
| 3309 | |
| 3310 | |
| 3311 | /** |
| 3312 | * \example process.cc |
| 3313 | */ |
| 3314 | |
| 3315 | |
| 3316 | } // namespace v8 |
| 3317 | |
| 3318 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3319 | #undef V8EXPORT |
| 3320 | #undef V8EXPORT_INLINE |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3321 | #undef TYPE_CHECK |
| 3322 | |
| 3323 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3324 | #endif // V8_H_ |