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