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"
|
|
|
|
#include "src/ast/scopes.h"
|
2016-07-22 12:19:50 +00:00
|
|
|
#include "src/compiler-dispatcher/compiler-dispatcher-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-05 13:18:42 +00:00
|
|
|
#include "src/parsing/parser.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 {
|
|
|
|
|
|
|
|
typedef TestWithContext CompilerDispatcherJobTest;
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
Handle<JSFunction> CreateFunction(
|
|
|
|
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);
|
|
|
|
Handle<SharedFunctionInfo> shared = isolate->factory()->NewSharedFunctionInfo(
|
2016-08-03 13:30:23 +00:00
|
|
|
isolate->factory()->NewStringFromAsciiChecked("f"), MaybeHandle<Code>(),
|
2016-07-29 10:05:57 +00:00
|
|
|
false);
|
|
|
|
SharedFunctionInfo::SetScript(shared, script);
|
2016-08-03 13:30:23 +00:00
|
|
|
shared->set_end_position(source->length());
|
2016-07-29 10:05:57 +00:00
|
|
|
Handle<JSFunction> function =
|
|
|
|
isolate->factory()->NewFunctionFromSharedFunctionInfo(
|
|
|
|
shared, handle(isolate->context(), isolate));
|
|
|
|
return scope.CloseAndEscape(function);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // 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-08-01 11:27:58 +00:00
|
|
|
i_isolate(), CreateFunction(i_isolate(), nullptr), FLAG_stack_size));
|
2016-07-29 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompilerDispatcherJobTest, CanParseOnBackgroundThread) {
|
|
|
|
{
|
|
|
|
std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob(
|
2016-08-01 11:27:58 +00:00
|
|
|
i_isolate(), CreateFunction(i_isolate(), nullptr), FLAG_stack_size));
|
2016-07-29 10:05:57 +00:00
|
|
|
ASSERT_FALSE(job->can_parse_on_background_thread());
|
|
|
|
}
|
|
|
|
{
|
2016-08-03 13:30:23 +00:00
|
|
|
ScriptResource script(test_script, strlen(test_script));
|
2016-07-29 10:05:57 +00:00
|
|
|
std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob(
|
2016-08-01 11:27:58 +00:00
|
|
|
i_isolate(), CreateFunction(i_isolate(), &script), FLAG_stack_size));
|
2016-07-29 10:05:57 +00:00
|
|
|
ASSERT_TRUE(job->can_parse_on_background_thread());
|
|
|
|
}
|
2016-07-22 12:19:50 +00:00
|
|
|
}
|
|
|
|
|
2016-07-29 10:05:57 +00:00
|
|
|
TEST_F(CompilerDispatcherJobTest, StateTransitions) {
|
|
|
|
std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob(
|
2016-08-01 11:27:58 +00:00
|
|
|
i_isolate(), CreateFunction(i_isolate(), nullptr), 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-01 11:27:58 +00:00
|
|
|
job->FinalizeParsingOnMainThread();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kReadyToCompile);
|
|
|
|
job->ResetOnMainThread();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kInitial);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompilerDispatcherJobTest, SyntaxError) {
|
|
|
|
ScriptResource script("^^^", strlen("^^^"));
|
|
|
|
std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob(
|
|
|
|
i_isolate(), CreateFunction(i_isolate(), &script), FLAG_stack_size));
|
|
|
|
|
|
|
|
job->PrepareToParseOnMainThread();
|
|
|
|
job->Parse();
|
|
|
|
job->FinalizeParsingOnMainThread();
|
|
|
|
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kFailed);
|
|
|
|
ASSERT_FALSE(i_isolate()->has_pending_exception());
|
|
|
|
job->ReportErrorsOnMainThread();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kDone);
|
|
|
|
ASSERT_TRUE(i_isolate()->has_pending_exception());
|
2016-08-05 13:18:42 +00:00
|
|
|
i_isolate()->clear_pending_exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CompilerDispatcherJobTest, ScopeChain) {
|
|
|
|
const char script[] =
|
|
|
|
"function g() { var g = 1; function f(x) { return x * g }; return f; } "
|
|
|
|
"g();";
|
|
|
|
Handle<JSFunction> f = Handle<JSFunction>::cast(Utils::OpenHandle(
|
|
|
|
*v8::Script::Compile(isolate()->GetCurrentContext(),
|
|
|
|
v8::String::NewFromUtf8(isolate(), script,
|
|
|
|
v8::NewStringType::kNormal)
|
|
|
|
.ToLocalChecked())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Run(isolate()->GetCurrentContext())
|
|
|
|
.ToLocalChecked()));
|
|
|
|
|
|
|
|
std::unique_ptr<CompilerDispatcherJob> job(
|
|
|
|
new CompilerDispatcherJob(i_isolate(), f, FLAG_stack_size));
|
|
|
|
|
|
|
|
job->PrepareToParseOnMainThread();
|
|
|
|
job->Parse();
|
|
|
|
job->FinalizeParsingOnMainThread();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kReadyToCompile);
|
|
|
|
|
|
|
|
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);
|
|
|
|
ASSERT_TRUE(var->IsUnallocated());
|
|
|
|
|
|
|
|
const AstRawString* var_g =
|
|
|
|
job->parse_info_->ast_value_factory()->GetOneByteString("g");
|
|
|
|
var = job->parse_info_->literal()->scope()->Lookup(var_g);
|
|
|
|
ASSERT_TRUE(var);
|
|
|
|
ASSERT_TRUE(var->IsContextSlot());
|
|
|
|
|
|
|
|
job->ResetOnMainThread();
|
|
|
|
ASSERT_TRUE(job->status() == CompileJobStatus::kInitial);
|
2016-07-28 07:46:15 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 12:19:50 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|