yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1 | // Copyright 2012 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 | |
| 28 | #ifndef V8_API_H_ |
| 29 | #define V8_API_H_ |
| 30 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 31 | #include "v8.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 33 | #include "../include/v8-testing.h" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 34 | #include "apiutils.h" |
| 35 | #include "contexts.h" |
| 36 | #include "factory.h" |
| 37 | #include "isolate.h" |
| 38 | #include "list-inl.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 39 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | namespace v8 { |
| 41 | |
| 42 | // Constants used in the implementation of the API. The most natural thing |
| 43 | // would usually be to place these with the classes that use them, but |
| 44 | // we want to keep them out of v8.h because it is an externally |
| 45 | // visible file. |
| 46 | class Consts { |
| 47 | public: |
| 48 | enum TemplateType { |
| 49 | FUNCTION_TEMPLATE = 0, |
| 50 | OBJECT_TEMPLATE = 1 |
| 51 | }; |
| 52 | }; |
| 53 | |
| 54 | |
| 55 | // Utilities for working with neander-objects, primitive |
| 56 | // env-independent JSObjects used by the api. |
| 57 | class NeanderObject { |
| 58 | public: |
| 59 | explicit NeanderObject(int size); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 60 | explicit inline NeanderObject(v8::internal::Handle<v8::internal::Object> obj); |
| 61 | explicit inline NeanderObject(v8::internal::Object* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 62 | inline v8::internal::Object* get(int index); |
| 63 | inline void set(int index, v8::internal::Object* value); |
| 64 | inline v8::internal::Handle<v8::internal::JSObject> value() { return value_; } |
| 65 | int size(); |
| 66 | private: |
| 67 | v8::internal::Handle<v8::internal::JSObject> value_; |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | // Utilities for working with neander-arrays, a simple extensible |
| 72 | // array abstraction built on neander-objects. |
| 73 | class NeanderArray { |
| 74 | public: |
| 75 | NeanderArray(); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 76 | explicit inline NeanderArray(v8::internal::Handle<v8::internal::Object> obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 77 | inline v8::internal::Handle<v8::internal::JSObject> value() { |
| 78 | return obj_.value(); |
| 79 | } |
| 80 | |
| 81 | void add(v8::internal::Handle<v8::internal::Object> value); |
| 82 | |
| 83 | int length(); |
| 84 | |
| 85 | v8::internal::Object* get(int index); |
| 86 | // Change the value at an index to undefined value. If the index is |
| 87 | // out of bounds, the request is ignored. Returns the old value. |
| 88 | void set(int index, v8::internal::Object* value); |
| 89 | private: |
| 90 | NeanderObject obj_; |
| 91 | }; |
| 92 | |
| 93 | |
| 94 | NeanderObject::NeanderObject(v8::internal::Handle<v8::internal::Object> obj) |
| 95 | : value_(v8::internal::Handle<v8::internal::JSObject>::cast(obj)) { } |
| 96 | |
| 97 | |
| 98 | NeanderObject::NeanderObject(v8::internal::Object* obj) |
| 99 | : value_(v8::internal::Handle<v8::internal::JSObject>( |
| 100 | v8::internal::JSObject::cast(obj))) { } |
| 101 | |
| 102 | |
| 103 | NeanderArray::NeanderArray(v8::internal::Handle<v8::internal::Object> obj) |
| 104 | : obj_(obj) { } |
| 105 | |
| 106 | |
| 107 | v8::internal::Object* NeanderObject::get(int offset) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 108 | ASSERT(value()->HasFastObjectElements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 109 | return v8::internal::FixedArray::cast(value()->elements())->get(offset); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | void NeanderObject::set(int offset, v8::internal::Object* value) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 114 | ASSERT(value_->HasFastObjectElements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | v8::internal::FixedArray::cast(value_->elements())->set(offset, value); |
| 116 | } |
| 117 | |
| 118 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 119 | template <typename T> inline T ToCData(v8::internal::Object* obj) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 120 | STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address)); |
| 121 | return reinterpret_cast<T>( |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 122 | reinterpret_cast<intptr_t>( |
| 123 | v8::internal::Foreign::cast(obj)->foreign_address())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | |
| 127 | template <typename T> |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 128 | inline v8::internal::Handle<v8::internal::Object> FromCData(T obj) { |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 129 | v8::internal::Isolate* isolate = v8::internal::Isolate::Current(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address)); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 131 | return isolate->factory()->NewForeign( |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 132 | reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(obj))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 136 | class ApiFunction { |
| 137 | public: |
| 138 | explicit ApiFunction(v8::internal::Address addr) : addr_(addr) { } |
| 139 | v8::internal::Address address() { return addr_; } |
| 140 | private: |
| 141 | v8::internal::Address addr_; |
| 142 | }; |
| 143 | |
| 144 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 145 | |
| 146 | class RegisteredExtension { |
| 147 | public: |
| 148 | explicit RegisteredExtension(Extension* extension); |
| 149 | static void Register(RegisteredExtension* that); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 150 | static void UnregisterAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 151 | Extension* extension() { return extension_; } |
| 152 | RegisteredExtension* next() { return next_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 153 | static RegisteredExtension* first_extension() { return first_extension_; } |
| 154 | private: |
| 155 | Extension* extension_; |
| 156 | RegisteredExtension* next_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 157 | static RegisteredExtension* first_extension_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 161 | #define OPEN_HANDLE_LIST(V) \ |
| 162 | V(Template, TemplateInfo) \ |
| 163 | V(FunctionTemplate, FunctionTemplateInfo) \ |
| 164 | V(ObjectTemplate, ObjectTemplateInfo) \ |
| 165 | V(Signature, SignatureInfo) \ |
| 166 | V(AccessorSignature, FunctionTemplateInfo) \ |
| 167 | V(TypeSwitch, TypeSwitchInfo) \ |
| 168 | V(Data, Object) \ |
| 169 | V(RegExp, JSRegExp) \ |
| 170 | V(Object, JSObject) \ |
| 171 | V(Array, JSArray) \ |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 172 | V(ArrayBuffer, JSArrayBuffer) \ |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 173 | V(ArrayBufferView, JSArrayBufferView) \ |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 174 | V(TypedArray, JSTypedArray) \ |
| 175 | V(Uint8Array, JSTypedArray) \ |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 176 | V(Uint8ClampedArray, JSTypedArray) \ |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 177 | V(Int8Array, JSTypedArray) \ |
| 178 | V(Uint16Array, JSTypedArray) \ |
| 179 | V(Int16Array, JSTypedArray) \ |
| 180 | V(Uint32Array, JSTypedArray) \ |
| 181 | V(Int32Array, JSTypedArray) \ |
| 182 | V(Float32Array, JSTypedArray) \ |
| 183 | V(Float64Array, JSTypedArray) \ |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 184 | V(DataView, JSDataView) \ |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 185 | V(String, String) \ |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 186 | V(Symbol, Symbol) \ |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 187 | V(Script, Object) \ |
| 188 | V(Function, JSFunction) \ |
| 189 | V(Message, JSObject) \ |
| 190 | V(Context, Context) \ |
| 191 | V(External, Foreign) \ |
| 192 | V(StackTrace, JSArray) \ |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 193 | V(StackFrame, JSObject) \ |
| 194 | V(DeclaredAccessorDescriptor, DeclaredAccessorDescriptor) |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 195 | |
| 196 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 197 | class Utils { |
| 198 | public: |
| 199 | static bool ReportApiFailure(const char* location, const char* message); |
| 200 | |
| 201 | static Local<FunctionTemplate> ToFunctionTemplate(NeanderObject obj); |
| 202 | static Local<ObjectTemplate> ToObjectTemplate(NeanderObject obj); |
| 203 | |
| 204 | static inline Local<Context> ToLocal( |
| 205 | v8::internal::Handle<v8::internal::Context> obj); |
| 206 | static inline Local<Value> ToLocal( |
| 207 | v8::internal::Handle<v8::internal::Object> obj); |
| 208 | static inline Local<Function> ToLocal( |
| 209 | v8::internal::Handle<v8::internal::JSFunction> obj); |
| 210 | static inline Local<String> ToLocal( |
| 211 | v8::internal::Handle<v8::internal::String> obj); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 212 | static inline Local<Symbol> ToLocal( |
| 213 | v8::internal::Handle<v8::internal::Symbol> obj); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 214 | static inline Local<RegExp> ToLocal( |
| 215 | v8::internal::Handle<v8::internal::JSRegExp> obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 216 | static inline Local<Object> ToLocal( |
| 217 | v8::internal::Handle<v8::internal::JSObject> obj); |
| 218 | static inline Local<Array> ToLocal( |
| 219 | v8::internal::Handle<v8::internal::JSArray> obj); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 220 | static inline Local<ArrayBuffer> ToLocal( |
| 221 | v8::internal::Handle<v8::internal::JSArrayBuffer> obj); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 222 | static inline Local<ArrayBufferView> ToLocal( |
| 223 | v8::internal::Handle<v8::internal::JSArrayBufferView> obj); |
| 224 | static inline Local<DataView> ToLocal( |
| 225 | v8::internal::Handle<v8::internal::JSDataView> obj); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 226 | |
| 227 | static inline Local<TypedArray> ToLocal( |
| 228 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 229 | static inline Local<Uint8Array> ToLocalUint8Array( |
| 230 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 231 | static inline Local<Uint8ClampedArray> ToLocalUint8ClampedArray( |
| 232 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 233 | static inline Local<Int8Array> ToLocalInt8Array( |
| 234 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 235 | static inline Local<Uint16Array> ToLocalUint16Array( |
| 236 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 237 | static inline Local<Int16Array> ToLocalInt16Array( |
| 238 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 239 | static inline Local<Uint32Array> ToLocalUint32Array( |
| 240 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 241 | static inline Local<Int32Array> ToLocalInt32Array( |
| 242 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 243 | static inline Local<Float32Array> ToLocalFloat32Array( |
| 244 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 245 | static inline Local<Float64Array> ToLocalFloat64Array( |
| 246 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 247 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 248 | static inline Local<Message> MessageToLocal( |
| 249 | v8::internal::Handle<v8::internal::Object> obj); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 250 | static inline Local<StackTrace> StackTraceToLocal( |
| 251 | v8::internal::Handle<v8::internal::JSArray> obj); |
| 252 | static inline Local<StackFrame> StackFrameToLocal( |
| 253 | v8::internal::Handle<v8::internal::JSObject> obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 254 | static inline Local<Number> NumberToLocal( |
| 255 | v8::internal::Handle<v8::internal::Object> obj); |
| 256 | static inline Local<Integer> IntegerToLocal( |
| 257 | v8::internal::Handle<v8::internal::Object> obj); |
| 258 | static inline Local<Uint32> Uint32ToLocal( |
| 259 | v8::internal::Handle<v8::internal::Object> obj); |
| 260 | static inline Local<FunctionTemplate> ToLocal( |
| 261 | v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj); |
| 262 | static inline Local<ObjectTemplate> ToLocal( |
| 263 | v8::internal::Handle<v8::internal::ObjectTemplateInfo> obj); |
| 264 | static inline Local<Signature> ToLocal( |
| 265 | v8::internal::Handle<v8::internal::SignatureInfo> obj); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 266 | static inline Local<AccessorSignature> AccessorSignatureToLocal( |
| 267 | v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 268 | static inline Local<TypeSwitch> ToLocal( |
| 269 | v8::internal::Handle<v8::internal::TypeSwitchInfo> obj); |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 270 | static inline Local<External> ExternalToLocal( |
| 271 | v8::internal::Handle<v8::internal::JSObject> obj); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 272 | static inline Local<DeclaredAccessorDescriptor> ToLocal( |
| 273 | v8::internal::Handle<v8::internal::DeclaredAccessorDescriptor> obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 274 | |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 275 | #define DECLARE_OPEN_HANDLE(From, To) \ |
| 276 | static inline v8::internal::Handle<v8::internal::To> \ |
| 277 | OpenHandle(const From* that, bool allow_empty_handle = false); |
| 278 | |
| 279 | OPEN_HANDLE_LIST(DECLARE_OPEN_HANDLE) |
| 280 | |
| 281 | #undef DECLARE_OPEN_HANDLE |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 282 | |
| 283 | template<class From, class To> |
| 284 | static inline Local<To> Convert(v8::internal::Handle<From> obj) { |
| 285 | ASSERT(obj.is_null() || !obj->IsTheHole()); |
| 286 | return Local<To>(reinterpret_cast<To*>(obj.location())); |
| 287 | } |
| 288 | |
| 289 | template <class T> |
| 290 | static inline v8::internal::Handle<v8::internal::Object> OpenPersistent( |
| 291 | const v8::Persistent<T>& persistent) { |
| 292 | return v8::internal::Handle<v8::internal::Object>( |
| 293 | reinterpret_cast<v8::internal::Object**>(persistent.val_)); |
| 294 | } |
| 295 | |
| 296 | template <class T> |
| 297 | static inline v8::internal::Handle<v8::internal::Object> OpenPersistent( |
| 298 | v8::Persistent<T>* persistent) { |
| 299 | return OpenPersistent(*persistent); |
| 300 | } |
| 301 | |
| 302 | template <class From, class To> |
| 303 | static inline v8::internal::Handle<To> OpenHandle(v8::Local<From> handle) { |
| 304 | return OpenHandle(*handle); |
| 305 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | |
| 309 | template <class T> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 310 | v8::internal::Handle<T> v8::internal::Handle<T>::EscapeFrom( |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 311 | v8::HandleScope* scope) { |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 312 | v8::internal::Handle<T> handle; |
| 313 | if (!is_null()) { |
| 314 | handle = *this; |
| 315 | } |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 316 | return Utils::OpenHandle(*scope->Close(Utils::ToLocal(handle)), true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 320 | template <class T> |
| 321 | inline T* ToApi(v8::internal::Handle<v8::internal::Object> obj) { |
| 322 | return reinterpret_cast<T*>(obj.location()); |
| 323 | } |
| 324 | |
| 325 | template <class T> |
| 326 | inline v8::Local<T> ToApiHandle( |
| 327 | v8::internal::Handle<v8::internal::Object> obj) { |
| 328 | return Utils::Convert<v8::internal::Object, T>(obj); |
| 329 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 330 | |
| 331 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 332 | // Implementations of ToLocal |
| 333 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 334 | #define MAKE_TO_LOCAL(Name, From, To) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 335 | Local<v8::To> Utils::Name(v8::internal::Handle<v8::internal::From> obj) { \ |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 336 | return Convert<v8::internal::From, v8::To>(obj); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 337 | } |
| 338 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 339 | |
| 340 | #define MAKE_TO_LOCAL_TYPED_ARRAY(TypedArray, typeConst) \ |
| 341 | Local<v8::TypedArray> Utils::ToLocal##TypedArray( \ |
| 342 | v8::internal::Handle<v8::internal::JSTypedArray> obj) { \ |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 343 | ASSERT(obj->type() == typeConst); \ |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 344 | return Convert<v8::internal::JSTypedArray, v8::TypedArray>(obj); \ |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 348 | MAKE_TO_LOCAL(ToLocal, Context, Context) |
| 349 | MAKE_TO_LOCAL(ToLocal, Object, Value) |
| 350 | MAKE_TO_LOCAL(ToLocal, JSFunction, Function) |
| 351 | MAKE_TO_LOCAL(ToLocal, String, String) |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 352 | MAKE_TO_LOCAL(ToLocal, Symbol, Symbol) |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 353 | MAKE_TO_LOCAL(ToLocal, JSRegExp, RegExp) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 354 | MAKE_TO_LOCAL(ToLocal, JSObject, Object) |
| 355 | MAKE_TO_LOCAL(ToLocal, JSArray, Array) |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 356 | MAKE_TO_LOCAL(ToLocal, JSArrayBuffer, ArrayBuffer) |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 357 | MAKE_TO_LOCAL(ToLocal, JSArrayBufferView, ArrayBufferView) |
| 358 | MAKE_TO_LOCAL(ToLocal, JSDataView, DataView) |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 359 | MAKE_TO_LOCAL(ToLocal, JSTypedArray, TypedArray) |
| 360 | |
| 361 | MAKE_TO_LOCAL_TYPED_ARRAY(Uint8Array, kExternalUnsignedByteArray) |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 362 | MAKE_TO_LOCAL_TYPED_ARRAY(Uint8ClampedArray, kExternalPixelArray) |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 363 | MAKE_TO_LOCAL_TYPED_ARRAY(Int8Array, kExternalByteArray) |
| 364 | MAKE_TO_LOCAL_TYPED_ARRAY(Uint16Array, kExternalUnsignedShortArray) |
| 365 | MAKE_TO_LOCAL_TYPED_ARRAY(Int16Array, kExternalShortArray) |
| 366 | MAKE_TO_LOCAL_TYPED_ARRAY(Uint32Array, kExternalUnsignedIntArray) |
| 367 | MAKE_TO_LOCAL_TYPED_ARRAY(Int32Array, kExternalIntArray) |
| 368 | MAKE_TO_LOCAL_TYPED_ARRAY(Float32Array, kExternalFloatArray) |
| 369 | MAKE_TO_LOCAL_TYPED_ARRAY(Float64Array, kExternalDoubleArray) |
| 370 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 371 | MAKE_TO_LOCAL(ToLocal, FunctionTemplateInfo, FunctionTemplate) |
| 372 | MAKE_TO_LOCAL(ToLocal, ObjectTemplateInfo, ObjectTemplate) |
| 373 | MAKE_TO_LOCAL(ToLocal, SignatureInfo, Signature) |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 374 | MAKE_TO_LOCAL(AccessorSignatureToLocal, FunctionTemplateInfo, AccessorSignature) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 375 | MAKE_TO_LOCAL(ToLocal, TypeSwitchInfo, TypeSwitch) |
| 376 | MAKE_TO_LOCAL(MessageToLocal, Object, Message) |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 377 | MAKE_TO_LOCAL(StackTraceToLocal, JSArray, StackTrace) |
| 378 | MAKE_TO_LOCAL(StackFrameToLocal, JSObject, StackFrame) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 379 | MAKE_TO_LOCAL(NumberToLocal, Object, Number) |
| 380 | MAKE_TO_LOCAL(IntegerToLocal, Object, Integer) |
| 381 | MAKE_TO_LOCAL(Uint32ToLocal, Object, Uint32) |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 382 | MAKE_TO_LOCAL(ExternalToLocal, JSObject, External) |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 383 | MAKE_TO_LOCAL(ToLocal, DeclaredAccessorDescriptor, DeclaredAccessorDescriptor) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 384 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 385 | #undef MAKE_TO_LOCAL_TYPED_ARRAY |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 386 | #undef MAKE_TO_LOCAL |
| 387 | |
| 388 | |
| 389 | // Implementations of OpenHandle |
| 390 | |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 391 | #define MAKE_OPEN_HANDLE(From, To) \ |
| 392 | v8::internal::Handle<v8::internal::To> Utils::OpenHandle( \ |
| 393 | const v8::From* that, bool allow_empty_handle) { \ |
| 394 | EXTRA_CHECK(allow_empty_handle || that != NULL); \ |
danno@chromium.org | c99cd48 | 2013-03-21 15:26:42 +0000 | [diff] [blame] | 395 | EXTRA_CHECK(that == NULL || \ |
| 396 | !(*reinterpret_cast<v8::internal::To**>( \ |
| 397 | const_cast<v8::From*>(that)))->IsFailure()); \ |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 398 | return v8::internal::Handle<v8::internal::To>( \ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 399 | reinterpret_cast<v8::internal::To**>(const_cast<v8::From*>(that))); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 400 | } |
| 401 | |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 402 | OPEN_HANDLE_LIST(MAKE_OPEN_HANDLE) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 403 | |
| 404 | #undef MAKE_OPEN_HANDLE |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 405 | #undef OPEN_HANDLE_LIST |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 406 | |
| 407 | |
| 408 | namespace internal { |
| 409 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 410 | // Tracks string usage to help make better decisions when |
| 411 | // externalizing strings. |
| 412 | // |
| 413 | // Implementation note: internally this class only tracks fresh |
| 414 | // strings and keeps a single use counter for them. |
| 415 | class StringTracker { |
| 416 | public: |
| 417 | // Records that the given string's characters were copied to some |
| 418 | // external buffer. If this happens often we should honor |
| 419 | // externalization requests for the string. |
| 420 | void RecordWrite(Handle<String> string) { |
| 421 | Address address = reinterpret_cast<Address>(*string); |
| 422 | Address top = isolate_->heap()->NewSpaceTop(); |
| 423 | if (IsFreshString(address, top)) { |
| 424 | IncrementUseCount(top); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // Estimates freshness and use frequency of the given string based |
| 429 | // on how close it is to the new space top and the recorded usage |
| 430 | // history. |
| 431 | inline bool IsFreshUnusedString(Handle<String> string) { |
| 432 | Address address = reinterpret_cast<Address>(*string); |
| 433 | Address top = isolate_->heap()->NewSpaceTop(); |
| 434 | return IsFreshString(address, top) && IsUseCountLow(top); |
| 435 | } |
| 436 | |
| 437 | private: |
| 438 | StringTracker() : use_count_(0), last_top_(NULL), isolate_(NULL) { } |
| 439 | |
| 440 | static inline bool IsFreshString(Address string, Address top) { |
| 441 | return top - kFreshnessLimit <= string && string <= top; |
| 442 | } |
| 443 | |
| 444 | inline bool IsUseCountLow(Address top) { |
| 445 | if (last_top_ != top) return true; |
| 446 | return use_count_ < kUseLimit; |
| 447 | } |
| 448 | |
| 449 | inline void IncrementUseCount(Address top) { |
| 450 | if (last_top_ != top) { |
| 451 | use_count_ = 0; |
| 452 | last_top_ = top; |
| 453 | } |
| 454 | ++use_count_; |
| 455 | } |
| 456 | |
| 457 | // Single use counter shared by all fresh strings. |
| 458 | int use_count_; |
| 459 | |
| 460 | // Last new space top when the use count above was valid. |
| 461 | Address last_top_; |
| 462 | |
| 463 | Isolate* isolate_; |
| 464 | |
| 465 | // How close to the new space top a fresh string has to be. |
| 466 | static const int kFreshnessLimit = 1024; |
| 467 | |
| 468 | // The number of uses required to consider a string useful. |
| 469 | static const int kUseLimit = 32; |
| 470 | |
| 471 | friend class Isolate; |
| 472 | |
| 473 | DISALLOW_COPY_AND_ASSIGN(StringTracker); |
| 474 | }; |
| 475 | |
| 476 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 477 | class DeferredHandles { |
| 478 | public: |
| 479 | ~DeferredHandles(); |
| 480 | |
| 481 | private: |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 482 | DeferredHandles(Object** first_block_limit, Isolate* isolate) |
| 483 | : next_(NULL), |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 484 | previous_(NULL), |
| 485 | first_block_limit_(first_block_limit), |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 486 | isolate_(isolate) { |
| 487 | isolate->LinkDeferredHandles(this); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 488 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 489 | |
| 490 | void Iterate(ObjectVisitor* v); |
| 491 | |
| 492 | List<Object**> blocks_; |
| 493 | DeferredHandles* next_; |
| 494 | DeferredHandles* previous_; |
| 495 | Object** first_block_limit_; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 496 | Isolate* isolate_; |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 497 | |
| 498 | friend class HandleScopeImplementer; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 499 | friend class Isolate; |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 500 | }; |
| 501 | |
| 502 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 503 | // This class is here in order to be able to declare it a friend of |
| 504 | // HandleScope. Moving these methods to be members of HandleScope would be |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 505 | // neat in some ways, but it would expose internal implementation details in |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 506 | // our public header file, which is undesirable. |
| 507 | // |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 508 | // An isolate has a single instance of this class to hold the current thread's |
| 509 | // data. In multithreaded V8 programs this data is copied in and out of storage |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 510 | // so that the currently executing thread always has its own copy of this |
| 511 | // data. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 512 | class HandleScopeImplementer { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 513 | public: |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 514 | explicit HandleScopeImplementer(Isolate* isolate) |
| 515 | : isolate_(isolate), |
| 516 | blocks_(0), |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 517 | entered_contexts_(0), |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 518 | saved_contexts_(0), |
| 519 | spare_(NULL), |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 520 | call_depth_(0), |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 521 | last_handle_before_deferred_block_(NULL) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 522 | |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 523 | ~HandleScopeImplementer() { |
| 524 | DeleteArray(spare_); |
| 525 | } |
| 526 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 527 | // Threading support for handle data. |
| 528 | static int ArchiveSpacePerThread(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 529 | char* RestoreThread(char* from); |
| 530 | char* ArchiveThread(char* to); |
| 531 | void FreeThreadResources(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | |
| 533 | // Garbage collection support. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 534 | void Iterate(v8::internal::ObjectVisitor* v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 535 | static char* Iterate(v8::internal::ObjectVisitor* v, char* data); |
| 536 | |
| 537 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 538 | inline internal::Object** GetSpareOrNewBlock(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 539 | inline void DeleteExtensions(internal::Object** prev_limit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 540 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 541 | inline void IncrementCallDepth() {call_depth_++;} |
| 542 | inline void DecrementCallDepth() {call_depth_--;} |
| 543 | inline bool CallDepthIsZero() { return call_depth_ == 0; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 544 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 545 | inline void EnterContext(Handle<Object> context); |
| 546 | inline bool LeaveLastContext(); |
| 547 | |
| 548 | // Returns the last entered context or an empty handle if no |
| 549 | // contexts have been entered. |
| 550 | inline Handle<Object> LastEnteredContext(); |
| 551 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 552 | inline void SaveContext(Context* context); |
| 553 | inline Context* RestoreContext(); |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 554 | inline bool HasSavedContexts(); |
| 555 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 556 | inline List<internal::Object**>* blocks() { return &blocks_; } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 557 | Isolate* isolate() const { return isolate_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 558 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 559 | void ReturnBlock(Object** block) { |
| 560 | ASSERT(block != NULL); |
| 561 | if (spare_ != NULL) DeleteArray(spare_); |
| 562 | spare_ = block; |
| 563 | } |
| 564 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 565 | private: |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 566 | void ResetAfterArchive() { |
| 567 | blocks_.Initialize(0); |
| 568 | entered_contexts_.Initialize(0); |
| 569 | saved_contexts_.Initialize(0); |
| 570 | spare_ = NULL; |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 571 | last_handle_before_deferred_block_ = NULL; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 572 | call_depth_ = 0; |
| 573 | } |
| 574 | |
| 575 | void Free() { |
| 576 | ASSERT(blocks_.length() == 0); |
| 577 | ASSERT(entered_contexts_.length() == 0); |
| 578 | ASSERT(saved_contexts_.length() == 0); |
| 579 | blocks_.Free(); |
| 580 | entered_contexts_.Free(); |
| 581 | saved_contexts_.Free(); |
| 582 | if (spare_ != NULL) { |
| 583 | DeleteArray(spare_); |
| 584 | spare_ = NULL; |
| 585 | } |
| 586 | ASSERT(call_depth_ == 0); |
| 587 | } |
| 588 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 589 | void BeginDeferredScope(); |
| 590 | DeferredHandles* Detach(Object** prev_limit); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 591 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 592 | Isolate* isolate_; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 593 | List<internal::Object**> blocks_; |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 594 | // Used as a stack to keep track of entered contexts. |
| 595 | List<Handle<Object> > entered_contexts_; |
| 596 | // Used as a stack to keep track of saved contexts. |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 597 | List<Context*> saved_contexts_; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 598 | Object** spare_; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 599 | int call_depth_; |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 600 | Object** last_handle_before_deferred_block_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 601 | // This is only used for threading support. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 602 | v8::ImplementationUtilities::HandleScopeData handle_scope_data_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 603 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 604 | void IterateThis(ObjectVisitor* v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 605 | char* RestoreThreadHelper(char* from); |
| 606 | char* ArchiveThreadHelper(char* to); |
| 607 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 608 | friend class DeferredHandles; |
| 609 | friend class DeferredHandleScope; |
| 610 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 611 | DISALLOW_COPY_AND_ASSIGN(HandleScopeImplementer); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 612 | }; |
| 613 | |
| 614 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 615 | const int kHandleBlockSize = v8::internal::KB - 2; // fit in one page |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 616 | |
| 617 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 618 | void HandleScopeImplementer::SaveContext(Context* context) { |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 619 | saved_contexts_.Add(context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 623 | Context* HandleScopeImplementer::RestoreContext() { |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 624 | return saved_contexts_.RemoveLast(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 628 | bool HandleScopeImplementer::HasSavedContexts() { |
| 629 | return !saved_contexts_.is_empty(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 633 | void HandleScopeImplementer::EnterContext(Handle<Object> context) { |
| 634 | entered_contexts_.Add(context); |
| 635 | } |
| 636 | |
| 637 | |
| 638 | bool HandleScopeImplementer::LeaveLastContext() { |
| 639 | if (entered_contexts_.is_empty()) return false; |
| 640 | entered_contexts_.RemoveLast(); |
| 641 | return true; |
| 642 | } |
| 643 | |
| 644 | |
| 645 | Handle<Object> HandleScopeImplementer::LastEnteredContext() { |
| 646 | if (entered_contexts_.is_empty()) return Handle<Object>::null(); |
| 647 | return entered_contexts_.last(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | |
| 651 | // If there's a spare block, use it for growing the current scope. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 652 | internal::Object** HandleScopeImplementer::GetSpareOrNewBlock() { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 653 | internal::Object** block = (spare_ != NULL) ? |
| 654 | spare_ : |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 655 | NewArray<internal::Object*>(kHandleBlockSize); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 656 | spare_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 657 | return block; |
| 658 | } |
| 659 | |
| 660 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 661 | void HandleScopeImplementer::DeleteExtensions(internal::Object** prev_limit) { |
| 662 | while (!blocks_.is_empty()) { |
| 663 | internal::Object** block_start = blocks_.last(); |
| 664 | internal::Object** block_limit = block_start + kHandleBlockSize; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 665 | #ifdef DEBUG |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 666 | // SealHandleScope may make the prev_limit to point inside the block. |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 667 | if (block_start <= prev_limit && prev_limit <= block_limit) { |
| 668 | #ifdef ENABLE_EXTRA_CHECKS |
| 669 | internal::HandleScope::ZapRange(prev_limit, block_limit); |
| 670 | #endif |
| 671 | break; |
| 672 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 673 | #else |
| 674 | if (prev_limit == block_limit) break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 675 | #endif |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 676 | |
| 677 | blocks_.RemoveLast(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 678 | #ifdef ENABLE_EXTRA_CHECKS |
| 679 | internal::HandleScope::ZapRange(block_start, block_limit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 680 | #endif |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 681 | if (spare_ != NULL) { |
| 682 | DeleteArray(spare_); |
| 683 | } |
| 684 | spare_ = block_start; |
| 685 | } |
| 686 | ASSERT((blocks_.is_empty() && prev_limit == NULL) || |
| 687 | (!blocks_.is_empty() && prev_limit != NULL)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 688 | } |
| 689 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 690 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 691 | // Interceptor functions called from generated inline caches to notify |
| 692 | // CPU profiler that external callbacks are invoked. |
| 693 | v8::Handle<v8::Value> InvokeAccessorGetter( |
| 694 | v8::Local<v8::String> property, |
| 695 | const v8::AccessorInfo& info, |
| 696 | v8::AccessorGetter getter); |
| 697 | |
| 698 | |
| 699 | void InvokeAccessorGetterCallback( |
| 700 | v8::Local<v8::String> property, |
| 701 | const v8::PropertyCallbackInfo<v8::Value>& info, |
| 702 | v8::AccessorGetterCallback getter); |
| 703 | |
| 704 | v8::Handle<v8::Value> InvokeInvocationCallback(const v8::Arguments& args, |
| 705 | v8::InvocationCallback callback); |
| 706 | void InvokeFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info, |
| 707 | v8::FunctionCallback callback); |
| 708 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 709 | class Testing { |
| 710 | public: |
| 711 | static v8::Testing::StressType stress_type() { return stress_type_; } |
| 712 | static void set_stress_type(v8::Testing::StressType stress_type) { |
| 713 | stress_type_ = stress_type; |
| 714 | } |
| 715 | |
| 716 | private: |
| 717 | static v8::Testing::StressType stress_type_; |
| 718 | }; |
| 719 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 720 | } } // namespace v8::internal |
| 721 | |
| 722 | #endif // V8_API_H_ |