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/flags.h" |
9 #include "src/isolate-inl.h" | 9 #include "src/isolate-inl.h" |
10 #include "test/unittests/test-utils.h" | 10 #include "test/unittests/test-utils.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace v8 { | 13 namespace v8 { |
14 namespace internal { | 14 namespace internal { |
15 | 15 |
16 typedef TestWithContext CompilerDispatcherJobTest; | 16 typedef TestWithContext CompilerDispatcherJobTest; |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
| 20 const char test_script[] = "x*x"; |
| 21 |
20 class ScriptResource : public v8::String::ExternalOneByteStringResource { | 22 class ScriptResource : public v8::String::ExternalOneByteStringResource { |
21 public: | 23 public: |
22 ScriptResource(const char* data, size_t length) | 24 ScriptResource(const char* data, size_t length) |
23 : data_(data), length_(length) {} | 25 : data_(data), length_(length) {} |
24 ~ScriptResource() override = default; | 26 ~ScriptResource() override = default; |
25 | 27 |
26 const char* data() const override { return data_; } | 28 const char* data() const override { return data_; } |
27 size_t length() const override { return length_; } | 29 size_t length() const override { return length_; } |
28 | 30 |
29 private: | 31 private: |
30 const char* data_; | 32 const char* data_; |
31 size_t length_; | 33 size_t length_; |
32 | 34 |
33 DISALLOW_COPY_AND_ASSIGN(ScriptResource); | 35 DISALLOW_COPY_AND_ASSIGN(ScriptResource); |
34 }; | 36 }; |
35 | 37 |
36 Handle<JSFunction> CreateFunction( | 38 Handle<JSFunction> CreateFunction( |
37 Isolate* isolate, ExternalOneByteString::Resource* maybe_resource) { | 39 Isolate* isolate, ExternalOneByteString::Resource* maybe_resource) { |
38 HandleScope scope(isolate); | 40 HandleScope scope(isolate); |
39 Handle<String> source; | 41 Handle<String> source; |
40 if (maybe_resource) { | 42 if (maybe_resource) { |
41 source = isolate->factory() | 43 source = isolate->factory() |
42 ->NewExternalStringFromOneByte(maybe_resource) | 44 ->NewExternalStringFromOneByte(maybe_resource) |
43 .ToHandleChecked(); | 45 .ToHandleChecked(); |
44 } else { | 46 } else { |
45 source = isolate->factory()->NewStringFromStaticChars("source"); | 47 source = isolate->factory()->NewStringFromAsciiChecked(test_script); |
46 } | 48 } |
47 Handle<Script> script = isolate->factory()->NewScript(source); | 49 Handle<Script> script = isolate->factory()->NewScript(source); |
48 Handle<SharedFunctionInfo> shared = isolate->factory()->NewSharedFunctionInfo( | 50 Handle<SharedFunctionInfo> shared = isolate->factory()->NewSharedFunctionInfo( |
49 isolate->factory()->NewStringFromStaticChars("f"), MaybeHandle<Code>(), | 51 isolate->factory()->NewStringFromAsciiChecked("f"), MaybeHandle<Code>(), |
50 false); | 52 false); |
51 SharedFunctionInfo::SetScript(shared, script); | 53 SharedFunctionInfo::SetScript(shared, script); |
52 shared->set_end_position(source->length() - 1); | 54 shared->set_end_position(source->length()); |
53 Handle<JSFunction> function = | 55 Handle<JSFunction> function = |
54 isolate->factory()->NewFunctionFromSharedFunctionInfo( | 56 isolate->factory()->NewFunctionFromSharedFunctionInfo( |
55 shared, handle(isolate->context(), isolate)); | 57 shared, handle(isolate->context(), isolate)); |
56 return scope.CloseAndEscape(function); | 58 return scope.CloseAndEscape(function); |
57 } | 59 } |
58 | 60 |
59 } // namespace | 61 } // namespace |
60 | 62 |
61 TEST_F(CompilerDispatcherJobTest, Construct) { | 63 TEST_F(CompilerDispatcherJobTest, Construct) { |
62 std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob( | 64 std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob( |
63 i_isolate(), CreateFunction(i_isolate(), nullptr), FLAG_stack_size)); | 65 i_isolate(), CreateFunction(i_isolate(), nullptr), FLAG_stack_size)); |
64 } | 66 } |
65 | 67 |
66 TEST_F(CompilerDispatcherJobTest, CanParseOnBackgroundThread) { | 68 TEST_F(CompilerDispatcherJobTest, CanParseOnBackgroundThread) { |
67 { | 69 { |
68 std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob( | 70 std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob( |
69 i_isolate(), CreateFunction(i_isolate(), nullptr), FLAG_stack_size)); | 71 i_isolate(), CreateFunction(i_isolate(), nullptr), FLAG_stack_size)); |
70 ASSERT_FALSE(job->can_parse_on_background_thread()); | 72 ASSERT_FALSE(job->can_parse_on_background_thread()); |
71 } | 73 } |
72 { | 74 { |
73 ScriptResource script("script", strlen("script")); | 75 ScriptResource script(test_script, strlen(test_script)); |
74 std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob( | 76 std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob( |
75 i_isolate(), CreateFunction(i_isolate(), &script), FLAG_stack_size)); | 77 i_isolate(), CreateFunction(i_isolate(), &script), FLAG_stack_size)); |
76 ASSERT_TRUE(job->can_parse_on_background_thread()); | 78 ASSERT_TRUE(job->can_parse_on_background_thread()); |
77 } | 79 } |
78 } | 80 } |
79 | 81 |
80 TEST_F(CompilerDispatcherJobTest, StateTransitions) { | 82 TEST_F(CompilerDispatcherJobTest, StateTransitions) { |
81 std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob( | 83 std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob( |
82 i_isolate(), CreateFunction(i_isolate(), nullptr), FLAG_stack_size)); | 84 i_isolate(), CreateFunction(i_isolate(), nullptr), FLAG_stack_size)); |
83 | 85 |
(...skipping 19 matching lines...) Expand all Loading... |
103 | 105 |
104 ASSERT_TRUE(job->status() == CompileJobStatus::kFailed); | 106 ASSERT_TRUE(job->status() == CompileJobStatus::kFailed); |
105 ASSERT_FALSE(i_isolate()->has_pending_exception()); | 107 ASSERT_FALSE(i_isolate()->has_pending_exception()); |
106 job->ReportErrorsOnMainThread(); | 108 job->ReportErrorsOnMainThread(); |
107 ASSERT_TRUE(job->status() == CompileJobStatus::kDone); | 109 ASSERT_TRUE(job->status() == CompileJobStatus::kDone); |
108 ASSERT_TRUE(i_isolate()->has_pending_exception()); | 110 ASSERT_TRUE(i_isolate()->has_pending_exception()); |
109 } | 111 } |
110 | 112 |
111 } // namespace internal | 113 } // namespace internal |
112 } // namespace v8 | 114 } // namespace v8 |
OLD | NEW |