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 "src/compiler-dispatcher/compiler-dispatcher-job.h" | 5 #include "src/compiler-dispatcher/compiler-dispatcher-job.h" |
6 | 6 |
7 #include "src/assert-scope.h" | 7 #include "src/assert-scope.h" |
8 #include "src/global-handles.h" | 8 #include "src/global-handles.h" |
9 #include "src/isolate.h" | 9 #include "src/isolate.h" |
10 #include "src/objects-inl.h" | 10 #include "src/objects-inl.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 HandleScope scope(isolate_); | 26 HandleScope scope(isolate_); |
27 Handle<SharedFunctionInfo> shared(function_->shared(), isolate_); | 27 Handle<SharedFunctionInfo> shared(function_->shared(), isolate_); |
28 Handle<Script> script(Script::cast(shared->script()), isolate_); | 28 Handle<Script> script(Script::cast(shared->script()), isolate_); |
29 Handle<String> source(String::cast(script->source()), isolate_); | 29 Handle<String> source(String::cast(script->source()), isolate_); |
30 can_parse_on_background_thread_ = | 30 can_parse_on_background_thread_ = |
31 source->IsExternalTwoByteString() || source->IsExternalOneByteString(); | 31 source->IsExternalTwoByteString() || source->IsExternalOneByteString(); |
32 } | 32 } |
33 | 33 |
34 CompilerDispatcherJob::~CompilerDispatcherJob() { | 34 CompilerDispatcherJob::~CompilerDispatcherJob() { |
35 DCHECK(ThreadId::Current().Equals(isolate_->thread_id())); | 35 DCHECK(ThreadId::Current().Equals(isolate_->thread_id())); |
| 36 DCHECK(status_ == CompileJobStatus::kInitial || |
| 37 status_ == CompileJobStatus::kDone); |
36 i::GlobalHandles::Destroy(Handle<Object>::cast(function_).location()); | 38 i::GlobalHandles::Destroy(Handle<Object>::cast(function_).location()); |
37 } | 39 } |
38 | 40 |
39 void CompilerDispatcherJob::PrepareToParseOnMainThread() { | 41 void CompilerDispatcherJob::PrepareToParseOnMainThread() { |
40 DCHECK(ThreadId::Current().Equals(isolate_->thread_id())); | 42 DCHECK(ThreadId::Current().Equals(isolate_->thread_id())); |
41 DCHECK(status() == CompileJobStatus::kInitial); | 43 DCHECK(status() == CompileJobStatus::kInitial); |
42 HandleScope scope(isolate_); | 44 HandleScope scope(isolate_); |
43 unicode_cache_.reset(new UnicodeCache()); | 45 unicode_cache_.reset(new UnicodeCache()); |
44 zone_.reset(new Zone(isolate_->allocator())); | 46 zone_.reset(new Zone(isolate_->allocator())); |
45 Handle<SharedFunctionInfo> shared(function_->shared(), isolate_); | 47 Handle<SharedFunctionInfo> shared(function_->shared(), isolate_); |
46 Handle<Script> script(Script::cast(shared->script()), isolate_); | 48 Handle<Script> script(Script::cast(shared->script()), isolate_); |
47 Handle<String> source(String::cast(script->source()), isolate_); | 49 Handle<String> source(String::cast(script->source()), isolate_); |
48 if (source->IsExternalTwoByteString()) { | 50 if (source->IsExternalTwoByteString()) { |
49 can_parse_on_background_thread_ = true; | |
50 character_stream_.reset(new ExternalTwoByteStringUtf16CharacterStream( | 51 character_stream_.reset(new ExternalTwoByteStringUtf16CharacterStream( |
51 Handle<ExternalTwoByteString>::cast(source), shared->start_position(), | 52 Handle<ExternalTwoByteString>::cast(source), shared->start_position(), |
52 shared->end_position())); | 53 shared->end_position())); |
53 } else if (source->IsExternalOneByteString()) { | 54 } else if (source->IsExternalOneByteString()) { |
54 can_parse_on_background_thread_ = true; | |
55 character_stream_.reset(new ExternalOneByteStringUtf16CharacterStream( | 55 character_stream_.reset(new ExternalOneByteStringUtf16CharacterStream( |
56 Handle<ExternalOneByteString>::cast(source), shared->start_position(), | 56 Handle<ExternalOneByteString>::cast(source), shared->start_position(), |
57 shared->end_position())); | 57 shared->end_position())); |
58 } else { | 58 } else { |
59 can_parse_on_background_thread_ = false; | 59 source = String::Flatten(source); |
| 60 // Have to globalize the reference here, so it survives between function |
| 61 // calls. |
| 62 source_ = Handle<String>::cast(isolate_->global_handles()->Create(*source)); |
60 character_stream_.reset(new GenericStringUtf16CharacterStream( | 63 character_stream_.reset(new GenericStringUtf16CharacterStream( |
61 source, shared->start_position(), shared->end_position())); | 64 source_, shared->start_position(), shared->end_position())); |
62 } | 65 } |
63 parse_info_.reset(new ParseInfo(zone_.get())); | 66 parse_info_.reset(new ParseInfo(zone_.get())); |
64 parse_info_->set_isolate(isolate_); | 67 parse_info_->set_isolate(isolate_); |
65 parse_info_->set_character_stream(character_stream_.get()); | 68 parse_info_->set_character_stream(character_stream_.get()); |
66 parse_info_->set_hash_seed(isolate_->heap()->HashSeed()); | 69 parse_info_->set_hash_seed(isolate_->heap()->HashSeed()); |
67 parse_info_->set_unicode_cache(unicode_cache_.get()); | 70 parse_info_->set_unicode_cache(unicode_cache_.get()); |
68 parser_.reset(new Parser(parse_info_.get())); | 71 parser_.reset(new Parser(parse_info_.get())); |
69 status_ = CompileJobStatus::kReadyToParse; | 72 status_ = CompileJobStatus::kReadyToParse; |
70 } | 73 } |
71 | 74 |
72 void CompilerDispatcherJob::Parse() { | 75 void CompilerDispatcherJob::Parse() { |
73 DCHECK(can_parse_on_background_thread_ || | 76 DCHECK(can_parse_on_background_thread_ || |
74 ThreadId::Current().Equals(isolate_->thread_id())); | 77 ThreadId::Current().Equals(isolate_->thread_id())); |
75 DCHECK(status() == CompileJobStatus::kReadyToParse); | 78 DCHECK(status() == CompileJobStatus::kReadyToParse); |
76 | 79 |
77 DisallowHeapAllocation no_allocation; | 80 DisallowHeapAllocation no_allocation; |
78 DisallowHandleAllocation no_handles; | 81 DisallowHandleAllocation no_handles; |
79 DisallowHandleDereference no_deref; | 82 std::unique_ptr<DisallowHandleDereference> no_deref; |
| 83 // If we can't parse on a background thread, we need to be able to deref the |
| 84 // source string. |
| 85 if (can_parse_on_background_thread_) { |
| 86 no_deref.reset(new DisallowHandleDereference()); |
| 87 } |
80 | 88 |
81 // Nullify the Isolate temporarily so that the parser doesn't accidentally | 89 // Nullify the Isolate temporarily so that the parser doesn't accidentally |
82 // use it. | 90 // use it. |
83 parse_info_->set_isolate(nullptr); | 91 parse_info_->set_isolate(nullptr); |
84 | 92 |
85 uintptr_t stack_limit = | 93 uintptr_t stack_limit = |
86 reinterpret_cast<uintptr_t>(&stack_limit) - max_stack_size_ * KB; | 94 reinterpret_cast<uintptr_t>(&stack_limit) - max_stack_size_ * KB; |
87 | 95 |
88 parser_->set_stack_limit(stack_limit); | 96 parser_->set_stack_limit(stack_limit); |
89 parser_->ParseOnBackground(parse_info_.get()); | 97 parser_->ParseOnBackground(parse_info_.get()); |
90 | 98 |
91 parse_info_->set_isolate(isolate_); | 99 parse_info_->set_isolate(isolate_); |
92 | 100 |
93 status_ = CompileJobStatus::kParsed; | 101 status_ = CompileJobStatus::kParsed; |
94 } | 102 } |
95 | 103 |
| 104 void CompilerDispatcherJob::FinalizeParsingOnMainThread() { |
| 105 DCHECK(ThreadId::Current().Equals(isolate_->thread_id())); |
| 106 DCHECK(status() == CompileJobStatus::kParsed); |
| 107 |
| 108 if (!source_.is_null()) { |
| 109 i::GlobalHandles::Destroy(Handle<Object>::cast(source_).location()); |
| 110 source_ = Handle<String>::null(); |
| 111 } |
| 112 |
| 113 if (parse_info_->literal() == nullptr) { |
| 114 status_ = CompileJobStatus::kFailed; |
| 115 return; |
| 116 } |
| 117 |
| 118 InternalizeParsingResult(); |
| 119 |
| 120 status_ = CompileJobStatus::kReadyToCompile; |
| 121 } |
| 122 |
| 123 void CompilerDispatcherJob::ReportErrorsOnMainThread() { |
| 124 DCHECK(ThreadId::Current().Equals(isolate_->thread_id())); |
| 125 DCHECK(status() == CompileJobStatus::kFailed); |
| 126 |
| 127 // Internalizing the parsing result will throw the error. |
| 128 InternalizeParsingResult(); |
| 129 |
| 130 status_ = CompileJobStatus::kDone; |
| 131 } |
| 132 |
| 133 void CompilerDispatcherJob::ResetOnMainThread() { |
| 134 DCHECK(ThreadId::Current().Equals(isolate_->thread_id())); |
| 135 |
| 136 parser_.reset(); |
| 137 unicode_cache_.reset(); |
| 138 character_stream_.reset(); |
| 139 parse_info_.reset(); |
| 140 zone_.reset(); |
| 141 |
| 142 if (!source_.is_null()) { |
| 143 i::GlobalHandles::Destroy(Handle<Object>::cast(source_).location()); |
| 144 source_ = Handle<String>::null(); |
| 145 } |
| 146 |
| 147 status_ = CompileJobStatus::kInitial; |
| 148 } |
| 149 |
| 150 void CompilerDispatcherJob::InternalizeParsingResult() { |
| 151 DCHECK(ThreadId::Current().Equals(isolate_->thread_id())); |
| 152 DCHECK(status() == CompileJobStatus::kParsed || |
| 153 status() == CompileJobStatus::kFailed); |
| 154 |
| 155 HandleScope scope(isolate_); |
| 156 Handle<SharedFunctionInfo> shared(function_->shared(), isolate_); |
| 157 Handle<Script> script(Script::cast(shared->script()), isolate_); |
| 158 |
| 159 parse_info_->set_script(script); |
| 160 parse_info_->set_context(handle(function_->context(), isolate_)); |
| 161 |
| 162 // Do the parsing tasks which need to be done on the main thread. This will |
| 163 // also handle parse errors. |
| 164 parser_->Internalize(isolate_, script, parse_info_->literal() == nullptr); |
| 165 parser_->HandleSourceURLComments(isolate_, script); |
| 166 |
| 167 parse_info_->set_character_stream(nullptr); |
| 168 parse_info_->set_unicode_cache(nullptr); |
| 169 parser_.reset(); |
| 170 unicode_cache_.reset(); |
| 171 character_stream_.reset(); |
| 172 } |
| 173 |
96 } // namespace internal | 174 } // namespace internal |
97 } // namespace v8 | 175 } // namespace v8 |
OLD | NEW |