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