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 source_ = Handle<String>::cast(isolate_->global_handles()->Create(*source)); | |
60 character_stream_.reset(new GenericStringUtf16CharacterStream( | 61 character_stream_.reset(new GenericStringUtf16CharacterStream( |
61 source, shared->start_position(), shared->end_position())); | 62 source_, shared->start_position(), shared->end_position())); |
62 } | 63 } |
63 parse_info_.reset(new ParseInfo(zone_.get())); | 64 parse_info_.reset(new ParseInfo(zone_.get())); |
64 parse_info_->set_isolate(isolate_); | 65 parse_info_->set_isolate(isolate_); |
65 parse_info_->set_character_stream(character_stream_.get()); | 66 parse_info_->set_character_stream(character_stream_.get()); |
66 parse_info_->set_hash_seed(isolate_->heap()->HashSeed()); | 67 parse_info_->set_hash_seed(isolate_->heap()->HashSeed()); |
67 parse_info_->set_unicode_cache(unicode_cache_.get()); | 68 parse_info_->set_unicode_cache(unicode_cache_.get()); |
68 parser_.reset(new Parser(parse_info_.get())); | 69 parser_.reset(new Parser(parse_info_.get())); |
69 status_ = CompileJobStatus::kReadyToParse; | 70 status_ = CompileJobStatus::kReadyToParse; |
70 } | 71 } |
71 | 72 |
72 void CompilerDispatcherJob::Parse() { | 73 void CompilerDispatcherJob::Parse() { |
73 DCHECK(can_parse_on_background_thread_ || | 74 DCHECK(can_parse_on_background_thread_ || |
74 ThreadId::Current().Equals(isolate_->thread_id())); | 75 ThreadId::Current().Equals(isolate_->thread_id())); |
75 DCHECK(status() == CompileJobStatus::kReadyToParse); | 76 DCHECK(status() == CompileJobStatus::kReadyToParse); |
76 | 77 |
77 DisallowHeapAllocation no_allocation; | 78 DisallowHeapAllocation no_allocation; |
78 DisallowHandleAllocation no_handles; | 79 DisallowHandleAllocation no_handles; |
79 DisallowHandleDereference no_deref; | 80 std::unique_ptr<DisallowHandleDereference> no_deref; |
81 // If we can't parse on a background thread, we need to be able to deref the | |
82 // source string. | |
83 if (can_parse_on_background_thread_) { | |
84 no_deref.reset(new DisallowHandleDereference()); | |
85 } | |
80 | 86 |
81 // Nullify the Isolate temporarily so that the parser doesn't accidentally | 87 // Nullify the Isolate temporarily so that the parser doesn't accidentally |
82 // use it. | 88 // use it. |
83 parse_info_->set_isolate(nullptr); | 89 parse_info_->set_isolate(nullptr); |
84 | 90 |
85 uintptr_t stack_limit = | 91 uintptr_t stack_limit = |
86 reinterpret_cast<uintptr_t>(&stack_limit) - max_stack_size_ * KB; | 92 reinterpret_cast<uintptr_t>(&stack_limit) - max_stack_size_ * KB; |
87 | 93 |
88 parser_->set_stack_limit(stack_limit); | 94 parser_->set_stack_limit(stack_limit); |
89 parser_->ParseOnBackground(parse_info_.get()); | 95 parser_->ParseOnBackground(parse_info_.get()); |
90 | 96 |
91 parse_info_->set_isolate(isolate_); | 97 parse_info_->set_isolate(isolate_); |
92 | 98 |
93 status_ = CompileJobStatus::kParsed; | 99 status_ = CompileJobStatus::kParsed; |
94 } | 100 } |
95 | 101 |
102 void CompilerDispatcherJob::FinalizeParsingOnMainThread() { | |
103 DCHECK(ThreadId::Current().Equals(isolate_->thread_id())); | |
104 DCHECK(status() == CompileJobStatus::kParsed); | |
105 | |
106 if (!source_.is_null()) { | |
107 i::GlobalHandles::Destroy(Handle<Object>::cast(source_).location()); | |
108 source_ = Handle<String>::null(); | |
109 } | |
110 | |
111 if (parse_info_->literal() == nullptr) { | |
112 status_ = CompileJobStatus::kFailed; | |
113 return; | |
114 } | |
115 | |
116 InternalizeParsingResult(); | |
marja
2016/08/01 07:50:24
Wouldn't it make sense to always call InternalizeP
jochen (gone - plz use gerrit)
2016/08/01 09:23:01
InternalizeParsingResult will schedule an exceptio
| |
117 | |
118 status_ = CompileJobStatus::kReadyToCompile; | |
119 } | |
120 | |
121 void CompilerDispatcherJob::ReportErrorsOnMainThread() { | |
122 DCHECK(ThreadId::Current().Equals(isolate_->thread_id())); | |
123 DCHECK(status() == CompileJobStatus::kFailed); | |
124 | |
125 // Internalizing the parsing result will throw the error. | |
126 InternalizeParsingResult(); | |
127 | |
128 status_ = CompileJobStatus::kDone; | |
129 } | |
130 | |
131 void CompilerDispatcherJob::ResetOnMainThread() { | |
132 DCHECK(ThreadId::Current().Equals(isolate_->thread_id())); | |
133 | |
134 parser_.reset(); | |
135 unicode_cache_.reset(); | |
136 character_stream_.reset(); | |
137 parse_info_.reset(); | |
138 zone_.reset(); | |
139 | |
140 if (!source_.is_null()) { | |
141 i::GlobalHandles::Destroy(Handle<Object>::cast(source_).location()); | |
142 source_ = Handle<String>::null(); | |
143 } | |
144 | |
145 status_ = CompileJobStatus::kInitial; | |
146 } | |
147 | |
148 void CompilerDispatcherJob::InternalizeParsingResult() { | |
149 DCHECK(ThreadId::Current().Equals(isolate_->thread_id())); | |
150 DCHECK(status() == CompileJobStatus::kParsed || | |
151 status() == CompileJobStatus::kFailed); | |
152 | |
153 HandleScope scope(isolate_); | |
154 Handle<SharedFunctionInfo> shared(function_->shared(), isolate_); | |
155 Handle<Script> script(Script::cast(shared->script()), isolate_); | |
156 | |
157 parse_info_->set_script(script); | |
158 parse_info_->set_context(handle(function_->context(), isolate_)); | |
159 | |
160 // Do the parsing tasks which need to be done on the main thread. This will | |
161 // also handle parse errors. | |
162 parser_->Internalize(isolate_, script, parse_info_->literal() == nullptr); | |
163 parser_->HandleSourceURLComments(isolate_, script); | |
164 | |
165 parse_info_->set_character_stream(nullptr); | |
166 parse_info_->set_unicode_cache(nullptr); | |
167 parser_.reset(); | |
168 unicode_cache_.reset(); | |
169 character_stream_.reset(); | |
170 } | |
171 | |
96 } // namespace internal | 172 } // namespace internal |
97 } // namespace v8 | 173 } // namespace v8 |
OLD | NEW |