OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <memory> | 5 #include <memory> |
6 | 6 |
7 #include "src/compiler-dispatcher/compiler-dispatcher-job.h" | 7 #include "src/compiler-dispatcher/compiler-dispatcher-job.h" |
| 8 #include "src/flags.h" |
8 #include "src/isolate-inl.h" | 9 #include "src/isolate-inl.h" |
9 #include "test/unittests/test-utils.h" | 10 #include "test/unittests/test-utils.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 namespace v8 { | 13 namespace v8 { |
13 namespace internal { | 14 namespace internal { |
14 | 15 |
15 typedef TestWithContext CompilerDispatcherJobTest; | 16 typedef TestWithContext CompilerDispatcherJobTest; |
16 | 17 |
| 18 namespace { |
| 19 |
| 20 class ScriptResource : public v8::String::ExternalOneByteStringResource { |
| 21 public: |
| 22 ScriptResource(const char* data, size_t length) |
| 23 : data_(data), length_(length) {} |
| 24 |
| 25 const char* data() const { return data_; } |
| 26 size_t length() const { return length_; } |
| 27 |
| 28 private: |
| 29 const char* data_; |
| 30 size_t length_; |
| 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(ScriptResource); |
| 33 }; |
| 34 |
| 35 Handle<JSFunction> CreateFunction( |
| 36 Isolate* isolate, ExternalOneByteString::Resource* maybe_resource) { |
| 37 HandleScope scope(isolate); |
| 38 Handle<String> source; |
| 39 if (maybe_resource) { |
| 40 source = isolate->factory() |
| 41 ->NewExternalStringFromOneByte(maybe_resource) |
| 42 .ToHandleChecked(); |
| 43 } else { |
| 44 source = isolate->factory()->NewStringFromStaticChars("source"); |
| 45 } |
| 46 Handle<Script> script = isolate->factory()->NewScript(source); |
| 47 Handle<SharedFunctionInfo> shared = isolate->factory()->NewSharedFunctionInfo( |
| 48 isolate->factory()->NewStringFromStaticChars("f"), MaybeHandle<Code>(), |
| 49 false); |
| 50 SharedFunctionInfo::SetScript(shared, script); |
| 51 Handle<JSFunction> function = |
| 52 isolate->factory()->NewFunctionFromSharedFunctionInfo( |
| 53 shared, handle(isolate->context(), isolate)); |
| 54 return scope.CloseAndEscape(function); |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
17 TEST_F(CompilerDispatcherJobTest, Construct) { | 59 TEST_F(CompilerDispatcherJobTest, Construct) { |
18 Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate()); | 60 Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate()); |
19 std::unique_ptr<CompilerDispatcherJob> job( | 61 std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob( |
20 new CompilerDispatcherJob(i_isolate, i_isolate->object_function())); | 62 i_isolate, CreateFunction(i_isolate, nullptr), FLAG_stack_size)); |
21 } | 63 } |
22 | 64 |
23 TEST_F(CompilerDispatcherJobTest, PrepareToParse) { | 65 TEST_F(CompilerDispatcherJobTest, CanParseOnBackgroundThread) { |
24 Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate()); | 66 Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate()); |
25 std::unique_ptr<CompilerDispatcherJob> job( | 67 { |
26 new CompilerDispatcherJob(i_isolate, i_isolate->object_function())); | 68 std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob( |
| 69 i_isolate, CreateFunction(i_isolate, nullptr), FLAG_stack_size)); |
| 70 ASSERT_FALSE(job->can_parse_on_background_thread()); |
| 71 } |
| 72 { |
| 73 ScriptResource script("script", strlen("script")); |
| 74 std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob( |
| 75 i_isolate, CreateFunction(i_isolate, &script), FLAG_stack_size)); |
| 76 ASSERT_TRUE(job->can_parse_on_background_thread()); |
| 77 } |
| 78 } |
| 79 |
| 80 TEST_F(CompilerDispatcherJobTest, StateTransitions) { |
| 81 Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate()); |
| 82 std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob( |
| 83 i_isolate, CreateFunction(i_isolate, nullptr), FLAG_stack_size)); |
27 | 84 |
28 ASSERT_TRUE(job->status() == CompileJobStatus::kInitial); | 85 ASSERT_TRUE(job->status() == CompileJobStatus::kInitial); |
29 job->PrepareToParseOnMainThread(); | 86 job->PrepareToParseOnMainThread(); |
30 ASSERT_TRUE(job->status() == CompileJobStatus::kReadyToParse); | 87 ASSERT_TRUE(job->status() == CompileJobStatus::kReadyToParse); |
| 88 job->Parse(); |
| 89 ASSERT_TRUE(job->status() == CompileJobStatus::kParsed); |
31 } | 90 } |
32 | 91 |
33 } // namespace internal | 92 } // namespace internal |
34 } // namespace v8 | 93 } // namespace v8 |
OLD | NEW |