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-11-13 12:04:57 +00:00
|
|
|
class UnoptimizedCompileJobTest : public TestWithNativeContext {
|
2016-12-12 15:35:41 +00:00
|
|
|
public:
|
2017-11-13 12:04:57 +00:00
|
|
|
UnoptimizedCompileJobTest() : tracer_(isolate()) {}
|
2017-07-27 13:33:53 +00:00
|
|
|
~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();
|
2017-11-13 12:04:57 +00:00
|
|
|
TestWithNativeContext ::SetUpTestCase();
|
2016-09-06 13:34:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void TearDownTestCase() {
|
2017-11-13 12:04:57 +00:00
|
|
|
TestWithNativeContext ::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 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-11-16 16:50:27 +00:00
|
|
|
#define ASSERT_JOB_STATUS(STATUS, JOB) ASSERT_EQ(STATUS, JOB->status())
|
2017-07-04 08:55:45 +00:00
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
TEST_F(UnoptimizedCompileJobTest, Construct) {
|
|
|
|
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
|
2017-11-13 12:04:57 +00:00
|
|
|
isolate(), tracer(), test::CreateSharedFunctionInfo(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-11-13 12:04:57 +00:00
|
|
|
isolate(), tracer(), test::CreateSharedFunctionInfo(isolate(), nullptr),
|
|
|
|
FLAG_stack_size));
|
2016-07-28 07:46:15 +00:00
|
|
|
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::Status::kInitial, job);
|
|
|
|
job->PrepareOnMainThread(isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::Status::kPrepared, job);
|
|
|
|
job->Compile(false);
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::Status::kCompiled, job);
|
|
|
|
job->FinalizeOnMainThread(isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::Status::kDone, job);
|
2017-11-13 12:04:57 +00:00
|
|
|
job->ResetOnMainThread(isolate());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::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-11-13 12:04:57 +00:00
|
|
|
isolate(), tracer(), test::CreateSharedFunctionInfo(isolate(), &script),
|
|
|
|
FLAG_stack_size));
|
2016-08-01 11:27:58 +00:00
|
|
|
|
2017-11-16 16:50:27 +00:00
|
|
|
job->PrepareOnMainThread(isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
job->Compile(false);
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
job->ReportErrorsOnMainThread(isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_TRUE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::Status::kFailed, job);
|
2017-11-13 12:04:57 +00:00
|
|
|
ASSERT_TRUE(isolate()->has_pending_exception());
|
2016-08-23 12:10:39 +00:00
|
|
|
|
2017-11-13 12:04:57 +00:00
|
|
|
isolate()->clear_pending_exception();
|
2016-08-23 12:10:39 +00:00
|
|
|
|
2017-11-13 12:04:57 +00:00
|
|
|
job->ResetOnMainThread(isolate());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::Status::kInitial, job);
|
2016-08-05 13:18:42 +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-11-13 12:04:57 +00:00
|
|
|
Handle<JSFunction> f = RunJS<JSFunction>(script);
|
2017-07-27 13:33:53 +00:00
|
|
|
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
|
2017-11-13 12:04:57 +00:00
|
|
|
isolate(), tracer(), handle(f->shared()), FLAG_stack_size));
|
2016-08-25 10:24:53 +00:00
|
|
|
|
2017-11-16 16:50:27 +00:00
|
|
|
job->PrepareOnMainThread(isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
job->Compile(false);
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
job->FinalizeOnMainThread(isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::Status::kDone, job);
|
2016-08-25 10:24:53 +00:00
|
|
|
|
2017-11-13 12:04:57 +00:00
|
|
|
Smi* value = Smi::cast(*RunJS("f(100);"));
|
2016-08-25 10:24:53 +00:00
|
|
|
ASSERT_TRUE(value == Smi::FromInt(160));
|
|
|
|
|
2017-11-13 12:04:57 +00:00
|
|
|
job->ResetOnMainThread(isolate());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::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 = ");
|
2017-10-25 10:47:24 +00:00
|
|
|
for (int i = 0; i < 500000; i++) {
|
|
|
|
// TODO(leszeks): Figure out a more "unit-test-y" way of forcing an analysis
|
|
|
|
// failure than a binop stack overflow.
|
|
|
|
|
|
|
|
// Alternate + and - to avoid n-ary operation nodes.
|
|
|
|
raw_script += "'x' + 'x' - ";
|
2016-08-25 10:24:53 +00:00
|
|
|
}
|
|
|
|
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-11-13 12:04:57 +00:00
|
|
|
isolate(), tracer(), test::CreateSharedFunctionInfo(isolate(), &script),
|
|
|
|
100));
|
2016-08-25 10:24:53 +00:00
|
|
|
|
2017-11-16 16:50:27 +00:00
|
|
|
job->PrepareOnMainThread(isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
job->Compile(false);
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
job->ReportErrorsOnMainThread(isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_TRUE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::Status::kFailed, job);
|
2017-11-13 12:04:57 +00:00
|
|
|
ASSERT_TRUE(isolate()->has_pending_exception());
|
2016-08-25 10:24:53 +00:00
|
|
|
|
2017-11-13 12:04:57 +00:00
|
|
|
isolate()->clear_pending_exception();
|
|
|
|
job->ResetOnMainThread(isolate());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::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 = ");
|
2017-10-25 10:47:24 +00:00
|
|
|
for (int i = 0; i < 500; i++) {
|
|
|
|
// Alternate + and - to avoid n-ary operation nodes.
|
|
|
|
raw_script += "'x' + 'x' - ";
|
2016-08-25 10:24:53 +00:00
|
|
|
}
|
|
|
|
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-11-13 12:04:57 +00:00
|
|
|
isolate(), tracer(), test::CreateSharedFunctionInfo(isolate(), &script),
|
|
|
|
50));
|
2016-08-25 10:24:53 +00:00
|
|
|
|
2017-11-16 16:50:27 +00:00
|
|
|
job->PrepareOnMainThread(isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
job->Compile(false);
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
job->ReportErrorsOnMainThread(isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_TRUE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::Status::kFailed, job);
|
2017-11-13 12:04:57 +00:00
|
|
|
ASSERT_TRUE(isolate()->has_pending_exception());
|
2016-08-25 10:24:53 +00:00
|
|
|
|
2017-11-13 12:04:57 +00:00
|
|
|
isolate()->clear_pending_exception();
|
|
|
|
job->ResetOnMainThread(isolate());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::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-11-16 16:50:27 +00:00
|
|
|
job_->Compile(true);
|
2017-07-04 08:55:45 +00:00
|
|
|
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-11-13 12:04:57 +00:00
|
|
|
isolate(), tracer(), test::CreateSharedFunctionInfo(isolate(), &script),
|
|
|
|
100));
|
2016-09-06 13:34:31 +00:00
|
|
|
|
2017-11-16 16:50:27 +00:00
|
|
|
job->PrepareOnMainThread(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-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::Status::kPrepared, job);
|
2018-03-01 08:45:52 +00:00
|
|
|
V8::GetCurrentPlatform()->CallOnWorkerThread(background_task);
|
2016-09-06 13:34:31 +00:00
|
|
|
semaphore.Wait();
|
2017-11-16 16:50:27 +00:00
|
|
|
job->FinalizeOnMainThread(isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::Status::kDone, job);
|
2016-09-06 13:34:31 +00:00
|
|
|
|
2017-11-13 12:04:57 +00:00
|
|
|
job->ResetOnMainThread(isolate());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::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[] =
|
2017-11-16 12:58:16 +00:00
|
|
|
"f = function() {\n"
|
|
|
|
" e = (function() { return 42; });\n"
|
|
|
|
" return e;\n"
|
|
|
|
"};\n"
|
|
|
|
"f;";
|
2017-11-13 12:04:57 +00:00
|
|
|
Handle<JSFunction> f = RunJS<JSFunction>(script);
|
2016-12-16 12:38:17 +00:00
|
|
|
|
2017-07-27 13:33:53 +00:00
|
|
|
std::unique_ptr<UnoptimizedCompileJob> job(new UnoptimizedCompileJob(
|
2017-11-13 12:04:57 +00:00
|
|
|
isolate(), tracer(), handle(f->shared()), FLAG_stack_size));
|
2016-12-16 12:38:17 +00:00
|
|
|
|
2017-11-16 16:50:27 +00:00
|
|
|
job->PrepareOnMainThread(isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
job->Compile(false);
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
job->FinalizeOnMainThread(isolate());
|
2017-07-04 08:55:45 +00:00
|
|
|
ASSERT_FALSE(job->IsFailed());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::Status::kDone, job);
|
2016-12-16 12:38:17 +00:00
|
|
|
|
2017-11-13 12:04:57 +00:00
|
|
|
Handle<JSFunction> e = RunJS<JSFunction>("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-11-13 12:04:57 +00:00
|
|
|
job->ResetOnMainThread(isolate());
|
2017-11-16 16:50:27 +00:00
|
|
|
ASSERT_JOB_STATUS(CompilerDispatcherJob::Status::kInitial, job);
|
2016-12-16 12:38:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 16:50:27 +00:00
|
|
|
#undef ASSERT_JOB_STATUS
|
|
|
|
|
2016-07-22 12:19:50 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|