blob: 3e0a5dcc424509be64d77c6322a05c1b4bca4dba [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/background-parsing-task.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006#include "src/debug/debug.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007
8namespace v8 {
9namespace internal {
10
11BackgroundParsingTask::BackgroundParsingTask(
12 StreamedSource* source, ScriptCompiler::CompileOptions options,
13 int stack_size, Isolate* isolate)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014 : source_(source), stack_size_(stack_size) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015 // We don't set the context to the CompilationInfo yet, because the background
16 // thread cannot do anything with it anyway. We set it just before compilation
17 // on the foreground thread.
18 DCHECK(options == ScriptCompiler::kProduceParserCache ||
19 options == ScriptCompiler::kProduceCodeCache ||
20 options == ScriptCompiler::kNoCompileOptions);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000021
22 // Prepare the data for the internalization phase and compilation phase, which
23 // will happen in the main thread after parsing.
Ben Murdochda12d292016-06-02 14:46:10 +010024 Zone* zone = new Zone(isolate->allocator());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000025 ParseInfo* info = new ParseInfo(zone);
26 source->zone.Reset(zone);
27 source->info.Reset(info);
28 info->set_isolate(isolate);
29 info->set_source_stream(source->source_stream.get());
30 info->set_source_stream_encoding(source->encoding);
31 info->set_hash_seed(isolate->heap()->HashSeed());
32 info->set_global();
33 info->set_unicode_cache(&source_->unicode_cache);
34 info->set_compile_options(options);
Ben Murdochda12d292016-06-02 14:46:10 +010035 // Parse eagerly with ignition since we will compile eagerly.
36 info->set_allow_lazy_parsing(!(i::FLAG_ignition && i::FLAG_ignition_eager));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037}
38
39
40void BackgroundParsingTask::Run() {
41 DisallowHeapAllocation no_allocation;
42 DisallowHandleAllocation no_handles;
43 DisallowHandleDereference no_deref;
44
45 ScriptData* script_data = NULL;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046 ScriptCompiler::CompileOptions options = source_->info->compile_options();
47 if (options == ScriptCompiler::kProduceParserCache ||
48 options == ScriptCompiler::kProduceCodeCache) {
49 source_->info->set_cached_data(&script_data);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050 }
51
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000052 uintptr_t stack_limit =
53 reinterpret_cast<uintptr_t>(&stack_limit) - stack_size_ * KB;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000055 source_->info->set_stack_limit(stack_limit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056 // Parser needs to stay alive for finalizing the parsing on the main
57 // thread. Passing &parse_info is OK because Parser doesn't store it.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000058 source_->parser.Reset(new Parser(source_->info.get()));
59 source_->parser->ParseOnBackground(source_->info.get());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060
61 if (script_data != NULL) {
62 source_->cached_data.Reset(new ScriptCompiler::CachedData(
63 script_data->data(), script_data->length(),
64 ScriptCompiler::CachedData::BufferOwned));
65 script_data->ReleaseDataOwnership();
66 delete script_data;
67 }
68}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000069} // namespace internal
70} // namespace v8