2014-09-04 08:44:03 +00:00
|
|
|
// 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.
|
|
|
|
|
2014-10-01 08:34:25 +00:00
|
|
|
#ifndef V8_UNITTESTS_TEST_UTILS_H_
|
|
|
|
#define V8_UNITTESTS_TEST_UTILS_H_
|
2014-09-04 08:44:03 +00:00
|
|
|
|
2017-08-30 07:24:59 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2014-09-04 08:44:03 +00:00
|
|
|
#include "include/v8.h"
|
2018-11-23 10:52:27 +00:00
|
|
|
#include "src/api-inl.h"
|
2014-09-04 08:44:03 +00:00
|
|
|
#include "src/base/macros.h"
|
2014-10-06 12:27:24 +00:00
|
|
|
#include "src/base/utils/random-number-generator.h"
|
2017-11-13 12:04:57 +00:00
|
|
|
#include "src/handles.h"
|
|
|
|
#include "src/objects-inl.h"
|
|
|
|
#include "src/objects.h"
|
2016-09-20 16:07:25 +00:00
|
|
|
#include "src/zone/accounting-allocator.h"
|
|
|
|
#include "src/zone/zone.h"
|
2014-09-04 08:44:03 +00:00
|
|
|
#include "testing/gtest-support.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
|
2015-04-29 09:54:34 +00:00
|
|
|
class ArrayBufferAllocator;
|
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
// RAII-like Isolate instance wrapper.
|
|
|
|
class IsolateWrapper final {
|
2014-09-04 08:44:03 +00:00
|
|
|
public:
|
2018-10-30 12:48:12 +00:00
|
|
|
// 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.
|
2019-03-28 07:57:51 +00:00
|
|
|
explicit IsolateWrapper(bool enforce_pointer_compression = false);
|
2018-10-29 20:49:54 +00:00
|
|
|
~IsolateWrapper();
|
2014-09-04 08:44:03 +00:00
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
v8::Isolate* isolate() const { return isolate_; }
|
2017-11-14 09:15:41 +00:00
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
private:
|
|
|
|
v8::ArrayBuffer::Allocator* array_buffer_allocator_;
|
|
|
|
v8::Isolate* isolate_;
|
2014-09-04 08:44:03 +00:00
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(IsolateWrapper);
|
|
|
|
};
|
|
|
|
|
|
|
|
class SharedIsolateHolder final {
|
|
|
|
public:
|
|
|
|
static v8::Isolate* isolate() { return isolate_wrapper_->isolate(); }
|
|
|
|
|
|
|
|
static void CreateIsolate() {
|
|
|
|
CHECK_NULL(isolate_wrapper_);
|
2019-03-28 07:57:51 +00:00
|
|
|
isolate_wrapper_ = new IsolateWrapper();
|
2018-10-29 20:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
//
|
2018-10-30 12:48:12 +00:00
|
|
|
template <typename TMixin>
|
|
|
|
class WithPrivateIsolateMixin : public TMixin {
|
|
|
|
public:
|
|
|
|
explicit WithPrivateIsolateMixin(bool enforce_pointer_compression = false)
|
2019-03-28 07:57:51 +00:00
|
|
|
: isolate_wrapper_(enforce_pointer_compression) {}
|
2018-10-30 12:48:12 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2019-03-28 07:57:51 +00:00
|
|
|
template <typename TMixin>
|
2018-10-29 20:49:54 +00:00
|
|
|
class WithSharedIsolateMixin : public TMixin {
|
|
|
|
public:
|
|
|
|
WithSharedIsolateMixin() = default;
|
|
|
|
|
2019-03-28 07:57:51 +00:00
|
|
|
v8::Isolate* v8_isolate() const { return SharedIsolateHolder::isolate(); }
|
2018-10-29 20:49:54 +00:00
|
|
|
|
|
|
|
static void SetUpTestCase() {
|
|
|
|
TMixin::SetUpTestCase();
|
2019-03-28 07:57:51 +00:00
|
|
|
SharedIsolateHolder::CreateIsolate();
|
2018-10-29 20:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void TearDownTestCase() {
|
2019-03-28 07:57:51 +00:00
|
|
|
SharedIsolateHolder::DeleteIsolate();
|
2018-10-29 20:49:54 +00:00
|
|
|
TMixin::TearDownTestCase();
|
2017-02-10 16:01:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(WithSharedIsolateMixin);
|
|
|
|
};
|
|
|
|
|
2018-10-30 12:48:12 +00:00
|
|
|
template <typename TMixin>
|
|
|
|
class WithPointerCompressionIsolateMixin
|
|
|
|
: public WithPrivateIsolateMixin<TMixin> {
|
|
|
|
public:
|
|
|
|
WithPointerCompressionIsolateMixin()
|
|
|
|
: WithPrivateIsolateMixin<TMixin>(true) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(WithPointerCompressionIsolateMixin);
|
|
|
|
};
|
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
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());
|
|
|
|
}
|
2017-11-13 12:04:57 +00:00
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
static void SetUpTestCase() { TMixin::SetUpTestCase(); }
|
|
|
|
static void TearDownTestCase() { TMixin::TearDownTestCase(); }
|
2014-09-04 08:44:03 +00:00
|
|
|
|
|
|
|
private:
|
2017-11-14 09:15:41 +00:00
|
|
|
v8::Isolate::Scope isolate_scope_;
|
|
|
|
v8::HandleScope handle_scope_;
|
2014-09-04 08:44:03 +00:00
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(WithIsolateScopeMixin);
|
2014-09-04 08:44:03 +00:00
|
|
|
};
|
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
template <typename TMixin>
|
|
|
|
class WithContextMixin : public TMixin {
|
2014-09-04 08:44:03 +00:00
|
|
|
public:
|
2018-10-29 20:49:54 +00:00
|
|
|
WithContextMixin()
|
|
|
|
: context_(Context::New(v8_isolate())), context_scope_(context_) {}
|
|
|
|
|
|
|
|
v8::Isolate* v8_isolate() const { return TMixin::v8_isolate(); }
|
2014-09-04 08:44:03 +00:00
|
|
|
|
2017-11-14 09:15:41 +00:00
|
|
|
const Local<Context>& context() const { return v8_context(); }
|
|
|
|
const Local<Context>& v8_context() const { return context_; }
|
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
Local<Value> RunJS(const char* source) {
|
|
|
|
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(); }
|
2014-09-04 08:44:03 +00:00
|
|
|
|
|
|
|
private:
|
2018-10-29 20:49:54 +00:00
|
|
|
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_;
|
2017-11-14 09:15:41 +00:00
|
|
|
v8::Context::Scope context_scope_;
|
2014-09-04 08:44:03 +00:00
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(WithContextMixin);
|
2014-09-04 08:44:03 +00:00
|
|
|
};
|
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
// 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>>>;
|
|
|
|
|
2018-10-30 12:48:12 +00:00
|
|
|
using TestWithIsolateAndPointerCompression = //
|
|
|
|
WithContextMixin< //
|
|
|
|
WithIsolateScopeMixin< //
|
|
|
|
WithPointerCompressionIsolateMixin< //
|
|
|
|
::testing::Test>>>;
|
|
|
|
|
2014-09-04 08:44:03 +00:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
// Forward declarations.
|
|
|
|
class Factory;
|
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
template <typename TMixin>
|
|
|
|
class WithInternalIsolateMixin : public TMixin {
|
2014-09-04 08:44:03 +00:00
|
|
|
public:
|
2018-10-29 20:49:54 +00:00
|
|
|
WithInternalIsolateMixin() = default;
|
|
|
|
|
|
|
|
Factory* factory() const { return isolate()->factory(); }
|
|
|
|
Isolate* isolate() const { return TMixin::i_isolate(); }
|
|
|
|
|
2019-01-17 11:31:28 +00:00
|
|
|
Handle<NativeContext> native_context() const {
|
|
|
|
return isolate()->native_context();
|
|
|
|
}
|
2014-09-04 08:44:03 +00:00
|
|
|
|
2017-11-13 12:04:57 +00:00
|
|
|
template <typename T = Object>
|
|
|
|
Handle<T> RunJS(const char* source) {
|
2018-07-20 09:19:29 +00:00
|
|
|
return Handle<T>::cast(RunJSInternal(source));
|
2017-11-13 12:04:57 +00:00
|
|
|
}
|
2018-10-29 20:49:54 +00:00
|
|
|
|
|
|
|
Handle<Object> RunJSInternal(const char* source) {
|
|
|
|
return Utils::OpenHandle(*TMixin::RunJS(source));
|
|
|
|
}
|
|
|
|
|
2018-09-20 13:18:16 +00:00
|
|
|
template <typename T = Object>
|
|
|
|
Handle<T> RunJS(::v8::String::ExternalOneByteStringResource* source) {
|
|
|
|
return Handle<T>::cast(RunJSInternal(source));
|
|
|
|
}
|
2014-09-04 08:44:03 +00:00
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
Handle<Object> RunJSInternal(
|
|
|
|
::v8::String::ExternalOneByteStringResource* source) {
|
|
|
|
return Utils::OpenHandle(*TMixin::RunJS(source));
|
|
|
|
}
|
2014-09-04 08:44:03 +00:00
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
base::RandomNumberGenerator* random_number_generator() const {
|
|
|
|
return isolate()->random_number_generator();
|
|
|
|
}
|
2014-09-04 08:44:03 +00:00
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
static void SetUpTestCase() { TMixin::SetUpTestCase(); }
|
|
|
|
static void TearDownTestCase() { TMixin::TearDownTestCase(); }
|
2014-09-04 08:44:03 +00:00
|
|
|
|
|
|
|
private:
|
2018-10-29 20:49:54 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(WithInternalIsolateMixin);
|
2014-09-04 08:44:03 +00:00
|
|
|
};
|
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
template <typename TMixin>
|
|
|
|
class WithZoneMixin : public TMixin {
|
2015-01-23 16:29:50 +00:00
|
|
|
public:
|
2018-10-29 20:49:54 +00:00
|
|
|
WithZoneMixin() : zone_(&allocator_, ZONE_NAME) {}
|
2015-01-23 16:29:50 +00:00
|
|
|
|
|
|
|
Zone* zone() { return &zone_; }
|
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
static void SetUpTestCase() { TMixin::SetUpTestCase(); }
|
|
|
|
static void TearDownTestCase() { TMixin::TearDownTestCase(); }
|
|
|
|
|
2015-01-23 16:29:50 +00:00
|
|
|
private:
|
2016-09-20 16:07:25 +00:00
|
|
|
v8::internal::AccountingAllocator allocator_;
|
2015-01-23 16:29:50 +00:00
|
|
|
Zone zone_;
|
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(WithZoneMixin);
|
2015-01-23 16:29:50 +00:00
|
|
|
};
|
|
|
|
|
2018-10-29 20:49:54 +00:00
|
|
|
using TestWithIsolate = //
|
|
|
|
WithInternalIsolateMixin< //
|
|
|
|
WithIsolateScopeMixin< //
|
|
|
|
WithSharedIsolateMixin< //
|
|
|
|
::testing::Test>>>;
|
|
|
|
|
|
|
|
using TestWithZone = WithZoneMixin<::testing::Test>;
|
|
|
|
|
|
|
|
using TestWithIsolateAndZone = //
|
|
|
|
WithInternalIsolateMixin< //
|
|
|
|
WithIsolateScopeMixin< //
|
|
|
|
WithSharedIsolateMixin< //
|
|
|
|
WithZoneMixin< //
|
|
|
|
::testing::Test>>>>;
|
|
|
|
|
|
|
|
using TestWithNativeContext = //
|
|
|
|
WithInternalIsolateMixin< //
|
|
|
|
WithContextMixin< //
|
|
|
|
WithIsolateScopeMixin< //
|
|
|
|
WithSharedIsolateMixin< //
|
|
|
|
::testing::Test>>>>;
|
|
|
|
|
|
|
|
using TestWithNativeContextAndZone = //
|
|
|
|
WithZoneMixin< //
|
|
|
|
WithInternalIsolateMixin< //
|
|
|
|
WithContextMixin< //
|
|
|
|
WithIsolateScopeMixin< //
|
|
|
|
WithSharedIsolateMixin< //
|
|
|
|
::testing::Test>>>>>;
|
2016-10-10 05:53:31 +00:00
|
|
|
|
2017-03-14 13:33:13 +00:00
|
|
|
class SaveFlags {
|
|
|
|
public:
|
|
|
|
SaveFlags();
|
|
|
|
~SaveFlags();
|
|
|
|
|
|
|
|
private:
|
2019-03-19 14:57:50 +00:00
|
|
|
#define FLAG_MODE_APPLY(ftype, ctype, nam, def, cmt) ctype SAVED_##nam;
|
|
|
|
#include "src/flag-definitions.h" // NOLINT
|
|
|
|
#undef FLAG_MODE_APPLY
|
2017-03-14 13:33:13 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(SaveFlags);
|
|
|
|
};
|
|
|
|
|
2018-11-29 09:09:52 +00:00
|
|
|
// For GTest.
|
2019-01-07 12:28:05 +00:00
|
|
|
inline void PrintTo(Object o, ::std::ostream* os) {
|
2018-11-29 09:09:52 +00:00
|
|
|
*os << reinterpret_cast<void*>(o.ptr());
|
|
|
|
}
|
|
|
|
inline void PrintTo(Smi o, ::std::ostream* os) {
|
|
|
|
*os << reinterpret_cast<void*>(o.ptr());
|
|
|
|
}
|
|
|
|
|
2014-09-04 08:44:03 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
2014-10-01 08:34:25 +00:00
|
|
|
#endif // V8_UNITTESTS_TEST_UTILS_H_
|