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"
|
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"
|
2016-12-12 15:35:41 +00:00
|
|
|
#include "test/unittests/compiler-dispatcher/compiler-dispatcher-helper.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 {
|
|
|
|
|
2016-12-12 15:35:41 +00:00
|
|
|
class CompilerDispatcherJobTest : public TestWithContext {
|
|
|
|
public:
|
|
|
|
CompilerDispatcherJobTest() : tracer_(i_isolate()) {}
|
|
|
|
~CompilerDispatcherJobTest() override {}
|
|
|
|
|
|
|
|
CompilerDispatcherTracer* tracer() { return &tracer_; }
|
|
|
|
|
2016-09-06 13:34:31 +00:00
|
|
|
static void SetUpTestCase() {
|
|
|
|
old_flag_ = i::FLAG_ignition;
|
|
|
|
i::FLAG_ignition = true;
|
|
|
|
TestWithContext::SetUpTestCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TearDownTestCase() {
|
|
|
|
TestWithContext::TearDownTestCase();
|
|
|
|
i::FLAG_ignition = old_flag_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-01-16 10:49:16 +00:00
|
|
|
CompilerDispatcherTracer tracer_;
|
2016-09-06 13:34:31 +00:00
|
|
|
static bool old_flag_;
|
2017-01-16 10:49:16 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CompilerDispatcherJobTest);
|
2016-09-06 13:34:31 +00:00
|
|
|
};
|
|
|
|
|
2017-01-16 10:49:16 +00:00
|
|
|
bool CompilerDispatcherJobTest::old_flag_;
|
2016-09-06 13:34:31 +00:00
|
|
|
|
2016-07-29 10:05:57 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-08-05 13:18:42 +00:00
|
|
|
const char test_script[] = "(x) { x*x; }";
|
2016-08-03 13:30:23 +00:00
|
|
|
|
2016-07-29 10:05:57 +00:00
|
|
|
class ScriptResource : public v8::String::ExternalOneByteStringResource {
|
|
|
|
public:
|
|
|
|
ScriptResource(const char* data, size_t length)
|
|
|
|
: data_(data), length_(length) {}
|
2016-07-29 10:56:07 +00:00
|
|
|
~ScriptResource() override = default;
|
2016-07-29 10:05:57 +00:00
|
|
|
|
2016-07-29 10:56:07 +00:00
|
|
|
const char* data() const override { return data_; }
|
|
|
|
size_t length() const override { return length_; }
|
2016-07-29 10:05:57 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const char* data_;
|
|
|
|
size_t length_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ScriptResource);
|
|
|
|
};
|
|
|
|
|
2016-09-20 12:07:52 +00:00
|
|
|
Handle<SharedFunctionInfo> CreateSharedFunctionInfo(
|
2016-07-29 10:05:57 +00:00
|
|
|
Isolate* isolate, ExternalOneByteString::Resource* maybe_resource) {
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
Handle<String> source;
|
|
|
|
if (maybe_resource) {
|
|
|
|
source = isolate->factory()
|
|
|
|
->NewExternalStringFromOneByte(maybe_resource)
|
|
|
|
.ToHandleChecked();
|
|
|
|
} else {
|
2016-08-03 13:30:23 +00:00
|
|
|
source = isolate->factory()->NewStringFromAsciiChecked(test_script);
|
2016-07-29 10:05:57 +00:00
|
|
|
}
|
|
|
|
Handle<Script> script = isolate->factory()->NewScript(source);
|
2016-12-15 17:19:55 +00:00
|
|
|
Handle<FixedArray> infos = isolate->factory()->NewFixedArray(3);
|
|
|
|
script->set_shared_function_infos(*infos);
|
2016-07-29 10:05:57 +00:00
|
|
|
Handle<SharedFunctionInfo> shared = isolate->factory()->NewSharedFunctionInfo(
|
2016-08-25 10:24:53 +00:00
|
|
|
isolate->factory()->NewStringFromAsciiChecked("f"),
|
|
|
|
isolate->builtins()->CompileLazy(), false);
|
2016-08-03 13:30:23 +00:00
|
|
|
shared->set_end_position(source->length());
|
2016-09-20 12:07:52 +00:00
|
|
|
shared->set_outer_scope_info(ScopeInfo::Empty(isolate));
|
2016-11-28 11:40:22 +00:00
|
|
|
shared->set_function_literal_id(1);
|
2016-12-15 17:19:55 +00:00
|
|
|
SharedFunctionInfo::SetScript(shared, script);
|
2016-09-20 12:07:52 +00:00
|
|
|
return scope.CloseAndEscape(shared);
|
2016-07-29 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-07-22 12:19:50 +00:00
|
|
|
TEST_F(CompilerDispatcherJobTest, Construct) {
|
2016-07-29 10:05:57 +00:00
|
|
|
std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob(
|
2016-12-12 15:35:41 +00:00
|
|
|
i_isolate(), tracer(), CreateSharedFunctionInfo(i_isolate(), nullptr),
|
2016-09-20 12:07:52 +00:00
|
|
|
FLAG_stack_size));
|
2016-07-29 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompilerDispatcherJobTest, StateTransitions) {
|
|
|
|
std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob(
|
2016-12-12 15:35:41 +00:00
|
|
|
i_isolate(), tracer(), CreateSharedFunctionInfo(i_isolate(), nullptr),
|
2016-09-20 12:07:52 +00:00
|
|
|
FLAG_stack_size));
|
2016-07-28 07:46:15 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kInitial);
|
|
|
|
job->PrepareToParseOnMainThread();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kReadyToParse);
|
2016-07-29 10:05:57 +00:00
|
|
|
job->Parse();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kParsed);
|
2016-08-23 12:10:39 +00:00
|
|
|
ASSERT_TRUE(job->FinalizeParsingOnMainThread());
|
2017-01-25 22:41:57 +00:00
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kReadyToAnalyze);
|
|
|
|
ASSERT_TRUE(job->AnalyzeOnMainThread());
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kAnalyzed);
|
2016-08-25 10:24:53 +00:00
|
|
|
ASSERT_TRUE(job->PrepareToCompileOnMainThread());
|
2016-08-01 11:27:58 +00:00
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kReadyToCompile);
|
2016-08-25 10:24:53 +00:00
|
|
|
job->Compile();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kCompiled);
|
|
|
|
ASSERT_TRUE(job->FinalizeCompilingOnMainThread());
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kDone);
|
2016-08-01 11:27:58 +00:00
|
|
|
job->ResetOnMainThread();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kInitial);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompilerDispatcherJobTest, SyntaxError) {
|
|
|
|
ScriptResource script("^^^", strlen("^^^"));
|
|
|
|
std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob(
|
2016-12-12 15:35:41 +00:00
|
|
|
i_isolate(), tracer(), CreateSharedFunctionInfo(i_isolate(), &script),
|
2016-09-20 12:07:52 +00:00
|
|
|
FLAG_stack_size));
|
2016-08-01 11:27:58 +00:00
|
|
|
|
|
|
|
job->PrepareToParseOnMainThread();
|
|
|
|
job->Parse();
|
2016-08-23 12:10:39 +00:00
|
|
|
ASSERT_FALSE(job->FinalizeParsingOnMainThread());
|
2016-08-01 11:27:58 +00:00
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kFailed);
|
|
|
|
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
|
|
|
|
|
|
|
job->ResetOnMainThread();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kInitial);
|
2016-08-05 13:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompilerDispatcherJobTest, ScopeChain) {
|
|
|
|
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();";
|
2016-08-25 10:24:53 +00:00
|
|
|
Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script));
|
2016-08-05 13:18:42 +00:00
|
|
|
|
2016-09-20 12:07:52 +00:00
|
|
|
std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob(
|
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
|
|
|
|
|
|
|
job->PrepareToParseOnMainThread();
|
|
|
|
job->Parse();
|
2016-08-23 12:10:39 +00:00
|
|
|
ASSERT_TRUE(job->FinalizeParsingOnMainThread());
|
2017-01-25 22:41:57 +00:00
|
|
|
ASSERT_TRUE(job->AnalyzeOnMainThread());
|
2016-09-15 16:41:03 +00:00
|
|
|
ASSERT_TRUE(job->PrepareToCompileOnMainThread());
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kReadyToCompile);
|
2016-08-05 13:18:42 +00:00
|
|
|
|
|
|
|
const AstRawString* var_x =
|
|
|
|
job->parse_info_->ast_value_factory()->GetOneByteString("x");
|
|
|
|
Variable* var = job->parse_info_->literal()->scope()->Lookup(var_x);
|
|
|
|
ASSERT_TRUE(var);
|
2016-09-15 16:41:03 +00:00
|
|
|
ASSERT_TRUE(var->IsParameter());
|
2016-08-05 13:18:42 +00:00
|
|
|
|
2016-09-20 12:07:52 +00:00
|
|
|
const AstRawString* var_y =
|
|
|
|
job->parse_info_->ast_value_factory()->GetOneByteString("y");
|
|
|
|
var = job->parse_info_->literal()->scope()->Lookup(var_y);
|
2016-08-05 13:18:42 +00:00
|
|
|
ASSERT_TRUE(var);
|
|
|
|
ASSERT_TRUE(var->IsContextSlot());
|
|
|
|
|
|
|
|
job->ResetOnMainThread();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kInitial);
|
2016-07-28 07:46:15 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 10:24:53 +00:00
|
|
|
TEST_F(CompilerDispatcherJobTest, CompileAndRun) {
|
|
|
|
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();";
|
|
|
|
Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script));
|
2016-09-20 12:07:52 +00:00
|
|
|
std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob(
|
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
|
|
|
|
|
|
|
job->PrepareToParseOnMainThread();
|
|
|
|
job->Parse();
|
|
|
|
job->FinalizeParsingOnMainThread();
|
2017-01-25 22:41:57 +00:00
|
|
|
job->AnalyzeOnMainThread();
|
2016-08-25 10:24:53 +00:00
|
|
|
job->PrepareToCompileOnMainThread();
|
|
|
|
job->Compile();
|
|
|
|
ASSERT_TRUE(job->FinalizeCompilingOnMainThread());
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kDone);
|
|
|
|
|
|
|
|
Smi* value = Smi::cast(*RunJS(isolate(), "f(100);"));
|
|
|
|
ASSERT_TRUE(value == Smi::FromInt(160));
|
|
|
|
|
|
|
|
job->ResetOnMainThread();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kInitial);
|
|
|
|
}
|
|
|
|
|
2017-01-25 22:41:57 +00:00
|
|
|
TEST_F(CompilerDispatcherJobTest, 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'; }";
|
|
|
|
ScriptResource script(raw_script.c_str(), strlen(raw_script.c_str()));
|
|
|
|
std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob(
|
2016-12-12 15:35:41 +00:00
|
|
|
i_isolate(), tracer(), CreateSharedFunctionInfo(i_isolate(), &script),
|
|
|
|
100));
|
2016-08-25 10:24:53 +00:00
|
|
|
|
|
|
|
job->PrepareToParseOnMainThread();
|
|
|
|
job->Parse();
|
|
|
|
job->FinalizeParsingOnMainThread();
|
2017-01-25 22:41:57 +00:00
|
|
|
ASSERT_FALSE(job->AnalyzeOnMainThread());
|
2016-08-25 10:24:53 +00:00
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kFailed);
|
|
|
|
ASSERT_TRUE(i_isolate()->has_pending_exception());
|
|
|
|
|
|
|
|
i_isolate()->clear_pending_exception();
|
|
|
|
job->ResetOnMainThread();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kInitial);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompilerDispatcherJobTest, CompileFailureToFinalize) {
|
|
|
|
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'; }";
|
|
|
|
ScriptResource script(raw_script.c_str(), strlen(raw_script.c_str()));
|
|
|
|
std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob(
|
2016-12-12 15:35:41 +00:00
|
|
|
i_isolate(), tracer(), CreateSharedFunctionInfo(i_isolate(), &script),
|
|
|
|
50));
|
2016-08-25 10:24:53 +00:00
|
|
|
|
|
|
|
job->PrepareToParseOnMainThread();
|
|
|
|
job->Parse();
|
|
|
|
job->FinalizeParsingOnMainThread();
|
2017-01-25 22:41:57 +00:00
|
|
|
job->AnalyzeOnMainThread();
|
2016-08-25 10:24:53 +00:00
|
|
|
job->PrepareToCompileOnMainThread();
|
|
|
|
job->Compile();
|
|
|
|
ASSERT_FALSE(job->FinalizeCompilingOnMainThread());
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kFailed);
|
|
|
|
ASSERT_TRUE(i_isolate()->has_pending_exception());
|
|
|
|
|
|
|
|
i_isolate()->clear_pending_exception();
|
|
|
|
job->ResetOnMainThread();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kInitial);
|
|
|
|
}
|
|
|
|
|
2016-09-06 13:34:31 +00:00
|
|
|
class CompileTask : public Task {
|
|
|
|
public:
|
|
|
|
CompileTask(CompilerDispatcherJob* job, base::Semaphore* semaphore)
|
|
|
|
: job_(job), semaphore_(semaphore) {}
|
|
|
|
~CompileTask() override {}
|
|
|
|
|
|
|
|
void Run() override {
|
|
|
|
job_->Compile();
|
|
|
|
semaphore_->Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CompilerDispatcherJob* job_;
|
|
|
|
base::Semaphore* semaphore_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CompileTask);
|
|
|
|
};
|
|
|
|
|
2017-01-16 10:49:16 +00:00
|
|
|
TEST_F(CompilerDispatcherJobTest, 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;"
|
|
|
|
"}";
|
|
|
|
ScriptResource script(raw_script, strlen(raw_script));
|
|
|
|
std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob(
|
2016-12-12 15:35:41 +00:00
|
|
|
i_isolate(), tracer(), CreateSharedFunctionInfo(i_isolate(), &script),
|
|
|
|
100));
|
2016-09-06 13:34:31 +00:00
|
|
|
|
|
|
|
job->PrepareToParseOnMainThread();
|
|
|
|
job->Parse();
|
|
|
|
job->FinalizeParsingOnMainThread();
|
2017-01-25 22:41:57 +00:00
|
|
|
job->AnalyzeOnMainThread();
|
2016-09-06 13:34:31 +00:00
|
|
|
job->PrepareToCompileOnMainThread();
|
|
|
|
|
|
|
|
base::Semaphore semaphore(0);
|
|
|
|
CompileTask* background_task = new CompileTask(job.get(), &semaphore);
|
|
|
|
V8::GetCurrentPlatform()->CallOnBackgroundThread(background_task,
|
|
|
|
Platform::kShortRunningTask);
|
|
|
|
semaphore.Wait();
|
|
|
|
ASSERT_TRUE(job->FinalizeCompilingOnMainThread());
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kDone);
|
|
|
|
|
|
|
|
job->ResetOnMainThread();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kInitial);
|
|
|
|
}
|
|
|
|
|
2016-12-16 12:38:17 +00:00
|
|
|
TEST_F(CompilerDispatcherJobTest, LazyInnerFunctions) {
|
|
|
|
const char script[] =
|
|
|
|
"function g() {\n"
|
|
|
|
" f = function() {\n"
|
|
|
|
" e = (function() { return 42; });\n"
|
|
|
|
" return e;\n"
|
|
|
|
" };\n"
|
|
|
|
" return f;\n"
|
|
|
|
"}\n"
|
|
|
|
"g();";
|
|
|
|
Handle<JSFunction> f = Handle<JSFunction>::cast(RunJS(isolate(), script));
|
|
|
|
|
|
|
|
std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob(
|
|
|
|
i_isolate(), tracer(), handle(f->shared()), FLAG_stack_size));
|
|
|
|
|
|
|
|
job->PrepareToParseOnMainThread();
|
|
|
|
job->Parse();
|
|
|
|
ASSERT_TRUE(job->FinalizeParsingOnMainThread());
|
2017-01-25 22:41:57 +00:00
|
|
|
ASSERT_TRUE(job->AnalyzeOnMainThread());
|
2016-12-16 12:38:17 +00:00
|
|
|
ASSERT_TRUE(job->PrepareToCompileOnMainThread());
|
|
|
|
job->Compile();
|
|
|
|
ASSERT_TRUE(job->FinalizeCompilingOnMainThread());
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kDone);
|
|
|
|
|
|
|
|
Handle<JSFunction> e = Handle<JSFunction>::cast(RunJS(isolate(), "f();"));
|
|
|
|
|
|
|
|
ASSERT_FALSE(e->shared()->HasBaselineCode());
|
|
|
|
|
|
|
|
job->ResetOnMainThread();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kInitial);
|
|
|
|
}
|
|
|
|
|
2016-07-22 12:19:50 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|