1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "include/v8-testing.h"
9 #include "src/contexts.h"
10 #include "src/factory.h"
11 #include "src/isolate.h"
13 #include "src/objects-inl.h"
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
24 FUNCTION_TEMPLATE = 0,
30 // Utilities for working with neander-objects, primitive
31 // env-independent JSObjects used by the api.
34 explicit NeanderObject(v8::internal::Isolate* isolate, int size);
35 explicit inline NeanderObject(v8::internal::Handle<v8::internal::Object> obj);
36 explicit inline NeanderObject(v8::internal::Object* obj);
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_; }
42 v8::internal::Handle<v8::internal::JSObject> value_;
46 // Utilities for working with neander-arrays, a simple extensible
47 // array abstraction built on neander-objects.
50 explicit NeanderArray(v8::internal::Isolate* isolate);
51 explicit inline NeanderArray(v8::internal::Handle<v8::internal::Object> obj);
52 inline v8::internal::Handle<v8::internal::JSObject> value() {
56 void add(internal::Isolate* isolate,
57 v8::internal::Handle<v8::internal::Object> value);
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);
70 NeanderObject::NeanderObject(v8::internal::Handle<v8::internal::Object> obj)
71 : value_(v8::internal::Handle<v8::internal::JSObject>::cast(obj)) { }
74 NeanderObject::NeanderObject(v8::internal::Object* obj)
75 : value_(v8::internal::Handle<v8::internal::JSObject>(
76 v8::internal::JSObject::cast(obj))) { }
79 NeanderArray::NeanderArray(v8::internal::Handle<v8::internal::Object> obj)
83 v8::internal::Object* NeanderObject::get(int offset) {
84 DCHECK(value()->HasFastObjectElements());
85 return v8::internal::FixedArray::cast(value()->elements())->get(offset);
89 void NeanderObject::set(int offset, v8::internal::Object* value) {
90 DCHECK(value_->HasFastObjectElements());
91 v8::internal::FixedArray::cast(value_->elements())->set(offset, value);
95 template <typename T> inline T ToCData(v8::internal::Object* obj) {
96 STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
97 if (obj == v8::internal::Smi::FromInt(0)) return nullptr;
98 return reinterpret_cast<T>(
99 reinterpret_cast<intptr_t>(
100 v8::internal::Foreign::cast(obj)->foreign_address()));
104 template <typename T>
105 inline v8::internal::Handle<v8::internal::Object> FromCData(
106 v8::internal::Isolate* isolate, T obj) {
107 STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
108 if (obj == nullptr) return handle(v8::internal::Smi::FromInt(0), isolate);
109 return isolate->factory()->NewForeign(
110 reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(obj)));
116 explicit ApiFunction(v8::internal::Address addr) : addr_(addr) { }
117 v8::internal::Address address() { return addr_; }
119 v8::internal::Address addr_;
124 class RegisteredExtension {
126 explicit RegisteredExtension(Extension* extension);
127 static void Register(RegisteredExtension* that);
128 static void UnregisterAll();
129 Extension* extension() { return extension_; }
130 RegisteredExtension* next() { return next_; }
131 static RegisteredExtension* first_extension() { return first_extension_; }
133 Extension* extension_;
134 RegisteredExtension* next_;
135 static RegisteredExtension* first_extension_;
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(TypeSwitch, TypeSwitchInfo) \
147 V(RegExp, JSRegExp) \
148 V(Object, JSObject) \
152 V(ArrayBuffer, JSArrayBuffer) \
153 V(ArrayBufferView, JSArrayBufferView) \
154 V(TypedArray, JSTypedArray) \
155 V(Uint8Array, JSTypedArray) \
156 V(Uint8ClampedArray, JSTypedArray) \
157 V(Int8Array, JSTypedArray) \
158 V(Uint16Array, JSTypedArray) \
159 V(Int16Array, JSTypedArray) \
160 V(Uint32Array, JSTypedArray) \
161 V(Int32Array, JSTypedArray) \
162 V(Float32Array, JSTypedArray) \
163 V(Float64Array, JSTypedArray) \
164 V(DataView, JSDataView) \
165 V(SharedArrayBuffer, JSArrayBuffer) \
169 V(Script, JSFunction) \
170 V(UnboundScript, SharedFunctionInfo) \
171 V(Function, JSFunction) \
172 V(Message, JSMessageObject) \
173 V(Context, Context) \
174 V(External, Object) \
175 V(StackTrace, JSArray) \
176 V(StackFrame, JSObject) \
177 V(NativeWeakMap, JSWeakMap)
181 static inline bool ApiCheck(bool condition,
182 const char* location,
183 const char* message) {
184 if (!condition) Utils::ReportApiFailure(location, message);
188 static Local<FunctionTemplate> ToFunctionTemplate(NeanderObject obj);
189 static Local<ObjectTemplate> ToObjectTemplate(NeanderObject obj);
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);
195 static inline Local<Function> ToLocal(
196 v8::internal::Handle<v8::internal::JSFunction> obj);
197 static inline Local<Name> ToLocal(
198 v8::internal::Handle<v8::internal::Name> obj);
199 static inline Local<String> ToLocal(
200 v8::internal::Handle<v8::internal::String> obj);
201 static inline Local<Symbol> ToLocal(
202 v8::internal::Handle<v8::internal::Symbol> obj);
203 static inline Local<RegExp> ToLocal(
204 v8::internal::Handle<v8::internal::JSRegExp> obj);
205 static inline Local<Object> ToLocal(
206 v8::internal::Handle<v8::internal::JSObject> obj);
207 static inline Local<Array> ToLocal(
208 v8::internal::Handle<v8::internal::JSArray> obj);
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<ArrayBuffer> ToLocal(
214 v8::internal::Handle<v8::internal::JSArrayBuffer> obj);
215 static inline Local<ArrayBufferView> ToLocal(
216 v8::internal::Handle<v8::internal::JSArrayBufferView> obj);
217 static inline Local<DataView> ToLocal(
218 v8::internal::Handle<v8::internal::JSDataView> obj);
220 static inline Local<TypedArray> ToLocal(
221 v8::internal::Handle<v8::internal::JSTypedArray> obj);
222 static inline Local<Uint8Array> ToLocalUint8Array(
223 v8::internal::Handle<v8::internal::JSTypedArray> obj);
224 static inline Local<Uint8ClampedArray> ToLocalUint8ClampedArray(
225 v8::internal::Handle<v8::internal::JSTypedArray> obj);
226 static inline Local<Int8Array> ToLocalInt8Array(
227 v8::internal::Handle<v8::internal::JSTypedArray> obj);
228 static inline Local<Uint16Array> ToLocalUint16Array(
229 v8::internal::Handle<v8::internal::JSTypedArray> obj);
230 static inline Local<Int16Array> ToLocalInt16Array(
231 v8::internal::Handle<v8::internal::JSTypedArray> obj);
232 static inline Local<Uint32Array> ToLocalUint32Array(
233 v8::internal::Handle<v8::internal::JSTypedArray> obj);
234 static inline Local<Int32Array> ToLocalInt32Array(
235 v8::internal::Handle<v8::internal::JSTypedArray> obj);
236 static inline Local<Float32Array> ToLocalFloat32Array(
237 v8::internal::Handle<v8::internal::JSTypedArray> obj);
238 static inline Local<Float64Array> ToLocalFloat64Array(
239 v8::internal::Handle<v8::internal::JSTypedArray> obj);
241 static inline Local<SharedArrayBuffer> ToLocalShared(
242 v8::internal::Handle<v8::internal::JSArrayBuffer> obj);
244 static inline Local<Message> MessageToLocal(
245 v8::internal::Handle<v8::internal::Object> obj);
246 static inline Local<Promise> PromiseToLocal(
247 v8::internal::Handle<v8::internal::JSObject> obj);
248 static inline Local<StackTrace> StackTraceToLocal(
249 v8::internal::Handle<v8::internal::JSArray> obj);
250 static inline Local<StackFrame> StackFrameToLocal(
251 v8::internal::Handle<v8::internal::JSObject> obj);
252 static inline Local<Number> NumberToLocal(
253 v8::internal::Handle<v8::internal::Object> obj);
254 static inline Local<Integer> IntegerToLocal(
255 v8::internal::Handle<v8::internal::Object> obj);
256 static inline Local<Uint32> Uint32ToLocal(
257 v8::internal::Handle<v8::internal::Object> obj);
258 static inline Local<FunctionTemplate> ToLocal(
259 v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj);
260 static inline Local<ObjectTemplate> ToLocal(
261 v8::internal::Handle<v8::internal::ObjectTemplateInfo> obj);
262 static inline Local<Signature> SignatureToLocal(
263 v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj);
264 static inline Local<AccessorSignature> AccessorSignatureToLocal(
265 v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj);
266 static inline Local<TypeSwitch> ToLocal(
267 v8::internal::Handle<v8::internal::TypeSwitchInfo> obj);
268 static inline Local<External> ExternalToLocal(
269 v8::internal::Handle<v8::internal::JSObject> obj);
270 static inline Local<NativeWeakMap> NativeWeakMapToLocal(
271 v8::internal::Handle<v8::internal::JSWeakMap> obj);
273 #define DECLARE_OPEN_HANDLE(From, To) \
274 static inline v8::internal::Handle<v8::internal::To> \
275 OpenHandle(const From* that, bool allow_empty_handle = false);
277 OPEN_HANDLE_LIST(DECLARE_OPEN_HANDLE)
279 #undef DECLARE_OPEN_HANDLE
281 template<class From, class To>
282 static inline Local<To> Convert(v8::internal::Handle<From> obj) {
283 DCHECK(obj.is_null() || !obj->IsTheHole());
284 return Local<To>(reinterpret_cast<To*>(obj.location()));
288 static inline v8::internal::Handle<v8::internal::Object> OpenPersistent(
289 const v8::Persistent<T>& persistent) {
290 return v8::internal::Handle<v8::internal::Object>(
291 reinterpret_cast<v8::internal::Object**>(persistent.val_));
295 static inline v8::internal::Handle<v8::internal::Object> OpenPersistent(
296 v8::Persistent<T>* persistent) {
297 return OpenPersistent(*persistent);
300 template <class From, class To>
301 static inline v8::internal::Handle<To> OpenHandle(v8::Local<From> handle) {
302 return OpenHandle(*handle);
306 static void ReportApiFailure(const char* location, const char* message);
311 inline T* ToApi(v8::internal::Handle<v8::internal::Object> obj) {
312 return reinterpret_cast<T*>(obj.location());
316 inline v8::Local<T> ToApiHandle(
317 v8::internal::Handle<v8::internal::Object> obj) {
318 return Utils::Convert<v8::internal::Object, T>(obj);
323 inline bool ToLocal(v8::internal::MaybeHandle<v8::internal::Object> maybe,
325 v8::internal::Handle<v8::internal::Object> handle;
326 if (maybe.ToHandle(&handle)) {
327 *local = Utils::Convert<v8::internal::Object, T>(handle);
334 // Implementations of ToLocal
336 #define MAKE_TO_LOCAL(Name, From, To) \
337 Local<v8::To> Utils::Name(v8::internal::Handle<v8::internal::From> obj) { \
338 return Convert<v8::internal::From, v8::To>(obj); \
342 #define MAKE_TO_LOCAL_TYPED_ARRAY(Type, typeName, TYPE, ctype, size) \
343 Local<v8::Type##Array> Utils::ToLocal##Type##Array( \
344 v8::internal::Handle<v8::internal::JSTypedArray> obj) { \
345 DCHECK(obj->type() == v8::internal::kExternal##Type##Array); \
346 return Convert<v8::internal::JSTypedArray, v8::Type##Array>(obj); \
350 MAKE_TO_LOCAL(ToLocal, Context, Context)
351 MAKE_TO_LOCAL(ToLocal, Object, Value)
352 MAKE_TO_LOCAL(ToLocal, JSFunction, Function)
353 MAKE_TO_LOCAL(ToLocal, Name, Name)
354 MAKE_TO_LOCAL(ToLocal, String, String)
355 MAKE_TO_LOCAL(ToLocal, Symbol, Symbol)
356 MAKE_TO_LOCAL(ToLocal, JSRegExp, RegExp)
357 MAKE_TO_LOCAL(ToLocal, JSObject, Object)
358 MAKE_TO_LOCAL(ToLocal, JSArray, Array)
359 MAKE_TO_LOCAL(ToLocal, JSMap, Map)
360 MAKE_TO_LOCAL(ToLocal, JSSet, Set)
361 MAKE_TO_LOCAL(ToLocal, JSArrayBuffer, ArrayBuffer)
362 MAKE_TO_LOCAL(ToLocal, JSArrayBufferView, ArrayBufferView)
363 MAKE_TO_LOCAL(ToLocal, JSDataView, DataView)
364 MAKE_TO_LOCAL(ToLocal, JSTypedArray, TypedArray)
365 MAKE_TO_LOCAL(ToLocalShared, JSArrayBuffer, SharedArrayBuffer)
367 TYPED_ARRAYS(MAKE_TO_LOCAL_TYPED_ARRAY)
369 MAKE_TO_LOCAL(ToLocal, FunctionTemplateInfo, FunctionTemplate)
370 MAKE_TO_LOCAL(ToLocal, ObjectTemplateInfo, ObjectTemplate)
371 MAKE_TO_LOCAL(SignatureToLocal, FunctionTemplateInfo, Signature)
372 MAKE_TO_LOCAL(AccessorSignatureToLocal, FunctionTemplateInfo, AccessorSignature)
373 MAKE_TO_LOCAL(ToLocal, TypeSwitchInfo, TypeSwitch)
374 MAKE_TO_LOCAL(MessageToLocal, Object, Message)
375 MAKE_TO_LOCAL(PromiseToLocal, JSObject, Promise)
376 MAKE_TO_LOCAL(StackTraceToLocal, JSArray, StackTrace)
377 MAKE_TO_LOCAL(StackFrameToLocal, JSObject, StackFrame)
378 MAKE_TO_LOCAL(NumberToLocal, Object, Number)
379 MAKE_TO_LOCAL(IntegerToLocal, Object, Integer)
380 MAKE_TO_LOCAL(Uint32ToLocal, Object, Uint32)
381 MAKE_TO_LOCAL(ExternalToLocal, JSObject, External)
382 MAKE_TO_LOCAL(NativeWeakMapToLocal, JSWeakMap, NativeWeakMap)
384 #undef MAKE_TO_LOCAL_TYPED_ARRAY
388 // Implementations of OpenHandle
390 #define MAKE_OPEN_HANDLE(From, To) \
391 v8::internal::Handle<v8::internal::To> Utils::OpenHandle( \
392 const v8::From* that, bool allow_empty_handle) { \
393 DCHECK(allow_empty_handle || that != NULL); \
394 DCHECK(that == NULL || \
395 (*reinterpret_cast<v8::internal::Object* const*>(that))->Is##To()); \
396 return v8::internal::Handle<v8::internal::To>( \
397 reinterpret_cast<v8::internal::To**>(const_cast<v8::From*>(that))); \
400 OPEN_HANDLE_LIST(MAKE_OPEN_HANDLE)
402 #undef MAKE_OPEN_HANDLE
403 #undef OPEN_HANDLE_LIST
409 class DeferredHandles {
414 DeferredHandles(Object** first_block_limit, Isolate* isolate)
417 first_block_limit_(first_block_limit),
419 isolate->LinkDeferredHandles(this);
422 void Iterate(ObjectVisitor* v);
424 List<Object**> blocks_;
425 DeferredHandles* next_;
426 DeferredHandles* previous_;
427 Object** first_block_limit_;
430 friend class HandleScopeImplementer;
431 friend class Isolate;
435 // This class is here in order to be able to declare it a friend of
436 // HandleScope. Moving these methods to be members of HandleScope would be
437 // neat in some ways, but it would expose internal implementation details in
438 // our public header file, which is undesirable.
440 // An isolate has a single instance of this class to hold the current thread's
441 // data. In multithreaded V8 programs this data is copied in and out of storage
442 // so that the currently executing thread always has its own copy of this
444 class HandleScopeImplementer {
446 explicit HandleScopeImplementer(Isolate* isolate)
449 entered_contexts_(0),
453 last_handle_before_deferred_block_(NULL) { }
455 ~HandleScopeImplementer() {
459 // Threading support for handle data.
460 static int ArchiveSpacePerThread();
461 char* RestoreThread(char* from);
462 char* ArchiveThread(char* to);
463 void FreeThreadResources();
465 // Garbage collection support.
466 void Iterate(v8::internal::ObjectVisitor* v);
467 static char* Iterate(v8::internal::ObjectVisitor* v, char* data);
470 inline internal::Object** GetSpareOrNewBlock();
471 inline void DeleteExtensions(internal::Object** prev_limit);
473 inline void IncrementCallDepth() {call_depth_++;}
474 inline void DecrementCallDepth() {call_depth_--;}
475 inline bool CallDepthIsZero() { return call_depth_ == 0; }
477 inline void EnterContext(Handle<Context> context);
478 inline void LeaveContext();
479 inline bool LastEnteredContextWas(Handle<Context> context);
481 // Returns the last entered context or an empty handle if no
482 // contexts have been entered.
483 inline Handle<Context> LastEnteredContext();
485 inline void SaveContext(Context* context);
486 inline Context* RestoreContext();
487 inline bool HasSavedContexts();
489 inline List<internal::Object**>* blocks() { return &blocks_; }
490 Isolate* isolate() const { return isolate_; }
492 void ReturnBlock(Object** block) {
493 DCHECK(block != NULL);
494 if (spare_ != NULL) DeleteArray(spare_);
499 void ResetAfterArchive() {
500 blocks_.Initialize(0);
501 entered_contexts_.Initialize(0);
502 saved_contexts_.Initialize(0);
504 last_handle_before_deferred_block_ = NULL;
509 DCHECK(blocks_.length() == 0);
510 DCHECK(entered_contexts_.length() == 0);
511 DCHECK(saved_contexts_.length() == 0);
513 entered_contexts_.Free();
514 saved_contexts_.Free();
515 if (spare_ != NULL) {
519 DCHECK(call_depth_ == 0);
522 void BeginDeferredScope();
523 DeferredHandles* Detach(Object** prev_limit);
526 List<internal::Object**> blocks_;
527 // Used as a stack to keep track of entered contexts.
528 List<Context*> entered_contexts_;
529 // Used as a stack to keep track of saved contexts.
530 List<Context*> saved_contexts_;
533 Object** last_handle_before_deferred_block_;
534 // This is only used for threading support.
535 HandleScopeData handle_scope_data_;
537 void IterateThis(ObjectVisitor* v);
538 char* RestoreThreadHelper(char* from);
539 char* ArchiveThreadHelper(char* to);
541 friend class DeferredHandles;
542 friend class DeferredHandleScope;
544 DISALLOW_COPY_AND_ASSIGN(HandleScopeImplementer);
548 const int kHandleBlockSize = v8::internal::KB - 2; // fit in one page
551 void HandleScopeImplementer::SaveContext(Context* context) {
552 saved_contexts_.Add(context);
556 Context* HandleScopeImplementer::RestoreContext() {
557 return saved_contexts_.RemoveLast();
561 bool HandleScopeImplementer::HasSavedContexts() {
562 return !saved_contexts_.is_empty();
566 void HandleScopeImplementer::EnterContext(Handle<Context> context) {
567 entered_contexts_.Add(*context);
571 void HandleScopeImplementer::LeaveContext() {
572 entered_contexts_.RemoveLast();
576 bool HandleScopeImplementer::LastEnteredContextWas(Handle<Context> context) {
577 return !entered_contexts_.is_empty() && entered_contexts_.last() == *context;
581 Handle<Context> HandleScopeImplementer::LastEnteredContext() {
582 if (entered_contexts_.is_empty()) return Handle<Context>::null();
583 return Handle<Context>(entered_contexts_.last());
587 // If there's a spare block, use it for growing the current scope.
588 internal::Object** HandleScopeImplementer::GetSpareOrNewBlock() {
589 internal::Object** block = (spare_ != NULL) ?
591 NewArray<internal::Object*>(kHandleBlockSize);
597 void HandleScopeImplementer::DeleteExtensions(internal::Object** prev_limit) {
598 while (!blocks_.is_empty()) {
599 internal::Object** block_start = blocks_.last();
600 internal::Object** block_limit = block_start + kHandleBlockSize;
602 // SealHandleScope may make the prev_limit to point inside the block.
603 if (block_start <= prev_limit && prev_limit <= block_limit) {
604 #ifdef ENABLE_HANDLE_ZAPPING
605 internal::HandleScope::ZapRange(prev_limit, block_limit);
610 blocks_.RemoveLast();
611 #ifdef ENABLE_HANDLE_ZAPPING
612 internal::HandleScope::ZapRange(block_start, block_limit);
614 if (spare_ != NULL) {
617 spare_ = block_start;
619 DCHECK((blocks_.is_empty() && prev_limit == NULL) ||
620 (!blocks_.is_empty() && prev_limit != NULL));
624 // Interceptor functions called from generated inline caches to notify
625 // CPU profiler that external callbacks are invoked.
626 void InvokeAccessorGetterCallback(
627 v8::Local<v8::Name> property,
628 const v8::PropertyCallbackInfo<v8::Value>& info,
629 v8::AccessorNameGetterCallback getter);
631 void InvokeFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info,
632 v8::FunctionCallback callback);
636 static v8::Testing::StressType stress_type() { return stress_type_; }
637 static void set_stress_type(v8::Testing::StressType stress_type) {
638 stress_type_ = stress_type;
642 static v8::Testing::StressType stress_type_;
645 } } // namespace v8::internal