Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_API_H_ |
| 6 | #define V8_API_H_ |
| 7 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "include/v8-testing.h" |
| 9 | #include "src/contexts.h" |
| 10 | #include "src/factory.h" |
| 11 | #include "src/isolate.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12 | #include "src/list.h" |
| 13 | #include "src/objects-inl.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15 | namespace v8 { |
| 16 | |
| 17 | // Constants used in the implementation of the API. The most natural thing |
| 18 | // would usually be to place these with the classes that use them, but |
| 19 | // we want to keep them out of v8.h because it is an externally |
| 20 | // visible file. |
| 21 | class Consts { |
| 22 | public: |
| 23 | enum TemplateType { |
| 24 | FUNCTION_TEMPLATE = 0, |
| 25 | OBJECT_TEMPLATE = 1 |
| 26 | }; |
| 27 | }; |
| 28 | |
| 29 | |
| 30 | // Utilities for working with neander-objects, primitive |
| 31 | // env-independent JSObjects used by the api. |
| 32 | class NeanderObject { |
| 33 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | explicit NeanderObject(v8::internal::Isolate* isolate, int size); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 35 | explicit inline NeanderObject(v8::internal::Handle<v8::internal::Object> obj); |
| 36 | explicit inline NeanderObject(v8::internal::Object* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 37 | inline v8::internal::Object* get(int index); |
| 38 | inline void set(int index, v8::internal::Object* value); |
| 39 | inline v8::internal::Handle<v8::internal::JSObject> value() { return value_; } |
| 40 | int size(); |
| 41 | private: |
| 42 | v8::internal::Handle<v8::internal::JSObject> value_; |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | // Utilities for working with neander-arrays, a simple extensible |
| 47 | // array abstraction built on neander-objects. |
| 48 | class NeanderArray { |
| 49 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 50 | explicit NeanderArray(v8::internal::Isolate* isolate); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 51 | explicit inline NeanderArray(v8::internal::Handle<v8::internal::Object> obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | inline v8::internal::Handle<v8::internal::JSObject> value() { |
| 53 | return obj_.value(); |
| 54 | } |
| 55 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 56 | void add(internal::Isolate* isolate, |
| 57 | v8::internal::Handle<v8::internal::Object> value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | |
| 59 | int length(); |
| 60 | |
| 61 | v8::internal::Object* get(int index); |
| 62 | // Change the value at an index to undefined value. If the index is |
| 63 | // out of bounds, the request is ignored. Returns the old value. |
| 64 | void set(int index, v8::internal::Object* value); |
| 65 | private: |
| 66 | NeanderObject obj_; |
| 67 | }; |
| 68 | |
| 69 | |
| 70 | NeanderObject::NeanderObject(v8::internal::Handle<v8::internal::Object> obj) |
| 71 | : value_(v8::internal::Handle<v8::internal::JSObject>::cast(obj)) { } |
| 72 | |
| 73 | |
| 74 | NeanderObject::NeanderObject(v8::internal::Object* obj) |
| 75 | : value_(v8::internal::Handle<v8::internal::JSObject>( |
| 76 | v8::internal::JSObject::cast(obj))) { } |
| 77 | |
| 78 | |
| 79 | NeanderArray::NeanderArray(v8::internal::Handle<v8::internal::Object> obj) |
| 80 | : obj_(obj) { } |
| 81 | |
| 82 | |
| 83 | v8::internal::Object* NeanderObject::get(int offset) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 84 | DCHECK(value()->HasFastObjectElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 85 | return v8::internal::FixedArray::cast(value()->elements())->get(offset); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | void NeanderObject::set(int offset, v8::internal::Object* value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 90 | DCHECK(value_->HasFastObjectElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 91 | v8::internal::FixedArray::cast(value_->elements())->set(offset, value); |
| 92 | } |
| 93 | |
| 94 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 95 | template <typename T> inline T ToCData(v8::internal::Object* obj) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 96 | STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 97 | if (obj == v8::internal::Smi::FromInt(0)) return nullptr; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | return reinterpret_cast<T>( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 99 | reinterpret_cast<intptr_t>( |
| 100 | v8::internal::Foreign::cast(obj)->foreign_address())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | |
| 104 | template <typename T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 105 | inline v8::internal::Handle<v8::internal::Object> FromCData( |
| 106 | v8::internal::Isolate* isolate, T obj) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 108 | if (obj == nullptr) return handle(v8::internal::Smi::FromInt(0), isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 109 | return isolate->factory()->NewForeign( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 110 | reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(obj))); |
| 111 | } |
| 112 | |
| 113 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 114 | class ApiFunction { |
| 115 | public: |
| 116 | explicit ApiFunction(v8::internal::Address addr) : addr_(addr) { } |
| 117 | v8::internal::Address address() { return addr_; } |
| 118 | private: |
| 119 | v8::internal::Address addr_; |
| 120 | }; |
| 121 | |
| 122 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 123 | |
| 124 | class RegisteredExtension { |
| 125 | public: |
| 126 | explicit RegisteredExtension(Extension* extension); |
| 127 | static void Register(RegisteredExtension* that); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 128 | static void UnregisterAll(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 129 | Extension* extension() { return extension_; } |
| 130 | RegisteredExtension* next() { return next_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 131 | static RegisteredExtension* first_extension() { return first_extension_; } |
| 132 | private: |
| 133 | Extension* extension_; |
| 134 | RegisteredExtension* next_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 135 | static RegisteredExtension* first_extension_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 139 | #define OPEN_HANDLE_LIST(V) \ |
| 140 | V(Template, TemplateInfo) \ |
| 141 | V(FunctionTemplate, FunctionTemplateInfo) \ |
| 142 | V(ObjectTemplate, ObjectTemplateInfo) \ |
| 143 | V(Signature, FunctionTemplateInfo) \ |
| 144 | V(AccessorSignature, FunctionTemplateInfo) \ |
| 145 | V(Data, Object) \ |
| 146 | V(RegExp, JSRegExp) \ |
| 147 | V(Object, JSReceiver) \ |
| 148 | V(Array, JSArray) \ |
| 149 | V(Map, JSMap) \ |
| 150 | V(Set, JSSet) \ |
| 151 | V(ArrayBuffer, JSArrayBuffer) \ |
| 152 | V(ArrayBufferView, JSArrayBufferView) \ |
| 153 | V(TypedArray, JSTypedArray) \ |
| 154 | V(Uint8Array, JSTypedArray) \ |
| 155 | V(Uint8ClampedArray, JSTypedArray) \ |
| 156 | V(Int8Array, JSTypedArray) \ |
| 157 | V(Uint16Array, JSTypedArray) \ |
| 158 | V(Int16Array, JSTypedArray) \ |
| 159 | V(Uint32Array, JSTypedArray) \ |
| 160 | V(Int32Array, JSTypedArray) \ |
| 161 | V(Float32Array, JSTypedArray) \ |
| 162 | V(Float64Array, JSTypedArray) \ |
| 163 | V(DataView, JSDataView) \ |
| 164 | V(SharedArrayBuffer, JSArrayBuffer) \ |
| 165 | V(Name, Name) \ |
| 166 | V(String, String) \ |
| 167 | V(Symbol, Symbol) \ |
| 168 | V(Script, JSFunction) \ |
| 169 | V(UnboundScript, SharedFunctionInfo) \ |
| 170 | V(Function, JSReceiver) \ |
| 171 | V(Message, JSMessageObject) \ |
| 172 | V(Context, Context) \ |
| 173 | V(External, Object) \ |
| 174 | V(StackTrace, JSArray) \ |
| 175 | V(StackFrame, JSObject) \ |
| 176 | V(Proxy, JSProxy) \ |
| 177 | V(NativeWeakMap, JSWeakMap) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 178 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 179 | class Utils { |
| 180 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 181 | static inline bool ApiCheck(bool condition, |
| 182 | const char* location, |
| 183 | const char* message) { |
| 184 | if (!condition) Utils::ReportApiFailure(location, message); |
| 185 | return condition; |
| 186 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | |
| 188 | static Local<FunctionTemplate> ToFunctionTemplate(NeanderObject obj); |
| 189 | static Local<ObjectTemplate> ToObjectTemplate(NeanderObject obj); |
| 190 | |
| 191 | static inline Local<Context> ToLocal( |
| 192 | v8::internal::Handle<v8::internal::Context> obj); |
| 193 | static inline Local<Value> ToLocal( |
| 194 | v8::internal::Handle<v8::internal::Object> obj); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 195 | static inline Local<Name> ToLocal( |
| 196 | v8::internal::Handle<v8::internal::Name> obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 197 | static inline Local<String> ToLocal( |
| 198 | v8::internal::Handle<v8::internal::String> obj); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 199 | static inline Local<Symbol> ToLocal( |
| 200 | v8::internal::Handle<v8::internal::Symbol> obj); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 201 | static inline Local<RegExp> ToLocal( |
| 202 | v8::internal::Handle<v8::internal::JSRegExp> obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | static inline Local<Object> ToLocal( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 204 | v8::internal::Handle<v8::internal::JSReceiver> obj); |
| 205 | static inline Local<Object> ToLocal( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 206 | v8::internal::Handle<v8::internal::JSObject> obj); |
| 207 | static inline Local<Array> ToLocal( |
| 208 | v8::internal::Handle<v8::internal::JSArray> obj); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 209 | static inline Local<Map> ToLocal( |
| 210 | v8::internal::Handle<v8::internal::JSMap> obj); |
| 211 | static inline Local<Set> ToLocal( |
| 212 | v8::internal::Handle<v8::internal::JSSet> obj); |
| 213 | static inline Local<Proxy> ToLocal( |
| 214 | v8::internal::Handle<v8::internal::JSProxy> obj); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 215 | static inline Local<ArrayBuffer> ToLocal( |
| 216 | v8::internal::Handle<v8::internal::JSArrayBuffer> obj); |
| 217 | static inline Local<ArrayBufferView> ToLocal( |
| 218 | v8::internal::Handle<v8::internal::JSArrayBufferView> obj); |
| 219 | static inline Local<DataView> ToLocal( |
| 220 | v8::internal::Handle<v8::internal::JSDataView> obj); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 221 | static inline Local<TypedArray> ToLocal( |
| 222 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 223 | static inline Local<Uint8Array> ToLocalUint8Array( |
| 224 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 225 | static inline Local<Uint8ClampedArray> ToLocalUint8ClampedArray( |
| 226 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 227 | static inline Local<Int8Array> ToLocalInt8Array( |
| 228 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 229 | static inline Local<Uint16Array> ToLocalUint16Array( |
| 230 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 231 | static inline Local<Int16Array> ToLocalInt16Array( |
| 232 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 233 | static inline Local<Uint32Array> ToLocalUint32Array( |
| 234 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 235 | static inline Local<Int32Array> ToLocalInt32Array( |
| 236 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 237 | static inline Local<Float32Array> ToLocalFloat32Array( |
| 238 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 239 | static inline Local<Float64Array> ToLocalFloat64Array( |
| 240 | v8::internal::Handle<v8::internal::JSTypedArray> obj); |
| 241 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 242 | static inline Local<SharedArrayBuffer> ToLocalShared( |
| 243 | v8::internal::Handle<v8::internal::JSArrayBuffer> obj); |
| 244 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 245 | static inline Local<Message> MessageToLocal( |
| 246 | v8::internal::Handle<v8::internal::Object> obj); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 247 | static inline Local<Promise> PromiseToLocal( |
| 248 | v8::internal::Handle<v8::internal::JSObject> obj); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 249 | static inline Local<StackTrace> StackTraceToLocal( |
| 250 | v8::internal::Handle<v8::internal::JSArray> obj); |
| 251 | static inline Local<StackFrame> StackFrameToLocal( |
| 252 | v8::internal::Handle<v8::internal::JSObject> obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 253 | static inline Local<Number> NumberToLocal( |
| 254 | v8::internal::Handle<v8::internal::Object> obj); |
| 255 | static inline Local<Integer> IntegerToLocal( |
| 256 | v8::internal::Handle<v8::internal::Object> obj); |
| 257 | static inline Local<Uint32> Uint32ToLocal( |
| 258 | v8::internal::Handle<v8::internal::Object> obj); |
| 259 | static inline Local<FunctionTemplate> ToLocal( |
| 260 | v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj); |
| 261 | static inline Local<ObjectTemplate> ToLocal( |
| 262 | v8::internal::Handle<v8::internal::ObjectTemplateInfo> obj); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 263 | static inline Local<Signature> SignatureToLocal( |
| 264 | v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 265 | static inline Local<AccessorSignature> AccessorSignatureToLocal( |
| 266 | v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 267 | static inline Local<External> ExternalToLocal( |
| 268 | v8::internal::Handle<v8::internal::JSObject> obj); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 269 | static inline Local<NativeWeakMap> NativeWeakMapToLocal( |
| 270 | v8::internal::Handle<v8::internal::JSWeakMap> obj); |
| 271 | static inline Local<Function> CallableToLocal( |
| 272 | v8::internal::Handle<v8::internal::JSReceiver> obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 273 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 274 | #define DECLARE_OPEN_HANDLE(From, To) \ |
| 275 | static inline v8::internal::Handle<v8::internal::To> \ |
| 276 | OpenHandle(const From* that, bool allow_empty_handle = false); |
| 277 | |
| 278 | OPEN_HANDLE_LIST(DECLARE_OPEN_HANDLE) |
| 279 | |
| 280 | #undef DECLARE_OPEN_HANDLE |
| 281 | |
| 282 | template<class From, class To> |
| 283 | static inline Local<To> Convert(v8::internal::Handle<From> obj) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 284 | DCHECK(obj.is_null() || |
| 285 | (obj->IsSmi() || |
| 286 | !obj->IsTheHole(i::HeapObject::cast(*obj)->GetIsolate()))); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 287 | return Local<To>(reinterpret_cast<To*>(obj.location())); |
| 288 | } |
| 289 | |
| 290 | template <class T> |
| 291 | static inline v8::internal::Handle<v8::internal::Object> OpenPersistent( |
| 292 | const v8::Persistent<T>& persistent) { |
| 293 | return v8::internal::Handle<v8::internal::Object>( |
| 294 | reinterpret_cast<v8::internal::Object**>(persistent.val_)); |
| 295 | } |
| 296 | |
| 297 | template <class T> |
| 298 | static inline v8::internal::Handle<v8::internal::Object> OpenPersistent( |
| 299 | v8::Persistent<T>* persistent) { |
| 300 | return OpenPersistent(*persistent); |
| 301 | } |
| 302 | |
| 303 | template <class From, class To> |
| 304 | static inline v8::internal::Handle<To> OpenHandle(v8::Local<From> handle) { |
| 305 | return OpenHandle(*handle); |
| 306 | } |
| 307 | |
| 308 | private: |
| 309 | static void ReportApiFailure(const char* location, const char* message); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 310 | }; |
| 311 | |
| 312 | |
| 313 | template <class T> |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 314 | inline T* ToApi(v8::internal::Handle<v8::internal::Object> obj) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 315 | return reinterpret_cast<T*>(obj.location()); |
| 316 | } |
| 317 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 318 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 319 | inline v8::Local<T> ToApiHandle( |
| 320 | v8::internal::Handle<v8::internal::Object> obj) { |
| 321 | return Utils::Convert<v8::internal::Object, T>(obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 325 | template <class T> |
| 326 | inline bool ToLocal(v8::internal::MaybeHandle<v8::internal::Object> maybe, |
| 327 | Local<T>* local) { |
| 328 | v8::internal::Handle<v8::internal::Object> handle; |
| 329 | if (maybe.ToHandle(&handle)) { |
| 330 | *local = Utils::Convert<v8::internal::Object, T>(handle); |
| 331 | return true; |
| 332 | } |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 337 | // Implementations of ToLocal |
| 338 | |
| 339 | #define MAKE_TO_LOCAL(Name, From, To) \ |
| 340 | Local<v8::To> Utils::Name(v8::internal::Handle<v8::internal::From> obj) { \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 341 | return Convert<v8::internal::From, v8::To>(obj); \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 344 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 345 | #define MAKE_TO_LOCAL_TYPED_ARRAY(Type, typeName, TYPE, ctype, size) \ |
| 346 | Local<v8::Type##Array> Utils::ToLocal##Type##Array( \ |
| 347 | v8::internal::Handle<v8::internal::JSTypedArray> obj) { \ |
| 348 | DCHECK(obj->type() == v8::internal::kExternal##Type##Array); \ |
| 349 | return Convert<v8::internal::JSTypedArray, v8::Type##Array>(obj); \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 353 | MAKE_TO_LOCAL(ToLocal, Context, Context) |
| 354 | MAKE_TO_LOCAL(ToLocal, Object, Value) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 355 | MAKE_TO_LOCAL(ToLocal, Name, Name) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 356 | MAKE_TO_LOCAL(ToLocal, String, String) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 357 | MAKE_TO_LOCAL(ToLocal, Symbol, Symbol) |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 358 | MAKE_TO_LOCAL(ToLocal, JSRegExp, RegExp) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 359 | MAKE_TO_LOCAL(ToLocal, JSReceiver, Object) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 360 | MAKE_TO_LOCAL(ToLocal, JSObject, Object) |
| 361 | MAKE_TO_LOCAL(ToLocal, JSArray, Array) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 362 | MAKE_TO_LOCAL(ToLocal, JSMap, Map) |
| 363 | MAKE_TO_LOCAL(ToLocal, JSSet, Set) |
| 364 | MAKE_TO_LOCAL(ToLocal, JSProxy, Proxy) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 365 | MAKE_TO_LOCAL(ToLocal, JSArrayBuffer, ArrayBuffer) |
| 366 | MAKE_TO_LOCAL(ToLocal, JSArrayBufferView, ArrayBufferView) |
| 367 | MAKE_TO_LOCAL(ToLocal, JSDataView, DataView) |
| 368 | MAKE_TO_LOCAL(ToLocal, JSTypedArray, TypedArray) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 369 | MAKE_TO_LOCAL(ToLocalShared, JSArrayBuffer, SharedArrayBuffer) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 370 | |
| 371 | TYPED_ARRAYS(MAKE_TO_LOCAL_TYPED_ARRAY) |
| 372 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 373 | MAKE_TO_LOCAL(ToLocal, FunctionTemplateInfo, FunctionTemplate) |
| 374 | MAKE_TO_LOCAL(ToLocal, ObjectTemplateInfo, ObjectTemplate) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 375 | MAKE_TO_LOCAL(SignatureToLocal, FunctionTemplateInfo, Signature) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 376 | MAKE_TO_LOCAL(AccessorSignatureToLocal, FunctionTemplateInfo, AccessorSignature) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 377 | MAKE_TO_LOCAL(MessageToLocal, Object, Message) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 378 | MAKE_TO_LOCAL(PromiseToLocal, JSObject, Promise) |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 379 | MAKE_TO_LOCAL(StackTraceToLocal, JSArray, StackTrace) |
| 380 | MAKE_TO_LOCAL(StackFrameToLocal, JSObject, StackFrame) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 381 | MAKE_TO_LOCAL(NumberToLocal, Object, Number) |
| 382 | MAKE_TO_LOCAL(IntegerToLocal, Object, Integer) |
| 383 | MAKE_TO_LOCAL(Uint32ToLocal, Object, Uint32) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 384 | MAKE_TO_LOCAL(ExternalToLocal, JSObject, External) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 385 | MAKE_TO_LOCAL(NativeWeakMapToLocal, JSWeakMap, NativeWeakMap) |
| 386 | MAKE_TO_LOCAL(CallableToLocal, JSReceiver, Function) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 387 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 388 | #undef MAKE_TO_LOCAL_TYPED_ARRAY |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 389 | #undef MAKE_TO_LOCAL |
| 390 | |
| 391 | |
| 392 | // Implementations of OpenHandle |
| 393 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 394 | #define MAKE_OPEN_HANDLE(From, To) \ |
| 395 | v8::internal::Handle<v8::internal::To> Utils::OpenHandle( \ |
| 396 | const v8::From* that, bool allow_empty_handle) { \ |
| 397 | DCHECK(allow_empty_handle || that != NULL); \ |
| 398 | DCHECK(that == NULL || \ |
| 399 | (*reinterpret_cast<v8::internal::Object* const*>(that))->Is##To()); \ |
| 400 | return v8::internal::Handle<v8::internal::To>( \ |
| 401 | reinterpret_cast<v8::internal::To**>(const_cast<v8::From*>(that))); \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 404 | OPEN_HANDLE_LIST(MAKE_OPEN_HANDLE) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 405 | |
| 406 | #undef MAKE_OPEN_HANDLE |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 407 | #undef OPEN_HANDLE_LIST |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 408 | |
| 409 | |
| 410 | namespace internal { |
| 411 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 412 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 413 | class DeferredHandles { |
| 414 | public: |
| 415 | ~DeferredHandles(); |
| 416 | |
| 417 | private: |
| 418 | DeferredHandles(Object** first_block_limit, Isolate* isolate) |
| 419 | : next_(NULL), |
| 420 | previous_(NULL), |
| 421 | first_block_limit_(first_block_limit), |
| 422 | isolate_(isolate) { |
| 423 | isolate->LinkDeferredHandles(this); |
| 424 | } |
| 425 | |
| 426 | void Iterate(ObjectVisitor* v); |
| 427 | |
| 428 | List<Object**> blocks_; |
| 429 | DeferredHandles* next_; |
| 430 | DeferredHandles* previous_; |
| 431 | Object** first_block_limit_; |
| 432 | Isolate* isolate_; |
| 433 | |
| 434 | friend class HandleScopeImplementer; |
| 435 | friend class Isolate; |
| 436 | }; |
| 437 | |
| 438 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 439 | // This class is here in order to be able to declare it a friend of |
| 440 | // HandleScope. Moving these methods to be members of HandleScope would be |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 441 | // neat in some ways, but it would expose internal implementation details in |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 442 | // our public header file, which is undesirable. |
| 443 | // |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 444 | // An isolate has a single instance of this class to hold the current thread's |
| 445 | // data. In multithreaded V8 programs this data is copied in and out of storage |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 446 | // so that the currently executing thread always has its own copy of this |
| 447 | // data. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 448 | class HandleScopeImplementer { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 449 | public: |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 450 | explicit HandleScopeImplementer(Isolate* isolate) |
| 451 | : isolate_(isolate), |
| 452 | blocks_(0), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 453 | entered_contexts_(0), |
| 454 | saved_contexts_(0), |
| 455 | spare_(NULL), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 456 | call_depth_(0), |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 457 | microtasks_depth_(0), |
| 458 | microtasks_suppressions_(0), |
| 459 | #ifdef DEBUG |
| 460 | debug_microtasks_depth_(0), |
| 461 | #endif |
| 462 | microtasks_policy_(v8::MicrotasksPolicy::kAuto), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 463 | last_handle_before_deferred_block_(NULL) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 464 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 465 | ~HandleScopeImplementer() { |
| 466 | DeleteArray(spare_); |
| 467 | } |
| 468 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 469 | // Threading support for handle data. |
| 470 | static int ArchiveSpacePerThread(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 471 | char* RestoreThread(char* from); |
| 472 | char* ArchiveThread(char* to); |
| 473 | void FreeThreadResources(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 474 | |
| 475 | // Garbage collection support. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 476 | void Iterate(v8::internal::ObjectVisitor* v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 477 | static char* Iterate(v8::internal::ObjectVisitor* v, char* data); |
| 478 | |
| 479 | |
| 480 | inline internal::Object** GetSpareOrNewBlock(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 481 | inline void DeleteExtensions(internal::Object** prev_limit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 482 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 483 | // Call depth represents nested v8 api calls. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 484 | inline void IncrementCallDepth() {call_depth_++;} |
| 485 | inline void DecrementCallDepth() {call_depth_--;} |
| 486 | inline bool CallDepthIsZero() { return call_depth_ == 0; } |
| 487 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 488 | // Microtasks scope depth represents nested scopes controlling microtasks |
| 489 | // invocation, which happens when depth reaches zero. |
| 490 | inline void IncrementMicrotasksScopeDepth() {microtasks_depth_++;} |
| 491 | inline void DecrementMicrotasksScopeDepth() {microtasks_depth_--;} |
| 492 | inline int GetMicrotasksScopeDepth() { return microtasks_depth_; } |
| 493 | |
| 494 | // Possibly nested microtasks suppression scopes prevent microtasks |
| 495 | // from running. |
| 496 | inline void IncrementMicrotasksSuppressions() {microtasks_suppressions_++;} |
| 497 | inline void DecrementMicrotasksSuppressions() {microtasks_suppressions_--;} |
| 498 | inline bool HasMicrotasksSuppressions() { return !!microtasks_suppressions_; } |
| 499 | |
| 500 | #ifdef DEBUG |
| 501 | // In debug we check that calls not intended to invoke microtasks are |
| 502 | // still correctly wrapped with microtask scopes. |
| 503 | inline void IncrementDebugMicrotasksScopeDepth() {debug_microtasks_depth_++;} |
| 504 | inline void DecrementDebugMicrotasksScopeDepth() {debug_microtasks_depth_--;} |
| 505 | inline bool DebugMicrotasksScopeDepthIsZero() { |
| 506 | return debug_microtasks_depth_ == 0; |
| 507 | } |
| 508 | #endif |
| 509 | |
| 510 | inline void set_microtasks_policy(v8::MicrotasksPolicy policy); |
| 511 | inline v8::MicrotasksPolicy microtasks_policy() const; |
| 512 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 513 | inline void EnterContext(Handle<Context> context); |
| 514 | inline void LeaveContext(); |
| 515 | inline bool LastEnteredContextWas(Handle<Context> context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 516 | |
| 517 | // Returns the last entered context or an empty handle if no |
| 518 | // contexts have been entered. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 519 | inline Handle<Context> LastEnteredContext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 520 | |
| 521 | inline void SaveContext(Context* context); |
| 522 | inline Context* RestoreContext(); |
| 523 | inline bool HasSavedContexts(); |
| 524 | |
| 525 | inline List<internal::Object**>* blocks() { return &blocks_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 526 | Isolate* isolate() const { return isolate_; } |
| 527 | |
| 528 | void ReturnBlock(Object** block) { |
| 529 | DCHECK(block != NULL); |
| 530 | if (spare_ != NULL) DeleteArray(spare_); |
| 531 | spare_ = block; |
| 532 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 533 | |
| 534 | private: |
| 535 | void ResetAfterArchive() { |
| 536 | blocks_.Initialize(0); |
| 537 | entered_contexts_.Initialize(0); |
| 538 | saved_contexts_.Initialize(0); |
| 539 | spare_ = NULL; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 540 | last_handle_before_deferred_block_ = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 541 | call_depth_ = 0; |
| 542 | } |
| 543 | |
| 544 | void Free() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 545 | DCHECK(blocks_.length() == 0); |
| 546 | DCHECK(entered_contexts_.length() == 0); |
| 547 | DCHECK(saved_contexts_.length() == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 548 | blocks_.Free(); |
| 549 | entered_contexts_.Free(); |
| 550 | saved_contexts_.Free(); |
| 551 | if (spare_ != NULL) { |
| 552 | DeleteArray(spare_); |
| 553 | spare_ = NULL; |
| 554 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 555 | DCHECK(call_depth_ == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 558 | void BeginDeferredScope(); |
| 559 | DeferredHandles* Detach(Object** prev_limit); |
| 560 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 561 | Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 562 | List<internal::Object**> blocks_; |
| 563 | // Used as a stack to keep track of entered contexts. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 564 | List<Context*> entered_contexts_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 565 | // Used as a stack to keep track of saved contexts. |
| 566 | List<Context*> saved_contexts_; |
| 567 | Object** spare_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 568 | int call_depth_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 569 | int microtasks_depth_; |
| 570 | int microtasks_suppressions_; |
| 571 | #ifdef DEBUG |
| 572 | int debug_microtasks_depth_; |
| 573 | #endif |
| 574 | v8::MicrotasksPolicy microtasks_policy_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 575 | Object** last_handle_before_deferred_block_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 576 | // This is only used for threading support. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 577 | HandleScopeData handle_scope_data_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 578 | |
| 579 | void IterateThis(ObjectVisitor* v); |
| 580 | char* RestoreThreadHelper(char* from); |
| 581 | char* ArchiveThreadHelper(char* to); |
| 582 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 583 | friend class DeferredHandles; |
| 584 | friend class DeferredHandleScope; |
| 585 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 586 | DISALLOW_COPY_AND_ASSIGN(HandleScopeImplementer); |
| 587 | }; |
| 588 | |
| 589 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 590 | const int kHandleBlockSize = v8::internal::KB - 2; // fit in one page |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 591 | |
| 592 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 593 | void HandleScopeImplementer::set_microtasks_policy( |
| 594 | v8::MicrotasksPolicy policy) { |
| 595 | microtasks_policy_ = policy; |
| 596 | } |
| 597 | |
| 598 | |
| 599 | v8::MicrotasksPolicy HandleScopeImplementer::microtasks_policy() const { |
| 600 | return microtasks_policy_; |
| 601 | } |
| 602 | |
| 603 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 604 | void HandleScopeImplementer::SaveContext(Context* context) { |
| 605 | saved_contexts_.Add(context); |
| 606 | } |
| 607 | |
| 608 | |
| 609 | Context* HandleScopeImplementer::RestoreContext() { |
| 610 | return saved_contexts_.RemoveLast(); |
| 611 | } |
| 612 | |
| 613 | |
| 614 | bool HandleScopeImplementer::HasSavedContexts() { |
| 615 | return !saved_contexts_.is_empty(); |
| 616 | } |
| 617 | |
| 618 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 619 | void HandleScopeImplementer::EnterContext(Handle<Context> context) { |
| 620 | entered_contexts_.Add(*context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 624 | void HandleScopeImplementer::LeaveContext() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 625 | entered_contexts_.RemoveLast(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 629 | bool HandleScopeImplementer::LastEnteredContextWas(Handle<Context> context) { |
| 630 | return !entered_contexts_.is_empty() && entered_contexts_.last() == *context; |
| 631 | } |
| 632 | |
| 633 | |
| 634 | Handle<Context> HandleScopeImplementer::LastEnteredContext() { |
| 635 | if (entered_contexts_.is_empty()) return Handle<Context>::null(); |
| 636 | return Handle<Context>(entered_contexts_.last()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | |
| 640 | // If there's a spare block, use it for growing the current scope. |
| 641 | internal::Object** HandleScopeImplementer::GetSpareOrNewBlock() { |
| 642 | internal::Object** block = (spare_ != NULL) ? |
| 643 | spare_ : |
| 644 | NewArray<internal::Object*>(kHandleBlockSize); |
| 645 | spare_ = NULL; |
| 646 | return block; |
| 647 | } |
| 648 | |
| 649 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 650 | void HandleScopeImplementer::DeleteExtensions(internal::Object** prev_limit) { |
| 651 | while (!blocks_.is_empty()) { |
| 652 | internal::Object** block_start = blocks_.last(); |
| 653 | internal::Object** block_limit = block_start + kHandleBlockSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 654 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 655 | // SealHandleScope may make the prev_limit to point inside the block. |
| 656 | if (block_start <= prev_limit && prev_limit <= block_limit) { |
| 657 | #ifdef ENABLE_HANDLE_ZAPPING |
| 658 | internal::HandleScope::ZapRange(prev_limit, block_limit); |
| 659 | #endif |
| 660 | break; |
| 661 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 662 | |
| 663 | blocks_.RemoveLast(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 664 | #ifdef ENABLE_HANDLE_ZAPPING |
| 665 | internal::HandleScope::ZapRange(block_start, block_limit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 666 | #endif |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 667 | if (spare_ != NULL) { |
| 668 | DeleteArray(spare_); |
| 669 | } |
| 670 | spare_ = block_start; |
| 671 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 672 | DCHECK((blocks_.is_empty() && prev_limit == NULL) || |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 673 | (!blocks_.is_empty() && prev_limit != NULL)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 676 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 677 | // Interceptor functions called from generated inline caches to notify |
| 678 | // CPU profiler that external callbacks are invoked. |
| 679 | void InvokeAccessorGetterCallback( |
| 680 | v8::Local<v8::Name> property, |
| 681 | const v8::PropertyCallbackInfo<v8::Value>& info, |
| 682 | v8::AccessorNameGetterCallback getter); |
| 683 | |
| 684 | void InvokeFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info, |
| 685 | v8::FunctionCallback callback); |
| 686 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 687 | class Testing { |
| 688 | public: |
| 689 | static v8::Testing::StressType stress_type() { return stress_type_; } |
| 690 | static void set_stress_type(v8::Testing::StressType stress_type) { |
| 691 | stress_type_ = stress_type; |
| 692 | } |
| 693 | |
| 694 | private: |
| 695 | static v8::Testing::StressType stress_type_; |
| 696 | }; |
| 697 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 698 | } // namespace internal |
| 699 | } // namespace v8 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 700 | |
| 701 | #endif // V8_API_H_ |