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