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.
10 #include "include/v8-testing.h"
11 #include "src/contexts.h"
12 #include "src/factory.h"
13 #include "src/isolate.h"
14 #include "src/list-inl.h"
18 // Constants used in the implementation of the API. The most natural thing
19 // would usually be to place these with the classes that use them, but
20 // we want to keep them out of v8.h because it is an externally
25 FUNCTION_TEMPLATE = 0,
31 // Utilities for working with neander-objects, primitive
32 // env-independent JSObjects used by the api.
35 explicit NeanderObject(v8::internal::Isolate* isolate, int size);
36 explicit inline NeanderObject(v8::internal::Handle<v8::internal::Object> obj);
37 explicit inline NeanderObject(v8::internal::Object* obj);
38 inline v8::internal::Object* get(int index);
39 inline void set(int index, v8::internal::Object* value);
40 inline v8::internal::Handle<v8::internal::JSObject> value() { return value_; }
43 v8::internal::Handle<v8::internal::JSObject> value_;
47 // Utilities for working with neander-arrays, a simple extensible
48 // array abstraction built on neander-objects.
51 explicit NeanderArray(v8::internal::Isolate* isolate);
52 explicit inline NeanderArray(v8::internal::Handle<v8::internal::Object> obj);
53 inline v8::internal::Handle<v8::internal::JSObject> value() {
57 void add(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 return reinterpret_cast<T>(
98 reinterpret_cast<intptr_t>(
99 v8::internal::Foreign::cast(obj)->foreign_address()));
103 template <typename T>
104 inline v8::internal::Handle<v8::internal::Object> FromCData(
105 v8::internal::Isolate* isolate, T obj) {
106 STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
107 return isolate->factory()->NewForeign(
108 reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(obj)));
114 explicit ApiFunction(v8::internal::Address addr) : addr_(addr) { }
115 v8::internal::Address address() { return addr_; }
117 v8::internal::Address addr_;
122 class RegisteredExtension {
124 explicit RegisteredExtension(Extension* extension);
125 static void Register(RegisteredExtension* that);
126 static void UnregisterAll();
127 Extension* extension() { return extension_; }
128 RegisteredExtension* next() { return next_; }
129 static RegisteredExtension* first_extension() { return first_extension_; }
131 Extension* extension_;
132 RegisteredExtension* next_;
133 static RegisteredExtension* first_extension_;
137 #define OPEN_HANDLE_LIST(V) \
138 V(Template, TemplateInfo) \
139 V(FunctionTemplate, FunctionTemplateInfo) \
140 V(ObjectTemplate, ObjectTemplateInfo) \
141 V(Signature, SignatureInfo) \
142 V(AccessorSignature, FunctionTemplateInfo) \
143 V(TypeSwitch, TypeSwitchInfo) \
145 V(RegExp, JSRegExp) \
146 V(Object, JSObject) \
148 V(ArrayBuffer, JSArrayBuffer) \
149 V(ArrayBufferView, JSArrayBufferView) \
150 V(TypedArray, JSTypedArray) \
151 V(Uint8Array, JSTypedArray) \
152 V(Uint8ClampedArray, JSTypedArray) \
153 V(Int8Array, JSTypedArray) \
154 V(Uint16Array, JSTypedArray) \
155 V(Int16Array, JSTypedArray) \
156 V(Uint32Array, JSTypedArray) \
157 V(Int32Array, JSTypedArray) \
158 V(Float32Array, JSTypedArray) \
159 V(Float64Array, JSTypedArray) \
160 V(DataView, JSDataView) \
163 V(Script, JSFunction) \
164 V(UnboundScript, SharedFunctionInfo) \
165 V(Function, JSFunction) \
166 V(Message, JSMessageObject) \
167 V(Context, Context) \
168 V(External, Object) \
169 V(StackTrace, JSArray) \
170 V(StackFrame, JSObject) \
171 V(DeclaredAccessorDescriptor, DeclaredAccessorDescriptor)
176 static inline bool ApiCheck(bool condition,
177 const char* location,
178 const char* message) {
179 if (!condition) Utils::ReportApiFailure(location, message);
183 static Local<FunctionTemplate> ToFunctionTemplate(NeanderObject obj);
184 static Local<ObjectTemplate> ToObjectTemplate(NeanderObject obj);
186 static inline Local<Context> ToLocal(
187 v8::internal::Handle<v8::internal::Context> obj);
188 static inline Local<Value> ToLocal(
189 v8::internal::Handle<v8::internal::Object> obj);
190 static inline Local<Function> ToLocal(
191 v8::internal::Handle<v8::internal::JSFunction> obj);
192 static inline Local<String> ToLocal(
193 v8::internal::Handle<v8::internal::String> obj);
194 static inline Local<Symbol> ToLocal(
195 v8::internal::Handle<v8::internal::Symbol> obj);
196 static inline Local<RegExp> ToLocal(
197 v8::internal::Handle<v8::internal::JSRegExp> obj);
198 static inline Local<Object> ToLocal(
199 v8::internal::Handle<v8::internal::JSObject> obj);
200 static inline Local<Array> ToLocal(
201 v8::internal::Handle<v8::internal::JSArray> obj);
202 static inline Local<ArrayBuffer> ToLocal(
203 v8::internal::Handle<v8::internal::JSArrayBuffer> obj);
204 static inline Local<ArrayBufferView> ToLocal(
205 v8::internal::Handle<v8::internal::JSArrayBufferView> obj);
206 static inline Local<DataView> ToLocal(
207 v8::internal::Handle<v8::internal::JSDataView> obj);
209 static inline Local<TypedArray> ToLocal(
210 v8::internal::Handle<v8::internal::JSTypedArray> obj);
211 static inline Local<Uint8Array> ToLocalUint8Array(
212 v8::internal::Handle<v8::internal::JSTypedArray> obj);
213 static inline Local<Uint8ClampedArray> ToLocalUint8ClampedArray(
214 v8::internal::Handle<v8::internal::JSTypedArray> obj);
215 static inline Local<Int8Array> ToLocalInt8Array(
216 v8::internal::Handle<v8::internal::JSTypedArray> obj);
217 static inline Local<Uint16Array> ToLocalUint16Array(
218 v8::internal::Handle<v8::internal::JSTypedArray> obj);
219 static inline Local<Int16Array> ToLocalInt16Array(
220 v8::internal::Handle<v8::internal::JSTypedArray> obj);
221 static inline Local<Uint32Array> ToLocalUint32Array(
222 v8::internal::Handle<v8::internal::JSTypedArray> obj);
223 static inline Local<Int32Array> ToLocalInt32Array(
224 v8::internal::Handle<v8::internal::JSTypedArray> obj);
225 static inline Local<Float32Array> ToLocalFloat32Array(
226 v8::internal::Handle<v8::internal::JSTypedArray> obj);
227 static inline Local<Float64Array> ToLocalFloat64Array(
228 v8::internal::Handle<v8::internal::JSTypedArray> obj);
230 static inline Local<Message> MessageToLocal(
231 v8::internal::Handle<v8::internal::Object> obj);
232 static inline Local<StackTrace> StackTraceToLocal(
233 v8::internal::Handle<v8::internal::JSArray> obj);
234 static inline Local<StackFrame> StackFrameToLocal(
235 v8::internal::Handle<v8::internal::JSObject> obj);
236 static inline Local<Number> NumberToLocal(
237 v8::internal::Handle<v8::internal::Object> obj);
238 static inline Local<Integer> IntegerToLocal(
239 v8::internal::Handle<v8::internal::Object> obj);
240 static inline Local<Uint32> Uint32ToLocal(
241 v8::internal::Handle<v8::internal::Object> obj);
242 static inline Local<FunctionTemplate> ToLocal(
243 v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj);
244 static inline Local<ObjectTemplate> ToLocal(
245 v8::internal::Handle<v8::internal::ObjectTemplateInfo> obj);
246 static inline Local<Signature> ToLocal(
247 v8::internal::Handle<v8::internal::SignatureInfo> obj);
248 static inline Local<AccessorSignature> AccessorSignatureToLocal(
249 v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj);
250 static inline Local<TypeSwitch> ToLocal(
251 v8::internal::Handle<v8::internal::TypeSwitchInfo> obj);
252 static inline Local<External> ExternalToLocal(
253 v8::internal::Handle<v8::internal::JSObject> obj);
254 static inline Local<DeclaredAccessorDescriptor> ToLocal(
255 v8::internal::Handle<v8::internal::DeclaredAccessorDescriptor> obj);
257 #define DECLARE_OPEN_HANDLE(From, To) \
258 static inline v8::internal::Handle<v8::internal::To> \
259 OpenHandle(const From* that, bool allow_empty_handle = false);
261 OPEN_HANDLE_LIST(DECLARE_OPEN_HANDLE)
263 #undef DECLARE_OPEN_HANDLE
265 template<class From, class To>
266 static inline Local<To> Convert(v8::internal::Handle<From> obj) {
267 DCHECK(obj.is_null() || !obj->IsTheHole());
268 return Local<To>(reinterpret_cast<To*>(obj.location()));
272 static inline v8::internal::Handle<v8::internal::Object> OpenPersistent(
273 const v8::Persistent<T>& persistent) {
274 return v8::internal::Handle<v8::internal::Object>(
275 reinterpret_cast<v8::internal::Object**>(persistent.val_));
279 static inline v8::internal::Handle<v8::internal::Object> OpenPersistent(
280 v8::Persistent<T>* persistent) {
281 return OpenPersistent(*persistent);
284 template <class From, class To>
285 static inline v8::internal::Handle<To> OpenHandle(v8::Local<From> handle) {
286 return OpenHandle(*handle);
290 static void ReportApiFailure(const char* location, const char* message);
295 v8::internal::Handle<T> v8::internal::Handle<T>::EscapeFrom(
296 v8::EscapableHandleScope* scope) {
297 v8::internal::Handle<T> handle;
301 return Utils::OpenHandle(*scope->Escape(Utils::ToLocal(handle)), true);
306 inline T* ToApi(v8::internal::Handle<v8::internal::Object> obj) {
307 return reinterpret_cast<T*>(obj.location());
311 inline v8::Local<T> ToApiHandle(
312 v8::internal::Handle<v8::internal::Object> obj) {
313 return Utils::Convert<v8::internal::Object, T>(obj);
317 // Implementations of ToLocal
319 #define MAKE_TO_LOCAL(Name, From, To) \
320 Local<v8::To> Utils::Name(v8::internal::Handle<v8::internal::From> obj) { \
321 return Convert<v8::internal::From, v8::To>(obj); \
325 #define MAKE_TO_LOCAL_TYPED_ARRAY(Type, typeName, TYPE, ctype, size) \
326 Local<v8::Type##Array> Utils::ToLocal##Type##Array( \
327 v8::internal::Handle<v8::internal::JSTypedArray> obj) { \
328 DCHECK(obj->type() == kExternal##Type##Array); \
329 return Convert<v8::internal::JSTypedArray, v8::Type##Array>(obj); \
333 MAKE_TO_LOCAL(ToLocal, Context, Context)
334 MAKE_TO_LOCAL(ToLocal, Object, Value)
335 MAKE_TO_LOCAL(ToLocal, JSFunction, Function)
336 MAKE_TO_LOCAL(ToLocal, String, String)
337 MAKE_TO_LOCAL(ToLocal, Symbol, Symbol)
338 MAKE_TO_LOCAL(ToLocal, JSRegExp, RegExp)
339 MAKE_TO_LOCAL(ToLocal, JSObject, Object)
340 MAKE_TO_LOCAL(ToLocal, JSArray, Array)
341 MAKE_TO_LOCAL(ToLocal, JSArrayBuffer, ArrayBuffer)
342 MAKE_TO_LOCAL(ToLocal, JSArrayBufferView, ArrayBufferView)
343 MAKE_TO_LOCAL(ToLocal, JSDataView, DataView)
344 MAKE_TO_LOCAL(ToLocal, JSTypedArray, TypedArray)
346 TYPED_ARRAYS(MAKE_TO_LOCAL_TYPED_ARRAY)
348 MAKE_TO_LOCAL(ToLocal, FunctionTemplateInfo, FunctionTemplate)
349 MAKE_TO_LOCAL(ToLocal, ObjectTemplateInfo, ObjectTemplate)
350 MAKE_TO_LOCAL(ToLocal, SignatureInfo, Signature)
351 MAKE_TO_LOCAL(AccessorSignatureToLocal, FunctionTemplateInfo, AccessorSignature)
352 MAKE_TO_LOCAL(ToLocal, TypeSwitchInfo, TypeSwitch)
353 MAKE_TO_LOCAL(MessageToLocal, Object, Message)
354 MAKE_TO_LOCAL(StackTraceToLocal, JSArray, StackTrace)
355 MAKE_TO_LOCAL(StackFrameToLocal, JSObject, StackFrame)
356 MAKE_TO_LOCAL(NumberToLocal, Object, Number)
357 MAKE_TO_LOCAL(IntegerToLocal, Object, Integer)
358 MAKE_TO_LOCAL(Uint32ToLocal, Object, Uint32)
359 MAKE_TO_LOCAL(ExternalToLocal, JSObject, External)
360 MAKE_TO_LOCAL(ToLocal, DeclaredAccessorDescriptor, DeclaredAccessorDescriptor)
362 #undef MAKE_TO_LOCAL_TYPED_ARRAY
366 // Implementations of OpenHandle
368 #define MAKE_OPEN_HANDLE(From, To) \
369 v8::internal::Handle<v8::internal::To> Utils::OpenHandle( \
370 const v8::From* that, bool allow_empty_handle) { \
371 EXTRA_CHECK(allow_empty_handle || that != NULL); \
372 EXTRA_CHECK(that == NULL || \
373 (*reinterpret_cast<v8::internal::Object* const*>(that))->Is##To()); \
374 return v8::internal::Handle<v8::internal::To>( \
375 reinterpret_cast<v8::internal::To**>(const_cast<v8::From*>(that))); \
378 OPEN_HANDLE_LIST(MAKE_OPEN_HANDLE)
380 #undef MAKE_OPEN_HANDLE
381 #undef OPEN_HANDLE_LIST
386 // Tracks string usage to help make better decisions when
387 // externalizing strings.
389 // Implementation note: internally this class only tracks fresh
390 // strings and keeps a single use counter for them.
391 class StringTracker {
393 // Records that the given string's characters were copied to some
394 // external buffer. If this happens often we should honor
395 // externalization requests for the string.
396 void RecordWrite(Handle<String> string) {
397 Address address = reinterpret_cast<Address>(*string);
398 Address top = isolate_->heap()->NewSpaceTop();
399 if (IsFreshString(address, top)) {
400 IncrementUseCount(top);
404 // Estimates freshness and use frequency of the given string based
405 // on how close it is to the new space top and the recorded usage
407 inline bool IsFreshUnusedString(Handle<String> string) {
408 Address address = reinterpret_cast<Address>(*string);
409 Address top = isolate_->heap()->NewSpaceTop();
410 return IsFreshString(address, top) && IsUseCountLow(top);
414 StringTracker() : use_count_(0), last_top_(NULL), isolate_(NULL) { }
416 static inline bool IsFreshString(Address string, Address top) {
417 return top - kFreshnessLimit <= string && string <= top;
420 inline bool IsUseCountLow(Address top) {
421 if (last_top_ != top) return true;
422 return use_count_ < kUseLimit;
425 inline void IncrementUseCount(Address top) {
426 if (last_top_ != top) {
433 // Single use counter shared by all fresh strings.
436 // Last new space top when the use count above was valid.
441 // How close to the new space top a fresh string has to be.
442 static const int kFreshnessLimit = 1024;
444 // The number of uses required to consider a string useful.
445 static const int kUseLimit = 32;
447 friend class Isolate;
449 DISALLOW_COPY_AND_ASSIGN(StringTracker);
453 class DeferredHandles {
458 DeferredHandles(Object** first_block_limit, Isolate* isolate)
461 first_block_limit_(first_block_limit),
463 isolate->LinkDeferredHandles(this);
466 void Iterate(ObjectVisitor* v);
468 List<Object**> blocks_;
469 DeferredHandles* next_;
470 DeferredHandles* previous_;
471 Object** first_block_limit_;
474 friend class HandleScopeImplementer;
475 friend class Isolate;
479 // This class is here in order to be able to declare it a friend of
480 // HandleScope. Moving these methods to be members of HandleScope would be
481 // neat in some ways, but it would expose internal implementation details in
482 // our public header file, which is undesirable.
484 // An isolate has a single instance of this class to hold the current thread's
485 // data. In multithreaded V8 programs this data is copied in and out of storage
486 // so that the currently executing thread always has its own copy of this
488 class HandleScopeImplementer {
490 explicit HandleScopeImplementer(Isolate* isolate)
493 entered_contexts_(0),
497 last_handle_before_deferred_block_(NULL) { }
499 ~HandleScopeImplementer() {
503 // Threading support for handle data.
504 static int ArchiveSpacePerThread();
505 char* RestoreThread(char* from);
506 char* ArchiveThread(char* to);
507 void FreeThreadResources();
509 // Garbage collection support.
510 void Iterate(v8::internal::ObjectVisitor* v);
511 static char* Iterate(v8::internal::ObjectVisitor* v, char* data);
514 inline internal::Object** GetSpareOrNewBlock();
515 inline void DeleteExtensions(internal::Object** prev_limit);
517 inline void IncrementCallDepth() {call_depth_++;}
518 inline void DecrementCallDepth() {call_depth_--;}
519 inline bool CallDepthIsZero() { return call_depth_ == 0; }
521 inline void EnterContext(Handle<Context> context);
522 inline void LeaveContext();
523 inline bool LastEnteredContextWas(Handle<Context> context);
525 // Returns the last entered context or an empty handle if no
526 // contexts have been entered.
527 inline Handle<Context> LastEnteredContext();
529 inline void SaveContext(Context* context);
530 inline Context* RestoreContext();
531 inline bool HasSavedContexts();
533 inline List<internal::Object**>* blocks() { return &blocks_; }
534 Isolate* isolate() const { return isolate_; }
536 void ReturnBlock(Object** block) {
537 DCHECK(block != NULL);
538 if (spare_ != NULL) DeleteArray(spare_);
543 void ResetAfterArchive() {
544 blocks_.Initialize(0);
545 entered_contexts_.Initialize(0);
546 saved_contexts_.Initialize(0);
548 last_handle_before_deferred_block_ = NULL;
553 DCHECK(blocks_.length() == 0);
554 DCHECK(entered_contexts_.length() == 0);
555 DCHECK(saved_contexts_.length() == 0);
557 entered_contexts_.Free();
558 saved_contexts_.Free();
559 if (spare_ != NULL) {
563 DCHECK(call_depth_ == 0);
566 void BeginDeferredScope();
567 DeferredHandles* Detach(Object** prev_limit);
570 List<internal::Object**> blocks_;
571 // Used as a stack to keep track of entered contexts.
572 List<Context*> entered_contexts_;
573 // Used as a stack to keep track of saved contexts.
574 List<Context*> saved_contexts_;
577 Object** last_handle_before_deferred_block_;
578 // This is only used for threading support.
579 HandleScopeData handle_scope_data_;
581 void IterateThis(ObjectVisitor* v);
582 char* RestoreThreadHelper(char* from);
583 char* ArchiveThreadHelper(char* to);
585 friend class DeferredHandles;
586 friend class DeferredHandleScope;
588 DISALLOW_COPY_AND_ASSIGN(HandleScopeImplementer);
592 const int kHandleBlockSize = v8::internal::KB - 2; // fit in one page
595 void HandleScopeImplementer::SaveContext(Context* context) {
596 saved_contexts_.Add(context);
600 Context* HandleScopeImplementer::RestoreContext() {
601 return saved_contexts_.RemoveLast();
605 bool HandleScopeImplementer::HasSavedContexts() {
606 return !saved_contexts_.is_empty();
610 void HandleScopeImplementer::EnterContext(Handle<Context> context) {
611 entered_contexts_.Add(*context);
615 void HandleScopeImplementer::LeaveContext() {
616 entered_contexts_.RemoveLast();
620 bool HandleScopeImplementer::LastEnteredContextWas(Handle<Context> context) {
621 return !entered_contexts_.is_empty() && entered_contexts_.last() == *context;
625 Handle<Context> HandleScopeImplementer::LastEnteredContext() {
626 if (entered_contexts_.is_empty()) return Handle<Context>::null();
627 return Handle<Context>(entered_contexts_.last());
631 // If there's a spare block, use it for growing the current scope.
632 internal::Object** HandleScopeImplementer::GetSpareOrNewBlock() {
633 internal::Object** block = (spare_ != NULL) ?
635 NewArray<internal::Object*>(kHandleBlockSize);
641 void HandleScopeImplementer::DeleteExtensions(internal::Object** prev_limit) {
642 while (!blocks_.is_empty()) {
643 internal::Object** block_start = blocks_.last();
644 internal::Object** block_limit = block_start + kHandleBlockSize;
646 // SealHandleScope may make the prev_limit to point inside the block.
647 if (block_start <= prev_limit && prev_limit <= block_limit) {
648 #ifdef ENABLE_HANDLE_ZAPPING
649 internal::HandleScope::ZapRange(prev_limit, block_limit);
654 if (prev_limit == block_limit) break;
657 blocks_.RemoveLast();
658 #ifdef ENABLE_HANDLE_ZAPPING
659 internal::HandleScope::ZapRange(block_start, block_limit);
661 if (spare_ != NULL) {
664 spare_ = block_start;
666 DCHECK((blocks_.is_empty() && prev_limit == NULL) ||
667 (!blocks_.is_empty() && prev_limit != NULL));
671 // Interceptor functions called from generated inline caches to notify
672 // CPU profiler that external callbacks are invoked.
673 void InvokeAccessorGetterCallback(
674 v8::Local<v8::String> property,
675 const v8::PropertyCallbackInfo<v8::Value>& info,
676 v8::AccessorGetterCallback getter);
678 void InvokeFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info,
679 v8::FunctionCallback callback);
683 static v8::Testing::StressType stress_type() { return stress_type_; }
684 static void set_stress_type(v8::Testing::StressType stress_type) {
685 stress_type_ = stress_type;
689 static v8::Testing::StressType stress_type_;
692 } } // namespace v8::internal