Commit 5a71ed0a authored by Igor Sheludko's avatar Igor Sheludko Committed by Commit Bot

[cleanup] Don't use virtual inheritance in gtest fixtures

and use Mixin pattern with linear inheritance instead. This will
allow to customize the way the Isolate is created.

Bug: v8:8238
Change-Id: Ic611df123653af3a0f2271394387492e440b5ea8
Reviewed-on: https://chromium-review.googlesource.com/c/1306433Reviewed-by: 's avatarBenedikt Meurer <bmeurer@chromium.org>
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57106}
parent a46bc035
...@@ -15,9 +15,7 @@ namespace internal { ...@@ -15,9 +15,7 @@ namespace internal {
namespace compiler { namespace compiler {
GraphTest::GraphTest(int num_parameters) GraphTest::GraphTest(int num_parameters)
: TestWithNativeContext(), : canonical_(isolate()),
TestWithIsolateAndZone(),
canonical_(isolate()),
common_(zone()), common_(zone()),
graph_(zone()), graph_(zone()),
broker_(isolate(), zone()), broker_(isolate(), zone()),
......
...@@ -24,8 +24,7 @@ namespace compiler { ...@@ -24,8 +24,7 @@ namespace compiler {
using ::testing::Matcher; using ::testing::Matcher;
class GraphTest : public virtual TestWithNativeContext, class GraphTest : public TestWithNativeContextAndZone {
public virtual TestWithIsolateAndZone {
public: public:
explicit GraphTest(int num_parameters = 1); explicit GraphTest(int num_parameters = 1);
~GraphTest() override; ~GraphTest() override;
......
...@@ -18,8 +18,7 @@ namespace v8 { ...@@ -18,8 +18,7 @@ namespace v8 {
namespace internal { namespace internal {
namespace compiler { namespace compiler {
class InstructionSelectorTest : public TestWithContext, class InstructionSelectorTest : public TestWithNativeContextAndZone {
public TestWithIsolateAndZone {
public: public:
InstructionSelectorTest(); InstructionSelectorTest();
~InstructionSelectorTest() override; ~InstructionSelectorTest() override;
......
...@@ -10,7 +10,7 @@ namespace v8 { ...@@ -10,7 +10,7 @@ namespace v8 {
namespace internal { namespace internal {
namespace compiler { namespace compiler {
class ZoneStatsTest : public TestWithIsolate { class ZoneStatsTest : public ::testing::Test {
public: public:
ZoneStatsTest() : zone_stats_(&allocator_) {} ZoneStatsTest() : zone_stats_(&allocator_) {}
......
...@@ -15,109 +15,28 @@ ...@@ -15,109 +15,28 @@
namespace v8 { namespace v8 {
// static IsolateWrapper::IsolateWrapper()
v8::ArrayBuffer::Allocator* TestWithIsolate::array_buffer_allocator_ = nullptr; : array_buffer_allocator_(
v8::ArrayBuffer::Allocator::NewDefaultAllocator()) {
// static
Isolate* TestWithIsolate::isolate_ = nullptr;
TestWithIsolate::TestWithIsolate()
: isolate_scope_(isolate()), handle_scope_(isolate()) {}
TestWithIsolate::~TestWithIsolate() = default;
// static
void TestWithIsolate::SetUpTestCase() {
Test::SetUpTestCase();
EXPECT_EQ(nullptr, isolate_);
v8::Isolate::CreateParams create_params; v8::Isolate::CreateParams create_params;
array_buffer_allocator_ = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
create_params.array_buffer_allocator = array_buffer_allocator_; create_params.array_buffer_allocator = array_buffer_allocator_;
isolate_ = v8::Isolate::New(create_params); isolate_ = v8::Isolate::New(create_params);
EXPECT_TRUE(isolate_ != nullptr); CHECK_NOT_NULL(isolate_);
} }
IsolateWrapper::~IsolateWrapper() {
// static
void TestWithIsolate::TearDownTestCase() {
ASSERT_TRUE(isolate_ != nullptr);
v8::Platform* platform = internal::V8::GetCurrentPlatform(); v8::Platform* platform = internal::V8::GetCurrentPlatform();
ASSERT_TRUE(platform != nullptr); CHECK_NOT_NULL(platform);
while (platform::PumpMessageLoop(platform, isolate_)) continue; while (platform::PumpMessageLoop(platform, isolate_)) continue;
isolate_->Dispose(); isolate_->Dispose();
isolate_ = nullptr;
delete array_buffer_allocator_; delete array_buffer_allocator_;
Test::TearDownTestCase();
}
Local<Value> TestWithIsolate::RunJS(const char* source) {
Local<Script> script =
v8::Script::Compile(
isolate()->GetCurrentContext(),
v8::String::NewFromUtf8(isolate(), source, v8::NewStringType::kNormal)
.ToLocalChecked())
.ToLocalChecked();
return script->Run(isolate()->GetCurrentContext()).ToLocalChecked();
}
Local<Value> TestWithIsolate::RunJS(
String::ExternalOneByteStringResource* source) {
Local<Script> script =
v8::Script::Compile(
isolate()->GetCurrentContext(),
v8::String::NewExternalOneByte(isolate(), source).ToLocalChecked())
.ToLocalChecked();
return script->Run(isolate()->GetCurrentContext()).ToLocalChecked();
}
TestWithContext::TestWithContext()
: context_(Context::New(isolate())), context_scope_(context_) {}
TestWithContext::~TestWithContext() = default;
v8::Local<v8::String> TestWithContext::NewString(const char* string) {
return v8::String::NewFromUtf8(v8_isolate(), string,
v8::NewStringType::kNormal)
.ToLocalChecked();
} }
void TestWithContext::SetGlobalProperty(const char* name, // static
v8::Local<v8::Value> value) { v8::IsolateWrapper* SharedIsolateHolder::isolate_wrapper_ = nullptr;
CHECK(v8_context()
->Global()
->Set(v8_context(), NewString(name), value)
.FromJust());
}
namespace internal { namespace internal {
TestWithIsolate::~TestWithIsolate() = default;
TestWithIsolateAndZone::~TestWithIsolateAndZone() = default;
Factory* TestWithIsolate::factory() const { return isolate()->factory(); }
Handle<Object> TestWithIsolate::RunJSInternal(const char* source) {
return Utils::OpenHandle(*::v8::TestWithIsolate::RunJS(source));
}
Handle<Object> TestWithIsolate::RunJSInternal(
::v8::String::ExternalOneByteStringResource* source) {
return Utils::OpenHandle(*::v8::TestWithIsolate::RunJS(source));
}
base::RandomNumberGenerator* TestWithIsolate::random_number_generator() const {
return isolate()->random_number_generator();
}
TestWithZone::~TestWithZone() = default;
TestWithNativeContext::~TestWithNativeContext() = default;
Handle<Context> TestWithNativeContext::native_context() const {
return isolate()->native_context();
}
SaveFlags::SaveFlags() { non_default_flags_ = FlagList::argv(); } SaveFlags::SaveFlags() { non_default_flags_ = FlagList::argv(); }
SaveFlags::~SaveFlags() { SaveFlags::~SaveFlags() {
......
...@@ -21,126 +21,247 @@ namespace v8 { ...@@ -21,126 +21,247 @@ namespace v8 {
class ArrayBufferAllocator; class ArrayBufferAllocator;
// Use v8::internal::TestWithIsolate if you are testing internals, // RAII-like Isolate instance wrapper.
// aka. directly work with Handles. class IsolateWrapper final {
class TestWithIsolate : public virtual ::testing::Test {
public: public:
TestWithIsolate(); IsolateWrapper();
~TestWithIsolate() override; ~IsolateWrapper();
v8::Isolate* isolate() const { return v8_isolate(); } v8::Isolate* isolate() const { return isolate_; }
v8::Isolate* v8_isolate() const { return isolate_; } private:
v8::ArrayBuffer::Allocator* array_buffer_allocator_;
v8::Isolate* isolate_;
v8::internal::Isolate* i_isolate() const { DISALLOW_COPY_AND_ASSIGN(IsolateWrapper);
return reinterpret_cast<v8::internal::Isolate*>(isolate()); };
class SharedIsolateHolder final {
public:
static v8::Isolate* isolate() { return isolate_wrapper_->isolate(); }
static void CreateIsolate() {
CHECK_NULL(isolate_wrapper_);
isolate_wrapper_ = new IsolateWrapper();
}
static void DeleteIsolate() {
CHECK_NOT_NULL(isolate_wrapper_);
delete isolate_wrapper_;
isolate_wrapper_ = nullptr;
}
private:
static v8::IsolateWrapper* isolate_wrapper_;
DISALLOW_IMPLICIT_CONSTRUCTORS(SharedIsolateHolder);
};
//
// A set of mixins from which the test fixtures will be constructed.
//
template <typename TMixin>
class WithSharedIsolateMixin : public TMixin {
public:
WithSharedIsolateMixin() = default;
v8::Isolate* v8_isolate() const { return SharedIsolateHolder::isolate(); }
static void SetUpTestCase() {
TMixin::SetUpTestCase();
SharedIsolateHolder::CreateIsolate();
}
static void TearDownTestCase() {
SharedIsolateHolder::DeleteIsolate();
TMixin::TearDownTestCase();
} }
Local<Value> RunJS(const char* source); private:
Local<Value> RunJS(String::ExternalOneByteStringResource* source); DISALLOW_COPY_AND_ASSIGN(WithSharedIsolateMixin);
};
template <typename TMixin>
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::internal::Isolate*>(v8_isolate());
}
static void SetUpTestCase(); static void SetUpTestCase() { TMixin::SetUpTestCase(); }
static void TearDownTestCase(); static void TearDownTestCase() { TMixin::TearDownTestCase(); }
private: private:
static v8::ArrayBuffer::Allocator* array_buffer_allocator_;
static v8::Isolate* isolate_;
v8::Isolate::Scope isolate_scope_; v8::Isolate::Scope isolate_scope_;
v8::HandleScope handle_scope_; v8::HandleScope handle_scope_;
DISALLOW_COPY_AND_ASSIGN(TestWithIsolate); DISALLOW_COPY_AND_ASSIGN(WithIsolateScopeMixin);
}; };
// Use v8::internal::TestWithNativeContext if you are testing internals, template <typename TMixin>
// aka. directly work with Handles. class WithContextMixin : public TMixin {
class TestWithContext : public virtual v8::TestWithIsolate {
public: public:
TestWithContext(); WithContextMixin()
~TestWithContext() override; : context_(Context::New(v8_isolate())), context_scope_(context_) {}
v8::Isolate* v8_isolate() const { return TMixin::v8_isolate(); }
const Local<Context>& context() const { return v8_context(); } const Local<Context>& context() const { return v8_context(); }
const Local<Context>& v8_context() const { return context_; } const Local<Context>& v8_context() const { return context_; }
v8::Local<v8::String> NewString(const char* string); Local<Value> RunJS(const char* source) {
void SetGlobalProperty(const char* name, v8::Local<v8::Value> value); return RunJS(v8::String::NewFromUtf8(v8_isolate(), source,
v8::NewStringType::kNormal)
.ToLocalChecked());
}
Local<Value> RunJS(v8::String::ExternalOneByteStringResource* source) {
return RunJS(
v8::String::NewExternalOneByte(v8_isolate(), source).ToLocalChecked());
}
v8::Local<v8::String> NewString(const char* string) {
return v8::String::NewFromUtf8(v8_isolate(), string,
v8::NewStringType::kNormal)
.ToLocalChecked();
}
void SetGlobalProperty(const char* name, v8::Local<v8::Value> value) {
CHECK(v8_context()
->Global()
->Set(v8_context(), NewString(name), value)
.FromJust());
}
static void SetUpTestCase() { TMixin::SetUpTestCase(); }
static void TearDownTestCase() { TMixin::TearDownTestCase(); }
private: private:
Local<Context> context_; Local<Value> RunJS(Local<String> source) {
auto context = v8_isolate()->GetCurrentContext();
Local<Script> script =
v8::Script::Compile(context, source).ToLocalChecked();
return script->Run(context).ToLocalChecked();
}
v8::Local<v8::Context> context_;
v8::Context::Scope context_scope_; v8::Context::Scope context_scope_;
DISALLOW_COPY_AND_ASSIGN(TestWithContext); DISALLOW_COPY_AND_ASSIGN(WithContextMixin);
}; };
// Use v8::internal::TestWithIsolate if you are testing internals,
// aka. directly work with Handles.
using TestWithIsolate = //
WithIsolateScopeMixin< //
WithSharedIsolateMixin< //
::testing::Test>>;
// Use v8::internal::TestWithNativeContext if you are testing internals,
// aka. directly work with Handles.
using TestWithContext = //
WithContextMixin< //
WithIsolateScopeMixin< //
WithSharedIsolateMixin< //
::testing::Test>>>;
namespace internal { namespace internal {
// Forward declarations. // Forward declarations.
class Factory; class Factory;
template <typename TMixin>
class TestWithIsolate : public virtual ::v8::TestWithIsolate { class WithInternalIsolateMixin : public TMixin {
public: public:
TestWithIsolate() = default; WithInternalIsolateMixin() = default;
~TestWithIsolate() override;
Factory* factory() const { return isolate()->factory(); }
Isolate* isolate() const { return TMixin::i_isolate(); }
Handle<Context> native_context() const { return isolate()->native_context(); }
Factory* factory() const;
Isolate* isolate() const { return i_isolate(); }
template <typename T = Object> template <typename T = Object>
Handle<T> RunJS(const char* source) { Handle<T> RunJS(const char* source) {
return Handle<T>::cast(RunJSInternal(source)); return Handle<T>::cast(RunJSInternal(source));
} }
Handle<Object> RunJSInternal(const char* source);
Handle<Object> RunJSInternal(const char* source) {
return Utils::OpenHandle(*TMixin::RunJS(source));
}
template <typename T = Object> template <typename T = Object>
Handle<T> RunJS(::v8::String::ExternalOneByteStringResource* source) { Handle<T> RunJS(::v8::String::ExternalOneByteStringResource* source) {
return Handle<T>::cast(RunJSInternal(source)); return Handle<T>::cast(RunJSInternal(source));
} }
Handle<Object> RunJSInternal(
::v8::String::ExternalOneByteStringResource* source);
base::RandomNumberGenerator* random_number_generator() const;
private: Handle<Object> RunJSInternal(
DISALLOW_COPY_AND_ASSIGN(TestWithIsolate); ::v8::String::ExternalOneByteStringResource* source) {
}; return Utils::OpenHandle(*TMixin::RunJS(source));
}
class TestWithZone : public virtual ::testing::Test { base::RandomNumberGenerator* random_number_generator() const {
public: return isolate()->random_number_generator();
TestWithZone() : zone_(&allocator_, ZONE_NAME) {} }
~TestWithZone() override;
Zone* zone() { return &zone_; } static void SetUpTestCase() { TMixin::SetUpTestCase(); }
static void TearDownTestCase() { TMixin::TearDownTestCase(); }
private: private:
v8::internal::AccountingAllocator allocator_; DISALLOW_COPY_AND_ASSIGN(WithInternalIsolateMixin);
Zone zone_;
DISALLOW_COPY_AND_ASSIGN(TestWithZone);
}; };
class TestWithIsolateAndZone : public virtual TestWithIsolate { template <typename TMixin>
class WithZoneMixin : public TMixin {
public: public:
TestWithIsolateAndZone() : zone_(&allocator_, ZONE_NAME) {} WithZoneMixin() : zone_(&allocator_, ZONE_NAME) {}
~TestWithIsolateAndZone() override;
Zone* zone() { return &zone_; } Zone* zone() { return &zone_; }
static void SetUpTestCase() { TMixin::SetUpTestCase(); }
static void TearDownTestCase() { TMixin::TearDownTestCase(); }
private: private:
v8::internal::AccountingAllocator allocator_; v8::internal::AccountingAllocator allocator_;
Zone zone_; Zone zone_;
DISALLOW_COPY_AND_ASSIGN(TestWithIsolateAndZone); DISALLOW_COPY_AND_ASSIGN(WithZoneMixin);
}; };
class TestWithNativeContext : public virtual ::v8::TestWithContext, using TestWithIsolate = //
public virtual TestWithIsolate { WithInternalIsolateMixin< //
public: WithIsolateScopeMixin< //
TestWithNativeContext() = default; WithSharedIsolateMixin< //
~TestWithNativeContext() override; ::testing::Test>>>;
Handle<Context> native_context() const; using TestWithZone = WithZoneMixin<::testing::Test>;
private: using TestWithIsolateAndZone = //
DISALLOW_COPY_AND_ASSIGN(TestWithNativeContext); WithInternalIsolateMixin< //
}; WithIsolateScopeMixin< //
WithSharedIsolateMixin< //
WithZoneMixin< //
::testing::Test>>>>;
using TestWithNativeContext = //
WithInternalIsolateMixin< //
WithContextMixin< //
WithIsolateScopeMixin< //
WithSharedIsolateMixin< //
::testing::Test>>>>;
using TestWithNativeContextAndZone = //
WithZoneMixin< //
WithInternalIsolateMixin< //
WithContextMixin< //
WithIsolateScopeMixin< //
WithSharedIsolateMixin< //
::testing::Test>>>>>;
class SaveFlags { class SaveFlags {
public: public:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment