2016-07-22 12:19:50 +00:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2016-08-05 13:18:42 +00:00
|
|
|
#include "include/v8.h"
|
|
|
|
#include "src/api.h"
|
2016-09-05 11:54:00 +00:00
|
|
|
#include "src/ast/ast.h"
|
2016-08-05 13:18:42 +00:00
|
|
|
#include "src/ast/scopes.h"
|
2016-09-06 13:34:31 +00:00
|
|
|
#include "src/base/platform/semaphore.h"
|
2016-07-22 12:19:50 +00:00
|
|
|
#include "src/compiler-dispatcher/compiler-dispatcher-job.h"
|
2016-12-12 15:35:41 +00:00
|
|
|
#include "src/compiler-dispatcher/compiler-dispatcher-tracer.h"
|
2017-08-18 20:34:14 +00:00
|
|
|
#include "src/compiler-dispatcher/unoptimized-compile-job.h"
|
2016-07-29 10:05:57 +00:00
|
|
|
#include "src/flags.h"
|
2016-07-22 12:19:50 +00:00
|
|
|
#include "src/isolate-inl.h"
|
2016-08-22 11:33:30 +00:00
|
|
|
#include "src/parsing/parse-info.h"
|
2016-09-06 13:34:31 +00:00
|
|
|
#include "src/v8.h"
|
2017-04-18 14:01:12 +00:00
|
|
|
#include "test/unittests/test-helpers.h"
|
2016-07-22 12:19:50 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
class UnoptimizedCompileJobTest : public TestWithContext {
|
2016-12-12 15:35:41 +00:00
|
|
|
public:
|
2017-07-27 13:33:53 +00:00
|
|
|
UnoptimizedCompileJobTest() : tracer_(i_isolate()) {}
|
|
|
|
~UnoptimizedCompileJobTest() override {}
|
2016-12-12 15:35:41 +00:00
|
|
|
|
|
|
|
CompilerDispatcherTracer* tracer() { return &tracer_; }
|
|
|
|
|
2016-09-06 13:34:31 +00:00
|
|
|
static void SetUpTestCase() {
|
2017-03-14 13:33:13 +00:00
|
|
|
CHECK_NULL(save_flags_);
|
|
|
|
save_flags_ = new SaveFlags();
|
2016-09-06 13:34:31 +00:00
|
|
|
TestWithContext::SetUpTestCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TearDownTestCase() {
|
|
|
|
TestWithContext::TearDownTestCase();
|
2017-03-14 13:33:13 +00:00
|
|
|
CHECK_NOT_NULL(save_flags_);
|
|
|
|
delete save_flags_;
|
|
|
|
save_flags_ = nullptr;
|
2016-09-06 13:34:31 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
static UnoptimizedCompileJob::Status GetStatus(UnoptimizedCompileJob* job) {
|
2017-07-04 08:55:45 +00:00
|
|
|
return job->status();
|
|
|
|
}
|
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
static UnoptimizedCompileJob::Status GetStatus(
|
|
|
|
const std::unique_ptr<UnoptimizedCompileJob>& job) {
|
2017-07-04 08:55:45 +00:00
|
|
|
return GetStatus(job.get());
|
|
|
|
}
|
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
static Variable* LookupVariableByName(UnoptimizedCompileJob* job,
|
2017-07-04 08:55:45 +00:00
|
|
|
const char* name) {
|
|
|
|
const AstRawString* name_raw_string =
|
|
|
|
job->parse_info_->ast_value_factory()->GetOneByteString(name);
|
|
|
|
return job->parse_info_->literal()->scope()->Lookup(name_raw_string);
|
|
|
|
}
|
|
|
|
|
2016-09-06 13:34:31 +00:00
|
|
|
private:
|
2017-01-16 10:49:16 +00:00
|
|
|
CompilerDispatcherTracer tracer_;
|
2017-03-14 13:33:13 +00:00
|
|
|
static SaveFlags* save_flags_;
|
2017-01-16 10:49:16 +00:00
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(UnoptimizedCompileJobTest);
|
2016-09-06 13:34:31 +00:00
|
|
|
};
|
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
SaveFlags* UnoptimizedCompileJobTest::save_flags_ = nullptr;
|
2016-09-06 13:34:31 +00:00
|
|
|
|
2017-07-04 08:55:45 +00:00
|
|
|
#define ASSERT_JOB_STATUS(STATUS, JOB) ASSERT_EQ(STATUS, GetStatus(JOB))
|
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
TEST_F(UnoptimizedCompileJobTest, Construct) {
|
|
|
|
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
|
2017-04-06 13:59:07 +00:00
|
|
|
i_isolate(), tracer(),
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), nullptr), FLAG_stack_size));
|
2016-07-29 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
TEST_F(UnoptimizedCompileJobTest, StateTransitions) {
|
|
|
|
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
|
2017-04-06 13:59:07 +00:00
|
|
|
i_isolate(), tracer(),
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), nullptr), FLAG_stack_size));
|
2016-07-28 07:46:15 +00:00
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kReadyToParse, job);
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kParsed, job);
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kReadyToAnalyze, job);
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kAnalyzed, job);
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kReadyToCompile, job);
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kCompiled, job);
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kDone, job);
|
2017-07-28 10:10:25 +00:00
|
|
|
job->ResetOnMainThread(i_isolate());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
|
2016-08-01 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
TEST_F(UnoptimizedCompileJobTest, SyntaxError) {
|
2017-04-06 13:59:07 +00:00
|
|
|
test::ScriptResource script("^^^", strlen("^^^"));
|
2017-07-27 13:33:53 +00:00
|
|
|
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
|
2017-04-06 13:59:07 +00:00
|
|
|
i_isolate(), tracer(),
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), &script), FLAG_stack_size));
|
2016-08-01 11:27:58 +00:00
|
|
|
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_TRUE(job->IsFailed());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kFailed, job);
|
2016-08-01 11:27:58 +00:00
|
|
|
ASSERT_TRUE(i_isolate()->has_pending_exception());
|
2016-08-23 12:10:39 +00:00
|
|
|
|
2016-08-05 13:18:42 +00:00
|
|
|
i_isolate()->clear_pending_exception();
|
2016-08-23 12:10:39 +00:00
|
|
|
|
2017-07-28 10:10:25 +00:00
|
|
|
job->ResetOnMainThread(i_isolate());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
|
2016-08-05 13:18:42 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
TEST_F(UnoptimizedCompileJobTest, ScopeChain) {
|
2016-08-05 13:18:42 +00:00
|
|
|
const char script[] =
|
2016-09-20 12:07:52 +00:00
|
|
|
"function g() { var y = 1; function f(x) { return x * y }; return f; } "
|
2016-08-05 13:18:42 +00:00
|
|
|
"g();";
|
2017-04-06 13:59:07 +00:00
|
|
|
Handle<JSFunction> f =
|
|
|
|
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
|
2016-08-05 13:18:42 +00:00
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
|
2016-12-12 15:35:41 +00:00
|
|
|
i_isolate(), tracer(), handle(f->shared()), FLAG_stack_size));
|
2016-08-05 13:18:42 +00:00
|
|
|
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kReadyToCompile, job);
|
2017-07-04 08:55:45 +00:00
|
|
|
|
|
|
|
Variable* var = LookupVariableByName(job.get(), "x");
|
2016-08-05 13:18:42 +00:00
|
|
|
ASSERT_TRUE(var);
|
2016-09-15 16:41:03 +00:00
|
|
|
ASSERT_TRUE(var->IsParameter());
|
2016-08-05 13:18:42 +00:00
|
|
|
|
2017-07-04 08:55:45 +00:00
|
|
|
var = LookupVariableByName(job.get(), "y");
|
2016-08-05 13:18:42 +00:00
|
|
|
ASSERT_TRUE(var);
|
|
|
|
ASSERT_TRUE(var->IsContextSlot());
|
|
|
|
|
2017-07-28 10:10:25 +00:00
|
|
|
job->ResetOnMainThread(i_isolate());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
|
2016-07-28 07:46:15 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
TEST_F(UnoptimizedCompileJobTest, CompileAndRun) {
|
2016-08-25 10:24:53 +00:00
|
|
|
const char script[] =
|
|
|
|
"function g() {\n"
|
|
|
|
" f = function(a) {\n"
|
|
|
|
" for (var i = 0; i < 3; i++) { a += 20; }\n"
|
|
|
|
" return a;\n"
|
|
|
|
" }\n"
|
|
|
|
" return f;\n"
|
|
|
|
"}\n"
|
|
|
|
"g();";
|
2017-04-06 13:59:07 +00:00
|
|
|
Handle<JSFunction> f =
|
|
|
|
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
|
2017-07-27 13:33:53 +00:00
|
|
|
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
|
2016-12-12 15:35:41 +00:00
|
|
|
i_isolate(), tracer(), handle(f->shared()), FLAG_stack_size));
|
2016-08-25 10:24:53 +00:00
|
|
|
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kDone, job);
|
2016-08-25 10:24:53 +00:00
|
|
|
|
2017-04-06 13:59:07 +00:00
|
|
|
Smi* value = Smi::cast(*test::RunJS(isolate(), "f(100);"));
|
2016-08-25 10:24:53 +00:00
|
|
|
ASSERT_TRUE(value == Smi::FromInt(160));
|
|
|
|
|
2017-07-28 10:10:25 +00:00
|
|
|
job->ResetOnMainThread(i_isolate());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
|
2016-08-25 10:24:53 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
TEST_F(UnoptimizedCompileJobTest, CompileFailureToAnalyse) {
|
2016-08-25 10:24:53 +00:00
|
|
|
std::string raw_script("() { var a = ");
|
|
|
|
for (int i = 0; i < 100000; i++) {
|
|
|
|
raw_script += "'x' + ";
|
|
|
|
}
|
|
|
|
raw_script += " 'x'; }";
|
2017-04-06 13:59:07 +00:00
|
|
|
test::ScriptResource script(raw_script.c_str(), strlen(raw_script.c_str()));
|
2017-07-27 13:33:53 +00:00
|
|
|
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
|
2017-04-06 13:59:07 +00:00
|
|
|
i_isolate(), tracer(),
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), &script), 100));
|
2016-08-25 10:24:53 +00:00
|
|
|
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_TRUE(job->IsFailed());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kFailed, job);
|
2016-08-25 10:24:53 +00:00
|
|
|
ASSERT_TRUE(i_isolate()->has_pending_exception());
|
|
|
|
|
|
|
|
i_isolate()->clear_pending_exception();
|
2017-07-28 10:10:25 +00:00
|
|
|
job->ResetOnMainThread(i_isolate());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
|
2016-08-25 10:24:53 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
TEST_F(UnoptimizedCompileJobTest, CompileFailureToFinalize) {
|
2016-08-25 10:24:53 +00:00
|
|
|
std::string raw_script("() { var a = ");
|
2016-08-25 13:57:23 +00:00
|
|
|
for (int i = 0; i < 1000; i++) {
|
2016-08-25 10:24:53 +00:00
|
|
|
raw_script += "'x' + ";
|
|
|
|
}
|
|
|
|
raw_script += " 'x'; }";
|
2017-04-06 13:59:07 +00:00
|
|
|
test::ScriptResource script(raw_script.c_str(), strlen(raw_script.c_str()));
|
2017-07-27 13:33:53 +00:00
|
|
|
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
|
2017-04-06 13:59:07 +00:00
|
|
|
i_isolate(), tracer(),
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), &script), 50));
|
2016-08-25 10:24:53 +00:00
|
|
|
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_TRUE(job->IsFailed());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kFailed, job);
|
2016-08-25 10:24:53 +00:00
|
|
|
ASSERT_TRUE(i_isolate()->has_pending_exception());
|
|
|
|
|
|
|
|
i_isolate()->clear_pending_exception();
|
2017-07-28 10:10:25 +00:00
|
|
|
job->ResetOnMainThread(i_isolate());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
|
2016-08-25 10:24:53 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 13:34:31 +00:00
|
|
|
class CompileTask : public Task {
|
|
|
|
public:
|
2017-07-27 13:33:53 +00:00
|
|
|
CompileTask(UnoptimizedCompileJob* job, base::Semaphore* semaphore)
|
2016-09-06 13:34:31 +00:00
|
|
|
: job_(job), semaphore_(semaphore) {}
|
|
|
|
~CompileTask() override {}
|
|
|
|
|
|
|
|
void Run() override {
|
2017-07-04 08:55:45 +00:00
|
|
|
job_->StepNextOnBackgroundThread();
|
|
|
|
ASSERT_FALSE(job_->IsFailed());
|
2016-09-06 13:34:31 +00:00
|
|
|
semaphore_->Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-07-27 13:33:53 +00:00
|
|
|
UnoptimizedCompileJob* job_;
|
2016-09-06 13:34:31 +00:00
|
|
|
base::Semaphore* semaphore_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CompileTask);
|
|
|
|
};
|
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
TEST_F(UnoptimizedCompileJobTest, CompileOnBackgroundThread) {
|
2016-09-06 13:34:31 +00:00
|
|
|
const char* raw_script =
|
|
|
|
"(a, b) {\n"
|
|
|
|
" var c = a + b;\n"
|
|
|
|
" function bar() { return b }\n"
|
|
|
|
" var d = { foo: 100, bar : bar() }\n"
|
|
|
|
" return bar;"
|
|
|
|
"}";
|
2017-04-06 13:59:07 +00:00
|
|
|
test::ScriptResource script(raw_script, strlen(raw_script));
|
2017-07-27 13:33:53 +00:00
|
|
|
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
|
2017-04-06 13:59:07 +00:00
|
|
|
i_isolate(), tracer(),
|
|
|
|
test::CreateSharedFunctionInfo(i_isolate(), &script), 100));
|
2016-09-06 13:34:31 +00:00
|
|
|
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2016-09-06 13:34:31 +00:00
|
|
|
|
|
|
|
base::Semaphore semaphore(0);
|
|
|
|
CompileTask* background_task = new CompileTask(job.get(), &semaphore);
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kReadyToCompile, job);
|
2016-09-06 13:34:31 +00:00
|
|
|
V8::GetCurrentPlatform()->CallOnBackgroundThread(background_task,
|
|
|
|
Platform::kShortRunningTask);
|
|
|
|
semaphore.Wait();
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kDone, job);
|
2016-09-06 13:34:31 +00:00
|
|
|
|
2017-07-28 10:10:25 +00:00
|
|
|
job->ResetOnMainThread(i_isolate());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
|
2016-09-06 13:34:31 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
TEST_F(UnoptimizedCompileJobTest, LazyInnerFunctions) {
|
2016-12-16 12:38:17 +00:00
|
|
|
const char script[] =
|
|
|
|
"function g() {\n"
|
|
|
|
" f = function() {\n"
|
|
|
|
" e = (function() { return 42; });\n"
|
|
|
|
" return e;\n"
|
|
|
|
" };\n"
|
|
|
|
" return f;\n"
|
|
|
|
"}\n"
|
|
|
|
"g();";
|
2017-04-06 13:59:07 +00:00
|
|
|
Handle<JSFunction> f =
|
|
|
|
Handle<JSFunction>::cast(test::RunJS(isolate(), script));
|
2016-12-16 12:38:17 +00:00
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
|
2016-12-16 12:38:17 +00:00
|
|
|
i_isolate(), tracer(), handle(f->shared()), FLAG_stack_size));
|
|
|
|
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-28 10:10:25 +00:00
|
|
|
job->StepNextOnMainThread(i_isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kDone, job);
|
2016-12-16 12:38:17 +00:00
|
|
|
|
2017-04-06 13:59:07 +00:00
|
|
|
Handle<JSFunction> e =
|
|
|
|
Handle<JSFunction>::cast(test::RunJS(isolate(), "f();"));
|
2016-12-16 12:38:17 +00:00
|
|
|
|
2017-09-11 14:23:35 +00:00
|
|
|
ASSERT_FALSE(e->shared()->is_compiled());
|
2016-12-16 12:38:17 +00:00
|
|
|
|
2017-07-28 10:10:25 +00:00
|
|
|
job->ResetOnMainThread(i_isolate());
|
2017-07-27 13:33:53 +00:00
|
|
|
ASSERT_JOB_STATUS(UnoptimizedCompileJob::Status::kInitial, job);
|
2016-12-16 12:38:17 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 12:19:50 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|