Commit b4c9e2e7 authored by Camillo Bruni's avatar Camillo Bruni Committed by Commit Bot

[unittests] Add TestWithIsolate::RunJS helper method

- Update most callsites to use the new RunJS method
- Update tests to use TestWithNativeContext if possible
- Remove RunJS from test-helpers.cc
- Remove TestWithRandomNumberGenerator from test-utils.h

Change-Id: Ib2a6cc56334dc391ca6a2aeb7780fa324f44f109
Reviewed-on: https://chromium-review.googlesource.com/765373Reviewed-by: 's avatarGeorg Neis <neis@chromium.org>
Commit-Queue: Camillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#49325}
parent 214f920a
......@@ -31,9 +31,28 @@ TEST(FunctionalTest, HashDoubleZero) {
EXPECT_EQ(h(0.0), h(-0.0));
}
namespace {
inline int64_t GetRandomSeedFromFlag(int random_seed) {
return random_seed ? random_seed : TimeTicks::Now().ToInternalValue();
}
} // namespace
template <typename T>
class FunctionalTest : public TestWithRandomNumberGenerator {};
class FunctionalTest : public ::testing::Test {
public:
FunctionalTest()
: rng_(GetRandomSeedFromFlag(::v8::internal::FLAG_random_seed)) {}
virtual ~FunctionalTest() {}
RandomNumberGenerator* rng() { return &rng_; }
private:
RandomNumberGenerator rng_;
DISALLOW_COPY_AND_ASSIGN(FunctionalTest);
};
typedef ::testing::Types<signed char, unsigned char,
short, // NOLINT(runtime/int)
......
......@@ -62,18 +62,18 @@ class CompilerDispatcherTestFlags {
SaveFlags* CompilerDispatcherTestFlags::save_flags_ = nullptr;
class CompilerDispatcherTest : public TestWithContext {
class CompilerDispatcherTest : public TestWithNativeContext {
public:
CompilerDispatcherTest() = default;
~CompilerDispatcherTest() override = default;
static void SetUpTestCase() {
CompilerDispatcherTestFlags::SetFlagsForTest();
TestWithContext::SetUpTestCase();
TestWithNativeContext ::SetUpTestCase();
}
static void TearDownTestCase() {
TestWithContext::TearDownTestCase();
TestWithNativeContext ::TearDownTestCase();
CompilerDispatcherTestFlags::RestoreFlags();
}
......@@ -92,25 +92,6 @@ class CompilerDispatcherTest : public TestWithContext {
DISALLOW_COPY_AND_ASSIGN(CompilerDispatcherTest);
};
class CompilerDispatcherTestWithoutContext : public v8::TestWithIsolate {
public:
CompilerDispatcherTestWithoutContext() = default;
~CompilerDispatcherTestWithoutContext() override = default;
static void SetUpTestCase() {
CompilerDispatcherTestFlags::SetFlagsForTest();
TestWithContext::SetUpTestCase();
}
static void TearDownTestCase() {
TestWithContext::TearDownTestCase();
CompilerDispatcherTestFlags::RestoreFlags();
}
private:
DISALLOW_COPY_AND_ASSIGN(CompilerDispatcherTestWithoutContext);
};
namespace {
class MockPlatform : public v8::Platform {
......@@ -311,8 +292,8 @@ TEST_F(CompilerDispatcherTest, IsEnqueued) {
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script[] = TEST_SCRIPT();
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
ASSERT_FALSE(dispatcher.IsEnqueued(shared));
......@@ -329,8 +310,7 @@ TEST_F(CompilerDispatcherTest, FinishNow) {
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script[] = TEST_SCRIPT();
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
ASSERT_FALSE(shared->is_compiled());
......@@ -358,7 +338,7 @@ TEST_F(CompilerDispatcherTest, FinishAllNow) {
std::string script("function g() { function " + func_name +
"(x) { var a = 'x'; }; return " + func_name +
"; } g();");
f[i] = Handle<JSFunction>::cast(test::RunJS(isolate(), script.c_str()));
f[i] = RunJS<JSFunction>(script.c_str());
shared[i] = Handle<SharedFunctionInfo>(f[i]->shared(), i_isolate());
ASSERT_FALSE(shared[i]->is_compiled());
ASSERT_TRUE(dispatcher.Enqueue(shared[i]));
......@@ -378,8 +358,7 @@ TEST_F(CompilerDispatcherTest, IdleTask) {
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script[] = TEST_SCRIPT();
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
ASSERT_FALSE(platform.IdleTaskPending());
......@@ -399,8 +378,7 @@ TEST_F(CompilerDispatcherTest, IdleTaskSmallIdleTime) {
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script[] = TEST_SCRIPT();
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
ASSERT_FALSE(platform.IdleTaskPending());
......@@ -444,8 +422,7 @@ TEST_F(CompilerDispatcherTest, IdleTaskException) {
script += "'x' + 'x' - ";
}
script += " 'x'; }; return " + func_name + "; } g();";
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script.c_str()));
Handle<JSFunction> f = RunJS<JSFunction>(script.c_str());
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
ASSERT_FALSE(platform.IdleTaskPending());
......@@ -466,8 +443,7 @@ TEST_F(CompilerDispatcherTest, CompileOnBackgroundThread) {
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script[] = TEST_SCRIPT();
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
ASSERT_FALSE(platform.IdleTaskPending());
......@@ -510,8 +486,7 @@ TEST_F(CompilerDispatcherTest, FinishNowWithBackgroundTask) {
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script[] = TEST_SCRIPT();
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
ASSERT_FALSE(platform.IdleTaskPending());
......@@ -550,13 +525,11 @@ TEST_F(CompilerDispatcherTest, IdleTaskMultipleJobs) {
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script1[] = TEST_SCRIPT();
Handle<JSFunction> f1 =
Handle<JSFunction>::cast(test::RunJS(isolate(), script1));
Handle<JSFunction> f1 = RunJS<JSFunction>(script1);
Handle<SharedFunctionInfo> shared1(f1->shared(), i_isolate());
const char script2[] = TEST_SCRIPT();
Handle<JSFunction> f2 =
Handle<JSFunction>::cast(test::RunJS(isolate(), script2));
Handle<JSFunction> f2 = RunJS<JSFunction>(script2);
Handle<SharedFunctionInfo> shared2(f2->shared(), i_isolate());
ASSERT_FALSE(platform.IdleTaskPending());
......@@ -585,8 +558,7 @@ TEST_F(CompilerDispatcherTest, FinishNowException) {
script += "'x' + 'x' - ";
}
script += " 'x'; }; return " + func_name + "; } g();";
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script.c_str()));
Handle<JSFunction> f = RunJS<JSFunction>(script.c_str());
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
ASSERT_FALSE(platform.IdleTaskPending());
......@@ -608,8 +580,7 @@ TEST_F(CompilerDispatcherTest, AsyncAbortAllPendingBackgroundTask) {
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script[] = TEST_SCRIPT();
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
ASSERT_FALSE(platform.IdleTaskPending());
......@@ -651,13 +622,11 @@ TEST_F(CompilerDispatcherTest, AsyncAbortAllRunningBackgroundTask) {
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script1[] = TEST_SCRIPT();
Handle<JSFunction> f1 =
Handle<JSFunction>::cast(test::RunJS(isolate(), script1));
Handle<JSFunction> f1 = RunJS<JSFunction>(script1);
Handle<SharedFunctionInfo> shared1(f1->shared(), i_isolate());
const char script2[] = TEST_SCRIPT();
Handle<JSFunction> f2 =
Handle<JSFunction>::cast(test::RunJS(isolate(), script2));
Handle<JSFunction> f2 = RunJS<JSFunction>(script2);
Handle<SharedFunctionInfo> shared2(f2->shared(), i_isolate());
ASSERT_FALSE(platform.IdleTaskPending());
......@@ -733,8 +702,7 @@ TEST_F(CompilerDispatcherTest, FinishNowDuringAbortAll) {
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script[] = TEST_SCRIPT();
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
ASSERT_FALSE(platform.IdleTaskPending());
......@@ -812,8 +780,7 @@ TEST_F(CompilerDispatcherTest, MemoryPressure) {
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script[] = TEST_SCRIPT();
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
// Can't enqueue tasks under memory pressure.
......@@ -860,8 +827,7 @@ TEST_F(CompilerDispatcherTest, MemoryPressureFromBackground) {
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script[] = TEST_SCRIPT();
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
ASSERT_TRUE(dispatcher.Enqueue(shared));
......@@ -892,8 +858,7 @@ TEST_F(CompilerDispatcherTest, EnqueueJob) {
MockPlatform platform;
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script[] = TEST_SCRIPT();
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
std::unique_ptr<CompilerDispatcherJob> job(
new UnoptimizedCompileJob(i_isolate(), dispatcher.tracer_.get(), shared,
......@@ -912,8 +877,7 @@ TEST_F(CompilerDispatcherTest, EnqueueAndStep) {
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script[] = TEST_SCRIPT();
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
ASSERT_FALSE(dispatcher.IsEnqueued(shared));
......@@ -934,9 +898,8 @@ TEST_F(CompilerDispatcherTest, CompileLazyFinishesDispatcherJob) {
// enqueued functions.
CompilerDispatcher* dispatcher = i_isolate()->compiler_dispatcher();
const char source[] = TEST_SCRIPT();
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), source));
const char script[] = TEST_SCRIPT();
Handle<JSFunction> f = RunJS<JSFunction>(script);
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
ASSERT_FALSE(shared->is_compiled());
......@@ -946,7 +909,7 @@ TEST_F(CompilerDispatcherTest, CompileLazyFinishesDispatcherJob) {
// Now force the function to run and ensure CompileLazy finished and dequeues
// it from the dispatcher.
test::RunJS(isolate(), "g()();");
RunJS("g()();");
ASSERT_TRUE(shared->is_compiled());
ASSERT_FALSE(dispatcher->IsEnqueued(shared));
}
......@@ -957,21 +920,19 @@ TEST_F(CompilerDispatcherTest, CompileLazy2FinishesDispatcherJob) {
CompilerDispatcher* dispatcher = i_isolate()->compiler_dispatcher();
const char source2[] = "function lazy2() { return 42; }; lazy2;";
Handle<JSFunction> lazy2 =
Handle<JSFunction>::cast(test::RunJS(isolate(), source2));
Handle<JSFunction> lazy2 = RunJS<JSFunction>(source2);
Handle<SharedFunctionInfo> shared2(lazy2->shared(), i_isolate());
ASSERT_FALSE(shared2->is_compiled());
const char source1[] = "function lazy1() { return lazy2(); }; lazy1;";
Handle<JSFunction> lazy1 =
Handle<JSFunction>::cast(test::RunJS(isolate(), source1));
Handle<JSFunction> lazy1 = RunJS<JSFunction>(source1);
Handle<SharedFunctionInfo> shared1(lazy1->shared(), i_isolate());
ASSERT_FALSE(shared1->is_compiled());
ASSERT_TRUE(dispatcher->Enqueue(shared1));
ASSERT_TRUE(dispatcher->Enqueue(shared2));
test::RunJS(isolate(), "lazy1();");
RunJS("lazy1();");
ASSERT_TRUE(shared1->is_compiled());
ASSERT_TRUE(shared2->is_compiled());
ASSERT_FALSE(dispatcher->IsEnqueued(shared1));
......@@ -983,8 +944,7 @@ TEST_F(CompilerDispatcherTest, EnqueueAndStepTwice) {
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script[] = TEST_SCRIPT();
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
Handle<SharedFunctionInfo> shared(f->shared(), i_isolate());
ASSERT_FALSE(dispatcher.IsEnqueued(shared));
......@@ -1009,12 +969,10 @@ TEST_F(CompilerDispatcherTest, CompileMultipleOnBackgroundThread) {
CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size);
const char script1[] = TEST_SCRIPT();
Handle<JSFunction> f1 =
Handle<JSFunction>::cast(test::RunJS(isolate(), script1));
Handle<JSFunction> f1 = RunJS<JSFunction>(script1);
Handle<SharedFunctionInfo> shared1(f1->shared(), i_isolate());
const char script2[] = TEST_SCRIPT();
Handle<JSFunction> f2 =
Handle<JSFunction>::cast(test::RunJS(isolate(), script2));
Handle<JSFunction> f2 = RunJS<JSFunction>(script2);
Handle<SharedFunctionInfo> shared2(f2->shared(), i_isolate());
ASSERT_FALSE(platform.IdleTaskPending());
......
......@@ -19,7 +19,7 @@
namespace v8 {
namespace internal {
typedef TestWithContext OptimizingCompileDispatcherTest;
typedef TestWithNativeContext OptimizingCompileDispatcherTest;
namespace {
......@@ -72,8 +72,8 @@ TEST_F(OptimizingCompileDispatcherTest, Construct) {
}
TEST_F(OptimizingCompileDispatcherTest, NonBlockingFlush) {
Handle<JSFunction> fun = Handle<JSFunction>::cast(test::RunJS(
isolate(), "function f() { function g() {}; return g;}; f();"));
Handle<JSFunction> fun =
RunJS<JSFunction>("function f() { function g() {}; return g;}; f();");
BlockingCompilationJob* job = new BlockingCompilationJob(i_isolate(), fun);
OptimizingCompileDispatcher dispatcher(i_isolate());
......
......@@ -23,9 +23,9 @@
namespace v8 {
namespace internal {
class UnoptimizedCompileJobTest : public TestWithContext {
class UnoptimizedCompileJobTest : public TestWithNativeContext {
public:
UnoptimizedCompileJobTest() : tracer_(i_isolate()) {}
UnoptimizedCompileJobTest() : tracer_(isolate()) {}
~UnoptimizedCompileJobTest() override {}
CompilerDispatcherTracer* tracer() { return &tracer_; }
......@@ -33,11 +33,11 @@ class UnoptimizedCompileJobTest : public TestWithContext {
static void SetUpTestCase() {
CHECK_NULL(save_flags_);
save_flags_ = new SaveFlags();
TestWithContext::SetUpTestCase();
TestWithNativeContext ::SetUpTestCase();
}
static void TearDownTestCase() {
TestWithContext::TearDownTestCase();
TestWithNativeContext ::TearDownTestCase();
CHECK_NOT_NULL(save_flags_);
delete save_flags_;
save_flags_ = nullptr;
......@@ -72,59 +72,59 @@ SaveFlags* UnoptimizedCompileJobTest::save_flags_ = nullptr;
TEST_F(UnoptimizedCompileJobTest, Construct) {
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
i_isolate(), tracer(),
test::CreateSharedFunctionInfo(i_isolate(), nullptr), FLAG_stack_size));
isolate(), tracer(), test::CreateSharedFunctionInfo(isolate(), nullptr),
FLAG_stack_size));
}
TEST_F(UnoptimizedCompileJobTest, StateTransitions) {
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
i_isolate(), tracer(),
test::CreateSharedFunctionInfo(i_isolate(), nullptr), FLAG_stack_size));
isolate(), tracer(), test::CreateSharedFunctionInfo(isolate(), nullptr),
FLAG_stack_size));
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kReadyToParse, job);
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kParsed, job);
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kReadyToAnalyze, job);
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kAnalyzed, job);
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kReadyToCompile, job);
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kCompiled, job);
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kDone, job);
job->ResetOnMainThread(i_isolate());
job->ResetOnMainThread(isolate());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
}
TEST_F(UnoptimizedCompileJobTest, SyntaxError) {
test::ScriptResource script("^^^", strlen("^^^"));
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
i_isolate(), tracer(),
test::CreateSharedFunctionInfo(i_isolate(), &script), FLAG_stack_size));
isolate(), tracer(), test::CreateSharedFunctionInfo(isolate(), &script),
FLAG_stack_size));
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_TRUE(job->IsFailed());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kFailed, job);
ASSERT_TRUE(i_isolate()->has_pending_exception());
ASSERT_TRUE(isolate()->has_pending_exception());
i_isolate()->clear_pending_exception();
isolate()->clear_pending_exception();
job->ResetOnMainThread(i_isolate());
job->ResetOnMainThread(isolate());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
}
......@@ -132,21 +132,20 @@ TEST_F(UnoptimizedCompileJobTest, ScopeChain) {
const char script[] =
"function g() { var y = 1; function f(x) { return x * y }; return f; } "
"g();";
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
i_isolate(), tracer(), handle(f->shared()), FLAG_stack_size));
isolate(), tracer(), handle(f->shared()), FLAG_stack_size));
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kReadyToCompile, job);
......@@ -158,7 +157,7 @@ TEST_F(UnoptimizedCompileJobTest, ScopeChain) {
ASSERT_TRUE(var);
ASSERT_TRUE(var->IsContextSlot());
job->ResetOnMainThread(i_isolate());
job->ResetOnMainThread(isolate());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
}
......@@ -172,31 +171,30 @@ TEST_F(UnoptimizedCompileJobTest, CompileAndRun) {
" return f;\n"
"}\n"
"g();";
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
i_isolate(), tracer(), handle(f->shared()), FLAG_stack_size));
isolate(), tracer(), handle(f->shared()), FLAG_stack_size));
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kDone, job);
Smi* value = Smi::cast(*test::RunJS(isolate(), "f(100);"));
Smi* value = Smi::cast(*RunJS("f(100);"));
ASSERT_TRUE(value == Smi::FromInt(160));
job->ResetOnMainThread(i_isolate());
job->ResetOnMainThread(isolate());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
}
......@@ -212,22 +210,22 @@ TEST_F(UnoptimizedCompileJobTest, CompileFailureToAnalyse) {
raw_script += " 'x'; }";
test::ScriptResource script(raw_script.c_str(), strlen(raw_script.c_str()));
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
i_isolate(), tracer(),
test::CreateSharedFunctionInfo(i_isolate(), &script), 100));
isolate(), tracer(), test::CreateSharedFunctionInfo(isolate(), &script),
100));
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_TRUE(job->IsFailed());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kFailed, job);
ASSERT_TRUE(i_isolate()->has_pending_exception());
ASSERT_TRUE(isolate()->has_pending_exception());
i_isolate()->clear_pending_exception();
job->ResetOnMainThread(i_isolate());
isolate()->clear_pending_exception();
job->ResetOnMainThread(isolate());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
}
......@@ -240,28 +238,28 @@ TEST_F(UnoptimizedCompileJobTest, CompileFailureToFinalize) {
raw_script += " 'x'; }";
test::ScriptResource script(raw_script.c_str(), strlen(raw_script.c_str()));
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
i_isolate(), tracer(),
test::CreateSharedFunctionInfo(i_isolate(), &script), 50));
isolate(), tracer(), test::CreateSharedFunctionInfo(isolate(), &script),
50));
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_TRUE(job->IsFailed());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kFailed, job);
ASSERT_TRUE(i_isolate()->has_pending_exception());
ASSERT_TRUE(isolate()->has_pending_exception());
i_isolate()->clear_pending_exception();
job->ResetOnMainThread(i_isolate());
isolate()->clear_pending_exception();
job->ResetOnMainThread(isolate());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
}
......@@ -293,18 +291,18 @@ TEST_F(UnoptimizedCompileJobTest, CompileOnBackgroundThread) {
"}";
test::ScriptResource script(raw_script, strlen(raw_script));
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
i_isolate(), tracer(),
test::CreateSharedFunctionInfo(i_isolate(), &script), 100));
isolate(), tracer(), test::CreateSharedFunctionInfo(isolate(), &script),
100));
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
base::Semaphore semaphore(0);
......@@ -313,11 +311,11 @@ TEST_F(UnoptimizedCompileJobTest, CompileOnBackgroundThread) {
V8::GetCurrentPlatform()->CallOnBackgroundThread(background_task,
Platform::kShortRunningTask);
semaphore.Wait();
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kDone, job);
job->ResetOnMainThread(i_isolate());
job->ResetOnMainThread(isolate());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
}
......@@ -331,34 +329,32 @@ TEST_F(UnoptimizedCompileJobTest, LazyInnerFunctions) {
" return f;\n"
"}\n"
"g();";
Handle<JSFunction> f =
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
Handle<JSFunction> f = RunJS<JSFunction>(script);
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
i_isolate(), tracer(), handle(f->shared()), FLAG_stack_size));
isolate(), tracer(), handle(f->shared()), FLAG_stack_size));
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
job->StepNextOnMainThread(i_isolate());
job->StepNextOnMainThread(isolate());
ASSERT_FALSE(job->IsFailed());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kDone, job);
Handle<JSFunction> e =
Handle<JSFunction>::cast(test::RunJS(isolate(), "f();"));
Handle<JSFunction> e = RunJS<JSFunction>("f();");
ASSERT_FALSE(e->shared()->is_compiled());
job->ResetOnMainThread(i_isolate());
job->ResetOnMainThread(isolate());
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
}
......
......@@ -11,7 +11,7 @@
namespace v8 {
namespace internal {
class PreParserTest : public TestWithContext {
class PreParserTest : public TestWithNativeContext {
public:
PreParserTest() {}
......@@ -22,16 +22,12 @@ class PreParserTest : public TestWithContext {
TEST_F(PreParserTest, LazyFunctionLength) {
const char* script_source = "function lazy(a, b, c) { } lazy";
Handle<Object> lazy_object = test::RunJS(isolate(), script_source);
Handle<JSFunction> lazy_function = RunJS<JSFunction>(script_source);
Handle<SharedFunctionInfo> shared(
Handle<JSFunction>::cast(lazy_object)->shared(), i_isolate());
Handle<SharedFunctionInfo> shared(lazy_function->shared());
CHECK_EQ(shared->length(), SharedFunctionInfo::kInvalidLength);
const char* get_length_source = "lazy.length";
Handle<Object> length = test::RunJS(isolate(), get_length_source);
CHECK(length->IsSmi());
Handle<Smi> length = RunJS<Smi>("lazy.length");
int32_t value;
CHECK(length->ToInt32(&value));
CHECK_EQ(3, value);
......
......@@ -15,17 +15,6 @@ namespace v8 {
namespace internal {
namespace test {
Handle<Object> RunJS(v8::Isolate* isolate, const char* script) {
return Utils::OpenHandle(
*v8::Script::Compile(
isolate->GetCurrentContext(),
v8::String::NewFromUtf8(isolate, script, v8::NewStringType::kNormal)
.ToLocalChecked())
.ToLocalChecked()
->Run(isolate->GetCurrentContext())
.ToLocalChecked());
}
Handle<String> CreateSource(Isolate* isolate,
ExternalOneByteString::Resource* maybe_resource) {
static const char test_script[] = "(x) { x*x; }";
......
......@@ -40,7 +40,6 @@ class ScriptResource : public v8::String::ExternalOneByteStringResource {
DISALLOW_COPY_AND_ASSIGN(ScriptResource);
};
Handle<Object> RunJS(v8::Isolate* isolate, const char* script);
Handle<String> CreateSource(
Isolate* isolate,
v8::String::ExternalOneByteStringResource* maybe_resource);
......
......@@ -5,6 +5,8 @@
#include "test/unittests/test-utils.h"
#include "include/libplatform/libplatform.h"
#include "include/v8.h"
#include "src/api.h"
#include "src/base/platform/time.h"
#include "src/flags.h"
#include "src/isolate.h"
......@@ -50,6 +52,15 @@ void TestWithIsolate::TearDownTestCase() {
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();
}
TestWithContext::TestWithContext()
: context_(Context::New(isolate())), context_scope_(context_) {}
......@@ -57,25 +68,6 @@ TestWithContext::TestWithContext()
TestWithContext::~TestWithContext() {}
namespace base {
namespace {
inline int64_t GetRandomSeedFromFlag(int random_seed) {
return random_seed ? random_seed : TimeTicks::Now().ToInternalValue();
}
} // namespace
TestWithRandomNumberGenerator::TestWithRandomNumberGenerator()
: rng_(GetRandomSeedFromFlag(::v8::internal::FLAG_random_seed)) {}
TestWithRandomNumberGenerator::~TestWithRandomNumberGenerator() {}
} // namespace base
namespace internal {
TestWithIsolate::~TestWithIsolate() {}
......
......@@ -8,8 +8,12 @@
#include <vector>
#include "include/v8.h"
#include "src/api.h"
#include "src/base/macros.h"
#include "src/base/utils/random-number-generator.h"
#include "src/handles.h"
#include "src/objects-inl.h"
#include "src/objects.h"
#include "src/zone/accounting-allocator.h"
#include "src/zone/zone.h"
#include "testing/gtest-support.h"
......@@ -18,7 +22,8 @@ namespace v8 {
class ArrayBufferAllocator;
// Use v8::internal::TestWithIsolate if you are testing internals,
// aka. directly work with Handles.
class TestWithIsolate : public virtual ::testing::Test {
public:
TestWithIsolate();
......@@ -30,6 +35,8 @@ class TestWithIsolate : public virtual ::testing::Test {
return reinterpret_cast<v8::internal::Isolate*>(isolate());
}
Local<Value> RunJS(const char* source);
static void SetUpTestCase();
static void TearDownTestCase();
......@@ -42,7 +49,8 @@ class TestWithIsolate : public virtual ::testing::Test {
DISALLOW_COPY_AND_ASSIGN(TestWithIsolate);
};
// Use v8::internal::TestWithNativeContext if you are testing internals,
// aka. directly work with Handles.
class TestWithContext : public virtual TestWithIsolate {
public:
TestWithContext();
......@@ -57,25 +65,6 @@ class TestWithContext : public virtual TestWithIsolate {
DISALLOW_COPY_AND_ASSIGN(TestWithContext);
};
namespace base {
class TestWithRandomNumberGenerator : public ::testing::Test {
public:
TestWithRandomNumberGenerator();
virtual ~TestWithRandomNumberGenerator();
RandomNumberGenerator* rng() { return &rng_; }
private:
RandomNumberGenerator rng_;
DISALLOW_COPY_AND_ASSIGN(TestWithRandomNumberGenerator);
};
} // namespace base
namespace internal {
// Forward declarations.
......@@ -91,6 +80,12 @@ class TestWithIsolate : public virtual ::v8::TestWithIsolate {
Isolate* isolate() const {
return reinterpret_cast<Isolate*>(::v8::TestWithIsolate::isolate());
}
template <typename T = Object>
Handle<T> RunJS(const char* source) {
Handle<Object> result =
Utils::OpenHandle(*::v8::TestWithIsolate::RunJS(source));
return Handle<T>::cast(result);
}
base::RandomNumberGenerator* random_number_generator() const;
private:
......
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