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