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"
|
|
|
|
#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;
|
|
|
|
|
2017-11-13 12:04:57 +00:00
|
|
|
// Use v8::internal::TestWithIsolate if you are testing internals,
|
|
|
|
// aka. directly work with Handles.
|
2015-01-26 11:21:14 +00:00
|
|
|
class TestWithIsolate : public virtual ::testing::Test {
|
2014-09-04 08:44:03 +00:00
|
|
|
public:
|
|
|
|
TestWithIsolate();
|
2018-09-14 15:34:02 +00:00
|
|
|
~TestWithIsolate() override;
|
2014-09-04 08:44:03 +00:00
|
|
|
|
2017-11-14 09:15:41 +00:00
|
|
|
v8::Isolate* isolate() const { return v8_isolate(); }
|
|
|
|
|
|
|
|
v8::Isolate* v8_isolate() const { return isolate_; }
|
2014-09-04 08:44:03 +00:00
|
|
|
|
2017-02-10 16:01:38 +00:00
|
|
|
v8::internal::Isolate* i_isolate() const {
|
|
|
|
return reinterpret_cast<v8::internal::Isolate*>(isolate());
|
|
|
|
}
|
|
|
|
|
2017-11-13 12:04:57 +00:00
|
|
|
Local<Value> RunJS(const char* source);
|
|
|
|
|
2014-09-04 08:44:03 +00:00
|
|
|
static void SetUpTestCase();
|
|
|
|
static void TearDownTestCase();
|
|
|
|
|
|
|
|
private:
|
2016-06-29 07:39:45 +00:00
|
|
|
static v8::ArrayBuffer::Allocator* array_buffer_allocator_;
|
2017-11-14 09:15:41 +00:00
|
|
|
static v8::Isolate* isolate_;
|
|
|
|
v8::Isolate::Scope isolate_scope_;
|
|
|
|
v8::HandleScope handle_scope_;
|
2014-09-04 08:44:03 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(TestWithIsolate);
|
|
|
|
};
|
|
|
|
|
2017-11-13 12:04:57 +00:00
|
|
|
// Use v8::internal::TestWithNativeContext if you are testing internals,
|
|
|
|
// aka. directly work with Handles.
|
2017-11-14 09:15:41 +00:00
|
|
|
class TestWithContext : public virtual v8::TestWithIsolate {
|
2014-09-04 08:44:03 +00:00
|
|
|
public:
|
|
|
|
TestWithContext();
|
2018-09-14 15:34:02 +00:00
|
|
|
~TestWithContext() override;
|
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-08-13 15:35:29 +00:00
|
|
|
v8::Local<v8::String> NewString(const char* string);
|
2017-11-14 09:15:41 +00:00
|
|
|
void SetGlobalProperty(const char* name, v8::Local<v8::Value> value);
|
2014-09-04 08:44:03 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Local<Context> context_;
|
2017-11-14 09:15:41 +00:00
|
|
|
v8::Context::Scope context_scope_;
|
2014-09-04 08:44:03 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(TestWithContext);
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
// Forward declarations.
|
|
|
|
class Factory;
|
|
|
|
|
|
|
|
|
|
|
|
class TestWithIsolate : public virtual ::v8::TestWithIsolate {
|
|
|
|
public:
|
2018-09-13 10:07:40 +00:00
|
|
|
TestWithIsolate() = default;
|
2018-09-14 15:34:02 +00:00
|
|
|
~TestWithIsolate() override;
|
2014-09-04 08:44:03 +00:00
|
|
|
|
|
|
|
Factory* factory() const;
|
2017-11-14 09:15:41 +00:00
|
|
|
Isolate* isolate() const { return i_isolate(); }
|
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-07-20 09:19:29 +00:00
|
|
|
Handle<Object> RunJSInternal(const char* source);
|
2014-12-02 04:48:57 +00:00
|
|
|
base::RandomNumberGenerator* random_number_generator() const;
|
2014-09-04 08:44:03 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(TestWithIsolate);
|
|
|
|
};
|
|
|
|
|
2015-01-23 16:29:50 +00:00
|
|
|
class TestWithZone : public virtual ::testing::Test {
|
2014-09-04 08:44:03 +00:00
|
|
|
public:
|
2016-10-17 12:12:30 +00:00
|
|
|
TestWithZone() : zone_(&allocator_, ZONE_NAME) {}
|
2018-09-14 15:34:02 +00:00
|
|
|
~TestWithZone() override;
|
2014-09-04 08:44:03 +00:00
|
|
|
|
|
|
|
Zone* zone() { return &zone_; }
|
|
|
|
|
|
|
|
private:
|
2016-09-20 16:07:25 +00:00
|
|
|
v8::internal::AccountingAllocator allocator_;
|
2014-09-04 08:44:03 +00:00
|
|
|
Zone zone_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(TestWithZone);
|
|
|
|
};
|
|
|
|
|
2015-01-23 16:29:50 +00:00
|
|
|
class TestWithIsolateAndZone : public virtual TestWithIsolate {
|
|
|
|
public:
|
2016-10-17 12:12:30 +00:00
|
|
|
TestWithIsolateAndZone() : zone_(&allocator_, ZONE_NAME) {}
|
2018-09-14 15:34:02 +00:00
|
|
|
~TestWithIsolateAndZone() override;
|
2015-01-23 16:29:50 +00:00
|
|
|
|
|
|
|
Zone* zone() { return &zone_; }
|
|
|
|
|
|
|
|
private:
|
2016-09-20 16:07:25 +00:00
|
|
|
v8::internal::AccountingAllocator allocator_;
|
2015-01-23 16:29:50 +00:00
|
|
|
Zone zone_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(TestWithIsolateAndZone);
|
|
|
|
};
|
|
|
|
|
2016-10-10 05:53:31 +00:00
|
|
|
class TestWithNativeContext : public virtual ::v8::TestWithContext,
|
|
|
|
public virtual TestWithIsolate {
|
|
|
|
public:
|
2018-09-13 10:07:40 +00:00
|
|
|
TestWithNativeContext() = default;
|
2018-09-14 15:34:02 +00:00
|
|
|
~TestWithNativeContext() override;
|
2016-10-10 05:53:31 +00:00
|
|
|
|
|
|
|
Handle<Context> native_context() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(TestWithNativeContext);
|
|
|
|
};
|
|
|
|
|
2017-03-14 13:33:13 +00:00
|
|
|
class SaveFlags {
|
|
|
|
public:
|
|
|
|
SaveFlags();
|
|
|
|
~SaveFlags();
|
|
|
|
|
|
|
|
private:
|
2017-08-30 07:24:59 +00:00
|
|
|
std::vector<const char*>* non_default_flags_;
|
2017-03-14 13:33:13 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(SaveFlags);
|
|
|
|
};
|
|
|
|
|
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_
|