// 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/v8.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; typedef std::map CounterMap; // RAII-like Isolate instance wrapper. class IsolateWrapper final { public: // When enforce_pointer_compression is true the Isolate is created with // enabled pointer compression. When it's false then the Isolate is created // with the default pointer compression state for current build. explicit IsolateWrapper(CounterLookupCallback counter_lookup_callback, bool enforce_pointer_compression = false); ~IsolateWrapper(); v8::Isolate* isolate() const { return isolate_; } private: v8::ArrayBuffer::Allocator* array_buffer_allocator_; v8::Isolate* isolate_; DISALLOW_COPY_AND_ASSIGN(IsolateWrapper); }; class SharedIsolateHolder final { public: static v8::Isolate* isolate() { return isolate_wrapper_->isolate(); } static void CreateIsolate() { CHECK_NULL(isolate_wrapper_); isolate_wrapper_ = new IsolateWrapper([](const char* name) -> int* { return nullptr; }); } static void DeleteIsolate() { CHECK_NOT_NULL(isolate_wrapper_); delete isolate_wrapper_; isolate_wrapper_ = nullptr; } private: static v8::IsolateWrapper* isolate_wrapper_; DISALLOW_IMPLICIT_CONSTRUCTORS(SharedIsolateHolder); }; class SharedIsolateAndCountersHolder final { public: static v8::Isolate* isolate() { return isolate_wrapper_->isolate(); } static void CreateIsolate() { CHECK_NULL(counter_map_); CHECK_NULL(isolate_wrapper_); counter_map_ = new CounterMap(); isolate_wrapper_ = new IsolateWrapper(LookupCounter); } static void DeleteIsolate() { CHECK_NOT_NULL(counter_map_); CHECK_NOT_NULL(isolate_wrapper_); delete isolate_wrapper_; isolate_wrapper_ = nullptr; delete counter_map_; counter_map_ = nullptr; } private: static int* LookupCounter(const char* name); static CounterMap* counter_map_; static v8::IsolateWrapper* isolate_wrapper_; DISALLOW_IMPLICIT_CONSTRUCTORS(SharedIsolateAndCountersHolder); }; // // A set of mixins from which the test fixtures will be constructed. // template class WithPrivateIsolateMixin : public TMixin { public: explicit WithPrivateIsolateMixin(bool enforce_pointer_compression = false) : isolate_wrapper_([](const char* name) -> int* { return nullptr; }, enforce_pointer_compression) {} v8::Isolate* v8_isolate() const { return isolate_wrapper_.isolate(); } static void SetUpTestCase() { TMixin::SetUpTestCase(); } static void TearDownTestCase() { TMixin::TearDownTestCase(); } private: v8::IsolateWrapper isolate_wrapper_; DISALLOW_COPY_AND_ASSIGN(WithPrivateIsolateMixin); }; template class WithSharedIsolateMixin : public TMixin { public: WithSharedIsolateMixin() = default; v8::Isolate* v8_isolate() const { return TSharedIsolateHolder::isolate(); } static void SetUpTestCase() { TMixin::SetUpTestCase(); TSharedIsolateHolder::CreateIsolate(); } static void TearDownTestCase() { TSharedIsolateHolder::DeleteIsolate(); TMixin::TearDownTestCase(); } private: DISALLOW_COPY_AND_ASSIGN(WithSharedIsolateMixin); }; template class WithPointerCompressionIsolateMixin : public WithPrivateIsolateMixin { public: WithPointerCompressionIsolateMixin() : WithPrivateIsolateMixin(true) {} private: DISALLOW_COPY_AND_ASSIGN(WithPointerCompressionIsolateMixin); }; template class WithIsolateScopeMixin : public TMixin { public: WithIsolateScopeMixin() : isolate_scope_(v8_isolate()), handle_scope_(v8_isolate()) {} v8::Isolate* isolate() const { return v8_isolate(); } v8::Isolate* v8_isolate() const { return TMixin::v8_isolate(); } v8::internal::Isolate* i_isolate() const { return reinterpret_cast(v8_isolate()); } static void SetUpTestCase() { TMixin::SetUpTestCase(); } static void TearDownTestCase() { TMixin::TearDownTestCase(); } private: v8::Isolate::Scope isolate_scope_; v8::HandleScope handle_scope_; DISALLOW_COPY_AND_ASSIGN(WithIsolateScopeMixin); }; template class WithContextMixin : public TMixin { public: WithContextMixin() : context_(Context::New(v8_isolate())), context_scope_(context_) {} v8::Isolate* v8_isolate() const { return TMixin::v8_isolate(); } const Local& context() const { return v8_context(); } const Local& v8_context() const { return context_; } Local RunJS(const char* source) { return RunJS(v8::String::NewFromUtf8(v8_isolate(), source, v8::NewStringType::kNormal) .ToLocalChecked()); } Local RunJS(v8::String::ExternalOneByteStringResource* source) { return RunJS( v8::String::NewExternalOneByte(v8_isolate(), source).ToLocalChecked()); } v8::Local NewString(const char* string) { return v8::String::NewFromUtf8(v8_isolate(), string, v8::NewStringType::kNormal) .ToLocalChecked(); } void SetGlobalProperty(const char* name, v8::Local value) { CHECK(v8_context() ->Global() ->Set(v8_context(), NewString(name), value) .FromJust()); } static void SetUpTestCase() { TMixin::SetUpTestCase(); } static void TearDownTestCase() { TMixin::TearDownTestCase(); } private: Local RunJS(Local source) { auto context = v8_isolate()->GetCurrentContext(); Local