blob: cb2b5c386c2cce09c6ce062cbcc181b3c98a32d8 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_API_H_
6#define V8_API_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "include/v8-testing.h"
9#include "src/contexts.h"
10#include "src/factory.h"
11#include "src/isolate.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012#include "src/list.h"
13#include "src/objects-inl.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010014
Steve Blocka7e24c12009-10-30 11:49:00 +000015namespace 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.
21class 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.
32class NeanderObject {
33 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000034 explicit NeanderObject(v8::internal::Isolate* isolate, int size);
Ben Murdoch8b112d22011-06-08 16:22:53 +010035 explicit inline NeanderObject(v8::internal::Handle<v8::internal::Object> obj);
36 explicit inline NeanderObject(v8::internal::Object* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +000037 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.
48class NeanderArray {
49 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050 explicit NeanderArray(v8::internal::Isolate* isolate);
Ben Murdoch8b112d22011-06-08 16:22:53 +010051 explicit inline NeanderArray(v8::internal::Handle<v8::internal::Object> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +000052 inline v8::internal::Handle<v8::internal::JSObject> value() {
53 return obj_.value();
54 }
55
Emily Bernierd0a1eb72015-03-24 16:35:39 -040056 void add(internal::Isolate* isolate,
57 v8::internal::Handle<v8::internal::Object> value);
Steve Blocka7e24c12009-10-30 11:49:00 +000058
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
70NeanderObject::NeanderObject(v8::internal::Handle<v8::internal::Object> obj)
71 : value_(v8::internal::Handle<v8::internal::JSObject>::cast(obj)) { }
72
73
74NeanderObject::NeanderObject(v8::internal::Object* obj)
75 : value_(v8::internal::Handle<v8::internal::JSObject>(
76 v8::internal::JSObject::cast(obj))) { }
77
78
79NeanderArray::NeanderArray(v8::internal::Handle<v8::internal::Object> obj)
80 : obj_(obj) { }
81
82
83v8::internal::Object* NeanderObject::get(int offset) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000084 DCHECK(value()->HasFastObjectElements());
Steve Blocka7e24c12009-10-30 11:49:00 +000085 return v8::internal::FixedArray::cast(value()->elements())->get(offset);
86}
87
88
89void NeanderObject::set(int offset, v8::internal::Object* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090 DCHECK(value_->HasFastObjectElements());
Steve Blocka7e24c12009-10-30 11:49:00 +000091 v8::internal::FixedArray::cast(value_->elements())->set(offset, value);
92}
93
94
Ben Murdoch3ef787d2012-04-12 10:51:47 +010095template <typename T> inline T ToCData(v8::internal::Object* obj) {
Steve Blocka7e24c12009-10-30 11:49:00 +000096 STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097 if (obj == v8::internal::Smi::FromInt(0)) return nullptr;
Steve Blocka7e24c12009-10-30 11:49:00 +000098 return reinterpret_cast<T>(
Ben Murdoch3ef787d2012-04-12 10:51:47 +010099 reinterpret_cast<intptr_t>(
100 v8::internal::Foreign::cast(obj)->foreign_address()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000101}
102
103
104template <typename T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105inline v8::internal::Handle<v8::internal::Object> FromCData(
106 v8::internal::Isolate* isolate, T obj) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000107 STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000108 if (obj == nullptr) return handle(v8::internal::Smi::FromInt(0), isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 return isolate->factory()->NewForeign(
Steve Blocka7e24c12009-10-30 11:49:00 +0000110 reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(obj)));
111}
112
113
Steve Blockd0582a62009-12-15 09:54:21 +0000114class 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 Blocka7e24c12009-10-30 11:49:00 +0000123
124class RegisteredExtension {
125 public:
126 explicit RegisteredExtension(Extension* extension);
127 static void Register(RegisteredExtension* that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128 static void UnregisterAll();
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 Extension* extension() { return extension_; }
130 RegisteredExtension* next() { return next_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000131 static RegisteredExtension* first_extension() { return first_extension_; }
132 private:
133 Extension* extension_;
134 RegisteredExtension* next_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000135 static RegisteredExtension* first_extension_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000136};
137
138
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000139#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 Murdochb8a8cc12014-11-26 15:28:44 +0000178
Steve Blocka7e24c12009-10-30 11:49:00 +0000179class Utils {
180 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 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 Blocka7e24c12009-10-30 11:49:00 +0000187
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 Murdochb8a8cc12014-11-26 15:28:44 +0000195 static inline Local<Name> ToLocal(
196 v8::internal::Handle<v8::internal::Name> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000197 static inline Local<String> ToLocal(
198 v8::internal::Handle<v8::internal::String> obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000199 static inline Local<Symbol> ToLocal(
200 v8::internal::Handle<v8::internal::Symbol> obj);
Ben Murdochf87a2032010-10-22 12:50:53 +0100201 static inline Local<RegExp> ToLocal(
202 v8::internal::Handle<v8::internal::JSRegExp> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000203 static inline Local<Object> ToLocal(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000204 v8::internal::Handle<v8::internal::JSReceiver> obj);
205 static inline Local<Object> ToLocal(
Steve Blocka7e24c12009-10-30 11:49:00 +0000206 v8::internal::Handle<v8::internal::JSObject> obj);
207 static inline Local<Array> ToLocal(
208 v8::internal::Handle<v8::internal::JSArray> obj);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000209 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 Murdochb8a8cc12014-11-26 15:28:44 +0000215 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 Murdochb8a8cc12014-11-26 15:28:44 +0000221 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 Murdoch4a90d5f2016-03-22 12:00:34 +0000242 static inline Local<SharedArrayBuffer> ToLocalShared(
243 v8::internal::Handle<v8::internal::JSArrayBuffer> obj);
244
Steve Blocka7e24c12009-10-30 11:49:00 +0000245 static inline Local<Message> MessageToLocal(
246 v8::internal::Handle<v8::internal::Object> obj);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400247 static inline Local<Promise> PromiseToLocal(
248 v8::internal::Handle<v8::internal::JSObject> obj);
Kristian Monsen25f61362010-05-21 11:50:48 +0100249 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 Blocka7e24c12009-10-30 11:49:00 +0000253 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 Murdoch4a90d5f2016-03-22 12:00:34 +0000263 static inline Local<Signature> SignatureToLocal(
264 v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000265 static inline Local<AccessorSignature> AccessorSignatureToLocal(
266 v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000267 static inline Local<External> ExternalToLocal(
268 v8::internal::Handle<v8::internal::JSObject> obj);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000269 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 Blocka7e24c12009-10-30 11:49:00 +0000273
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000274#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
278OPEN_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) {
284 DCHECK(obj.is_null() || !obj->IsTheHole());
285 return Local<To>(reinterpret_cast<To*>(obj.location()));
286 }
287
288 template <class T>
289 static inline v8::internal::Handle<v8::internal::Object> OpenPersistent(
290 const v8::Persistent<T>& persistent) {
291 return v8::internal::Handle<v8::internal::Object>(
292 reinterpret_cast<v8::internal::Object**>(persistent.val_));
293 }
294
295 template <class T>
296 static inline v8::internal::Handle<v8::internal::Object> OpenPersistent(
297 v8::Persistent<T>* persistent) {
298 return OpenPersistent(*persistent);
299 }
300
301 template <class From, class To>
302 static inline v8::internal::Handle<To> OpenHandle(v8::Local<From> handle) {
303 return OpenHandle(*handle);
304 }
305
306 private:
307 static void ReportApiFailure(const char* location, const char* message);
Steve Blocka7e24c12009-10-30 11:49:00 +0000308};
309
310
311template <class T>
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100312inline T* ToApi(v8::internal::Handle<v8::internal::Object> obj) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000313 return reinterpret_cast<T*>(obj.location());
314}
315
Steve Blocka7e24c12009-10-30 11:49:00 +0000316template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317inline v8::Local<T> ToApiHandle(
318 v8::internal::Handle<v8::internal::Object> obj) {
319 return Utils::Convert<v8::internal::Object, T>(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000320}
321
322
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000323template <class T>
324inline bool ToLocal(v8::internal::MaybeHandle<v8::internal::Object> maybe,
325 Local<T>* local) {
326 v8::internal::Handle<v8::internal::Object> handle;
327 if (maybe.ToHandle(&handle)) {
328 *local = Utils::Convert<v8::internal::Object, T>(handle);
329 return true;
330 }
331 return false;
332}
333
334
Steve Blocka7e24c12009-10-30 11:49:00 +0000335// Implementations of ToLocal
336
337#define MAKE_TO_LOCAL(Name, From, To) \
338 Local<v8::To> Utils::Name(v8::internal::Handle<v8::internal::From> obj) { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000339 return Convert<v8::internal::From, v8::To>(obj); \
Steve Blocka7e24c12009-10-30 11:49:00 +0000340 }
341
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000342
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000343#define MAKE_TO_LOCAL_TYPED_ARRAY(Type, typeName, TYPE, ctype, size) \
344 Local<v8::Type##Array> Utils::ToLocal##Type##Array( \
345 v8::internal::Handle<v8::internal::JSTypedArray> obj) { \
346 DCHECK(obj->type() == v8::internal::kExternal##Type##Array); \
347 return Convert<v8::internal::JSTypedArray, v8::Type##Array>(obj); \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000348 }
349
350
Steve Blocka7e24c12009-10-30 11:49:00 +0000351MAKE_TO_LOCAL(ToLocal, Context, Context)
352MAKE_TO_LOCAL(ToLocal, Object, Value)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353MAKE_TO_LOCAL(ToLocal, Name, Name)
Steve Blocka7e24c12009-10-30 11:49:00 +0000354MAKE_TO_LOCAL(ToLocal, String, String)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000355MAKE_TO_LOCAL(ToLocal, Symbol, Symbol)
Ben Murdochf87a2032010-10-22 12:50:53 +0100356MAKE_TO_LOCAL(ToLocal, JSRegExp, RegExp)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000357MAKE_TO_LOCAL(ToLocal, JSReceiver, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +0000358MAKE_TO_LOCAL(ToLocal, JSObject, Object)
359MAKE_TO_LOCAL(ToLocal, JSArray, Array)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000360MAKE_TO_LOCAL(ToLocal, JSMap, Map)
361MAKE_TO_LOCAL(ToLocal, JSSet, Set)
362MAKE_TO_LOCAL(ToLocal, JSProxy, Proxy)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000363MAKE_TO_LOCAL(ToLocal, JSArrayBuffer, ArrayBuffer)
364MAKE_TO_LOCAL(ToLocal, JSArrayBufferView, ArrayBufferView)
365MAKE_TO_LOCAL(ToLocal, JSDataView, DataView)
366MAKE_TO_LOCAL(ToLocal, JSTypedArray, TypedArray)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000367MAKE_TO_LOCAL(ToLocalShared, JSArrayBuffer, SharedArrayBuffer)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000368
369TYPED_ARRAYS(MAKE_TO_LOCAL_TYPED_ARRAY)
370
Steve Blocka7e24c12009-10-30 11:49:00 +0000371MAKE_TO_LOCAL(ToLocal, FunctionTemplateInfo, FunctionTemplate)
372MAKE_TO_LOCAL(ToLocal, ObjectTemplateInfo, ObjectTemplate)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000373MAKE_TO_LOCAL(SignatureToLocal, FunctionTemplateInfo, Signature)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000374MAKE_TO_LOCAL(AccessorSignatureToLocal, FunctionTemplateInfo, AccessorSignature)
Steve Blocka7e24c12009-10-30 11:49:00 +0000375MAKE_TO_LOCAL(MessageToLocal, Object, Message)
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400376MAKE_TO_LOCAL(PromiseToLocal, JSObject, Promise)
Kristian Monsen25f61362010-05-21 11:50:48 +0100377MAKE_TO_LOCAL(StackTraceToLocal, JSArray, StackTrace)
378MAKE_TO_LOCAL(StackFrameToLocal, JSObject, StackFrame)
Steve Blocka7e24c12009-10-30 11:49:00 +0000379MAKE_TO_LOCAL(NumberToLocal, Object, Number)
380MAKE_TO_LOCAL(IntegerToLocal, Object, Integer)
381MAKE_TO_LOCAL(Uint32ToLocal, Object, Uint32)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000382MAKE_TO_LOCAL(ExternalToLocal, JSObject, External)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000383MAKE_TO_LOCAL(NativeWeakMapToLocal, JSWeakMap, NativeWeakMap)
384MAKE_TO_LOCAL(CallableToLocal, JSReceiver, Function)
Steve Blocka7e24c12009-10-30 11:49:00 +0000385
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000386#undef MAKE_TO_LOCAL_TYPED_ARRAY
Steve Blocka7e24c12009-10-30 11:49:00 +0000387#undef MAKE_TO_LOCAL
388
389
390// Implementations of OpenHandle
391
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000392#define MAKE_OPEN_HANDLE(From, To) \
393 v8::internal::Handle<v8::internal::To> Utils::OpenHandle( \
394 const v8::From* that, bool allow_empty_handle) { \
395 DCHECK(allow_empty_handle || that != NULL); \
396 DCHECK(that == NULL || \
397 (*reinterpret_cast<v8::internal::Object* const*>(that))->Is##To()); \
398 return v8::internal::Handle<v8::internal::To>( \
399 reinterpret_cast<v8::internal::To**>(const_cast<v8::From*>(that))); \
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 }
401
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000402OPEN_HANDLE_LIST(MAKE_OPEN_HANDLE)
Steve Blocka7e24c12009-10-30 11:49:00 +0000403
404#undef MAKE_OPEN_HANDLE
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000405#undef OPEN_HANDLE_LIST
Steve Blocka7e24c12009-10-30 11:49:00 +0000406
407
408namespace internal {
409
Steve Block44f0eee2011-05-26 01:26:41 +0100410
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000411class DeferredHandles {
412 public:
413 ~DeferredHandles();
414
415 private:
416 DeferredHandles(Object** first_block_limit, Isolate* isolate)
417 : next_(NULL),
418 previous_(NULL),
419 first_block_limit_(first_block_limit),
420 isolate_(isolate) {
421 isolate->LinkDeferredHandles(this);
422 }
423
424 void Iterate(ObjectVisitor* v);
425
426 List<Object**> blocks_;
427 DeferredHandles* next_;
428 DeferredHandles* previous_;
429 Object** first_block_limit_;
430 Isolate* isolate_;
431
432 friend class HandleScopeImplementer;
433 friend class Isolate;
434};
435
436
Steve Blocka7e24c12009-10-30 11:49:00 +0000437// This class is here in order to be able to declare it a friend of
438// HandleScope. Moving these methods to be members of HandleScope would be
Steve Block44f0eee2011-05-26 01:26:41 +0100439// neat in some ways, but it would expose internal implementation details in
Steve Blocka7e24c12009-10-30 11:49:00 +0000440// our public header file, which is undesirable.
441//
Steve Block44f0eee2011-05-26 01:26:41 +0100442// An isolate has a single instance of this class to hold the current thread's
443// data. In multithreaded V8 programs this data is copied in and out of storage
Steve Blocka7e24c12009-10-30 11:49:00 +0000444// so that the currently executing thread always has its own copy of this
445// data.
Ben Murdoch257744e2011-11-30 15:57:28 +0000446class HandleScopeImplementer {
Steve Blocka7e24c12009-10-30 11:49:00 +0000447 public:
Ben Murdoch257744e2011-11-30 15:57:28 +0000448 explicit HandleScopeImplementer(Isolate* isolate)
449 : isolate_(isolate),
450 blocks_(0),
Steve Blocka7e24c12009-10-30 11:49:00 +0000451 entered_contexts_(0),
452 saved_contexts_(0),
453 spare_(NULL),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000454 call_depth_(0),
Ben Murdochda12d292016-06-02 14:46:10 +0100455 microtasks_depth_(0),
456 microtasks_suppressions_(0),
457#ifdef DEBUG
458 debug_microtasks_depth_(0),
459#endif
460 microtasks_policy_(v8::MicrotasksPolicy::kAuto),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000461 last_handle_before_deferred_block_(NULL) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000462
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000463 ~HandleScopeImplementer() {
464 DeleteArray(spare_);
465 }
466
Steve Blocka7e24c12009-10-30 11:49:00 +0000467 // Threading support for handle data.
468 static int ArchiveSpacePerThread();
Steve Block44f0eee2011-05-26 01:26:41 +0100469 char* RestoreThread(char* from);
470 char* ArchiveThread(char* to);
471 void FreeThreadResources();
Steve Blocka7e24c12009-10-30 11:49:00 +0000472
473 // Garbage collection support.
Steve Block44f0eee2011-05-26 01:26:41 +0100474 void Iterate(v8::internal::ObjectVisitor* v);
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 static char* Iterate(v8::internal::ObjectVisitor* v, char* data);
476
477
478 inline internal::Object** GetSpareOrNewBlock();
John Reck59135872010-11-02 12:39:01 -0700479 inline void DeleteExtensions(internal::Object** prev_limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000480
Ben Murdochda12d292016-06-02 14:46:10 +0100481 // Call depth represents nested v8 api calls.
Steve Blocka7e24c12009-10-30 11:49:00 +0000482 inline void IncrementCallDepth() {call_depth_++;}
483 inline void DecrementCallDepth() {call_depth_--;}
484 inline bool CallDepthIsZero() { return call_depth_ == 0; }
485
Ben Murdochda12d292016-06-02 14:46:10 +0100486 // Microtasks scope depth represents nested scopes controlling microtasks
487 // invocation, which happens when depth reaches zero.
488 inline void IncrementMicrotasksScopeDepth() {microtasks_depth_++;}
489 inline void DecrementMicrotasksScopeDepth() {microtasks_depth_--;}
490 inline int GetMicrotasksScopeDepth() { return microtasks_depth_; }
491
492 // Possibly nested microtasks suppression scopes prevent microtasks
493 // from running.
494 inline void IncrementMicrotasksSuppressions() {microtasks_suppressions_++;}
495 inline void DecrementMicrotasksSuppressions() {microtasks_suppressions_--;}
496 inline bool HasMicrotasksSuppressions() { return !!microtasks_suppressions_; }
497
498#ifdef DEBUG
499 // In debug we check that calls not intended to invoke microtasks are
500 // still correctly wrapped with microtask scopes.
501 inline void IncrementDebugMicrotasksScopeDepth() {debug_microtasks_depth_++;}
502 inline void DecrementDebugMicrotasksScopeDepth() {debug_microtasks_depth_--;}
503 inline bool DebugMicrotasksScopeDepthIsZero() {
504 return debug_microtasks_depth_ == 0;
505 }
506#endif
507
508 inline void set_microtasks_policy(v8::MicrotasksPolicy policy);
509 inline v8::MicrotasksPolicy microtasks_policy() const;
510
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000511 inline void EnterContext(Handle<Context> context);
512 inline void LeaveContext();
513 inline bool LastEnteredContextWas(Handle<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000514
515 // Returns the last entered context or an empty handle if no
516 // contexts have been entered.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000517 inline Handle<Context> LastEnteredContext();
Steve Blocka7e24c12009-10-30 11:49:00 +0000518
519 inline void SaveContext(Context* context);
520 inline Context* RestoreContext();
521 inline bool HasSavedContexts();
522
523 inline List<internal::Object**>* blocks() { return &blocks_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000524 Isolate* isolate() const { return isolate_; }
525
526 void ReturnBlock(Object** block) {
527 DCHECK(block != NULL);
528 if (spare_ != NULL) DeleteArray(spare_);
529 spare_ = block;
530 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000531
532 private:
533 void ResetAfterArchive() {
534 blocks_.Initialize(0);
535 entered_contexts_.Initialize(0);
536 saved_contexts_.Initialize(0);
537 spare_ = NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000538 last_handle_before_deferred_block_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000539 call_depth_ = 0;
540 }
541
542 void Free() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000543 DCHECK(blocks_.length() == 0);
544 DCHECK(entered_contexts_.length() == 0);
545 DCHECK(saved_contexts_.length() == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000546 blocks_.Free();
547 entered_contexts_.Free();
548 saved_contexts_.Free();
549 if (spare_ != NULL) {
550 DeleteArray(spare_);
551 spare_ = NULL;
552 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000553 DCHECK(call_depth_ == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000554 }
555
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000556 void BeginDeferredScope();
557 DeferredHandles* Detach(Object** prev_limit);
558
Ben Murdoch257744e2011-11-30 15:57:28 +0000559 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000560 List<internal::Object**> blocks_;
561 // Used as a stack to keep track of entered contexts.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000562 List<Context*> entered_contexts_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000563 // Used as a stack to keep track of saved contexts.
564 List<Context*> saved_contexts_;
565 Object** spare_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000566 int call_depth_;
Ben Murdochda12d292016-06-02 14:46:10 +0100567 int microtasks_depth_;
568 int microtasks_suppressions_;
569#ifdef DEBUG
570 int debug_microtasks_depth_;
571#endif
572 v8::MicrotasksPolicy microtasks_policy_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000573 Object** last_handle_before_deferred_block_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000574 // This is only used for threading support.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000575 HandleScopeData handle_scope_data_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000576
577 void IterateThis(ObjectVisitor* v);
578 char* RestoreThreadHelper(char* from);
579 char* ArchiveThreadHelper(char* to);
580
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000581 friend class DeferredHandles;
582 friend class DeferredHandleScope;
583
Steve Blocka7e24c12009-10-30 11:49:00 +0000584 DISALLOW_COPY_AND_ASSIGN(HandleScopeImplementer);
585};
586
587
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100588const int kHandleBlockSize = v8::internal::KB - 2; // fit in one page
Steve Blocka7e24c12009-10-30 11:49:00 +0000589
590
Ben Murdochda12d292016-06-02 14:46:10 +0100591void HandleScopeImplementer::set_microtasks_policy(
592 v8::MicrotasksPolicy policy) {
593 microtasks_policy_ = policy;
594}
595
596
597v8::MicrotasksPolicy HandleScopeImplementer::microtasks_policy() const {
598 return microtasks_policy_;
599}
600
601
Steve Blocka7e24c12009-10-30 11:49:00 +0000602void HandleScopeImplementer::SaveContext(Context* context) {
603 saved_contexts_.Add(context);
604}
605
606
607Context* HandleScopeImplementer::RestoreContext() {
608 return saved_contexts_.RemoveLast();
609}
610
611
612bool HandleScopeImplementer::HasSavedContexts() {
613 return !saved_contexts_.is_empty();
614}
615
616
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000617void HandleScopeImplementer::EnterContext(Handle<Context> context) {
618 entered_contexts_.Add(*context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000619}
620
621
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000622void HandleScopeImplementer::LeaveContext() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000623 entered_contexts_.RemoveLast();
Steve Blocka7e24c12009-10-30 11:49:00 +0000624}
625
626
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000627bool HandleScopeImplementer::LastEnteredContextWas(Handle<Context> context) {
628 return !entered_contexts_.is_empty() && entered_contexts_.last() == *context;
629}
630
631
632Handle<Context> HandleScopeImplementer::LastEnteredContext() {
633 if (entered_contexts_.is_empty()) return Handle<Context>::null();
634 return Handle<Context>(entered_contexts_.last());
Steve Blocka7e24c12009-10-30 11:49:00 +0000635}
636
637
638// If there's a spare block, use it for growing the current scope.
639internal::Object** HandleScopeImplementer::GetSpareOrNewBlock() {
640 internal::Object** block = (spare_ != NULL) ?
641 spare_ :
642 NewArray<internal::Object*>(kHandleBlockSize);
643 spare_ = NULL;
644 return block;
645}
646
647
John Reck59135872010-11-02 12:39:01 -0700648void HandleScopeImplementer::DeleteExtensions(internal::Object** prev_limit) {
649 while (!blocks_.is_empty()) {
650 internal::Object** block_start = blocks_.last();
651 internal::Object** block_limit = block_start + kHandleBlockSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000652
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000653 // SealHandleScope may make the prev_limit to point inside the block.
654 if (block_start <= prev_limit && prev_limit <= block_limit) {
655#ifdef ENABLE_HANDLE_ZAPPING
656 internal::HandleScope::ZapRange(prev_limit, block_limit);
657#endif
658 break;
659 }
John Reck59135872010-11-02 12:39:01 -0700660
661 blocks_.RemoveLast();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000662#ifdef ENABLE_HANDLE_ZAPPING
663 internal::HandleScope::ZapRange(block_start, block_limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000664#endif
John Reck59135872010-11-02 12:39:01 -0700665 if (spare_ != NULL) {
666 DeleteArray(spare_);
667 }
668 spare_ = block_start;
669 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000670 DCHECK((blocks_.is_empty() && prev_limit == NULL) ||
John Reck59135872010-11-02 12:39:01 -0700671 (!blocks_.is_empty() && prev_limit != NULL));
Steve Blocka7e24c12009-10-30 11:49:00 +0000672}
673
Ben Murdochb0fe1622011-05-05 13:52:32 +0100674
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000675// Interceptor functions called from generated inline caches to notify
676// CPU profiler that external callbacks are invoked.
677void InvokeAccessorGetterCallback(
678 v8::Local<v8::Name> property,
679 const v8::PropertyCallbackInfo<v8::Value>& info,
680 v8::AccessorNameGetterCallback getter);
681
682void InvokeFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info,
683 v8::FunctionCallback callback);
684
Ben Murdochb0fe1622011-05-05 13:52:32 +0100685class Testing {
686 public:
687 static v8::Testing::StressType stress_type() { return stress_type_; }
688 static void set_stress_type(v8::Testing::StressType stress_type) {
689 stress_type_ = stress_type;
690 }
691
692 private:
693 static v8::Testing::StressType stress_type_;
694};
695
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000696} // namespace internal
697} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000698
699#endif // V8_API_H_