// 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-extension.h" #include "include/v8-local-handle.h" #include "include/v8-primitive.h" #include "include/v8-template.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_ENABLE_SANDBOX CHECK(v8::V8::InitializeSandbox()); #endif // V8_ENABLE_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 }; enum IsolateSharedMode { kStandaloneIsolate, kSharedIsolate, kClientIsolate }; // RAII-like Isolate instance wrapper. // // It is the caller's responsibility to ensure that the shared Isolate outlives // all client Isolates. class IsolateWrapper final { public: IsolateWrapper(CountersMode counters_mode, IsolateSharedMode shared_mode = kStandaloneIsolate, v8::Isolate* shared_isolate_if_client = nullptr); ~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, kStandaloneIsolate) {} v8::Isolate* v8_isolate() const { return isolate_wrapper_.isolate(); } private: v8::IsolateWrapper isolate_wrapper_; }; // Warning: This is not a drop-in replacement for WithIsolateMixin! // // Users of WithMaybeSharedIsolateMixin, including TEST_F tests and classes that // mix this class in, must explicit check IsJSSharedMemorySupported() before // calling v8_isolate(). Creating shared Isolates is not supported on all build // configurations. template class WithMaybeSharedIsolateMixin : public TMixin { public: WithMaybeSharedIsolateMixin() { if (IsJSSharedMemorySupported()) { isolate_wrapper_.emplace(kCountersMode, kSharedIsolate); } } bool IsJSSharedMemorySupported() const { DCHECK_IMPLIES( internal::ReadOnlyHeap::IsReadOnlySpaceShared(), !COMPRESS_POINTERS_BOOL || COMPRESS_POINTERS_IN_SHARED_CAGE_BOOL); return internal::ReadOnlyHeap::IsReadOnlySpaceShared(); } v8::Isolate* v8_isolate() const { DCHECK(IsJSSharedMemorySupported()); return isolate_wrapper_->isolate(); } private: base::Optional 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()); } i::Handle MakeName(const char* str, int suffix) { v8::base::EmbeddedVector buffer; v8::base::SNPrintF(buffer, "%s%d", str, suffix); return MakeString(buffer.begin()); } i::Handle MakeString(const char* str) { i::Factory* factory = i_isolate()->factory(); return factory->InternalizeUtf8String(str); } Local RunJS(const char* source) { return RunJS( v8::String::NewFromUtf8(this->v8_isolate(), source).ToLocalChecked()); } MaybeLocal TryRunJS(const char* source) { return TryRunJS( v8::String::NewFromUtf8(this->v8_isolate(), source).ToLocalChecked()); } static MaybeLocal TryRunJS(Isolate* isolate, Local source) { auto context = isolate->GetCurrentContext(); v8::Local result; Local