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