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