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