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