// Copyright 2014 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_UNITTESTS_TEST_UTILS_H_ #define V8_UNITTESTS_TEST_UTILS_H_ #include #include #include "include/libplatform/libplatform.h" #include "include/v8-array-buffer.h" #include "include/v8-context.h" #include "include/v8-local-handle.h" #include "include/v8-primitive.h" #include "src/api/api-inl.h" #include "src/base/macros.h" #include "src/base/utils/random-number-generator.h" #include "src/handles/handles.h" #include "src/objects/objects-inl.h" #include "src/objects/objects.h" #include "src/zone/accounting-allocator.h" #include "src/zone/zone.h" #include "testing/gtest-support.h" namespace v8 { class ArrayBufferAllocator; template class WithDefaultPlatformMixin : public TMixin { public: WithDefaultPlatformMixin() { platform_ = v8::platform::NewDefaultPlatform( 0, v8::platform::IdleTaskSupport::kEnabled); CHECK_NOT_NULL(platform_.get()); v8::V8::InitializePlatform(platform_.get()); #ifdef V8_SANDBOX CHECK(v8::V8::InitializeSandbox()); #endif // V8_SANDBOX v8::V8::Initialize(); } ~WithDefaultPlatformMixin() { CHECK_NOT_NULL(platform_.get()); v8::V8::Dispose(); v8::V8::DisposePlatform(); } v8::Platform* platform() const { return platform_.get(); } private: std::unique_ptr platform_; }; using CounterMap = std::map; enum CountersMode { kNoCounters, kEnableCounters }; // RAII-like Isolate instance wrapper. class IsolateWrapper final { public: explicit IsolateWrapper(CountersMode counters_mode); ~IsolateWrapper(); IsolateWrapper(const IsolateWrapper&) = delete; IsolateWrapper& operator=(const IsolateWrapper&) = delete; v8::Isolate* isolate() const { return isolate_; } private: std::unique_ptr array_buffer_allocator_; std::unique_ptr counter_map_; v8::Isolate* isolate_; }; // // A set of mixins from which the test fixtures will be constructed. // template class WithIsolateMixin : public TMixin { public: WithIsolateMixin() : isolate_wrapper_(kCountersMode) {} v8::Isolate* v8_isolate() const { return isolate_wrapper_.isolate(); } private: v8::IsolateWrapper isolate_wrapper_; }; template class WithIsolateScopeMixin : public TMixin { public: WithIsolateScopeMixin() : isolate_scope_(this->v8_isolate()), handle_scope_(this->v8_isolate()) {} WithIsolateScopeMixin(const WithIsolateScopeMixin&) = delete; WithIsolateScopeMixin& operator=(const WithIsolateScopeMixin&) = delete; v8::Isolate* isolate() const { return this->v8_isolate(); } v8::internal::Isolate* i_isolate() const { return reinterpret_cast(this->v8_isolate()); } Local RunJS(const char* source) { return RunJS( v8::String::NewFromUtf8(this->v8_isolate(), source).ToLocalChecked()); } Local RunJS(v8::String::ExternalOneByteStringResource* source) { return RunJS(v8::String::NewExternalOneByte(this->v8_isolate(), source) .ToLocalChecked()); } void CollectGarbage(i::AllocationSpace space) { i_isolate()->heap()->CollectGarbage(space, i::GarbageCollectionReason::kTesting); } void CollectAllGarbage() { i_isolate()->heap()->CollectAllGarbage( i::Heap::kNoGCFlags, i::GarbageCollectionReason::kTesting); } void CollectAllAvailableGarbage() { i_isolate()->heap()->CollectAllAvailableGarbage( i::GarbageCollectionReason::kTesting); } void PreciseCollectAllGarbage() { i_isolate()->heap()->PreciseCollectAllGarbage( i::Heap::kNoGCFlags, i::GarbageCollectionReason::kTesting); } v8::Local NewString(const char* string) { return v8::String::NewFromUtf8(this->v8_isolate(), string).ToLocalChecked(); } private: Local RunJS(Local source) { auto context = this->v8_isolate()->GetCurrentContext(); Local