• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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"
6 #include "src/debug/debug.h"
7 
8 namespace v8 {
9 namespace internal {
10 
BackgroundParsingTask(StreamedSource * source,ScriptCompiler::CompileOptions options,int stack_size,Isolate * isolate)11 BackgroundParsingTask::BackgroundParsingTask(
12     StreamedSource* source, ScriptCompiler::CompileOptions options,
13     int stack_size, Isolate* isolate)
14     : source_(source), stack_size_(stack_size) {
15   // 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);
21 
22   // Prepare the data for the internalization phase and compilation phase, which
23   // will happen in the main thread after parsing.
24   Zone* zone = new Zone();
25   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);
35   info->set_allow_lazy_parsing(true);
36 }
37 
38 
Run()39 void BackgroundParsingTask::Run() {
40   DisallowHeapAllocation no_allocation;
41   DisallowHandleAllocation no_handles;
42   DisallowHandleDereference no_deref;
43 
44   ScriptData* script_data = NULL;
45   ScriptCompiler::CompileOptions options = source_->info->compile_options();
46   if (options == ScriptCompiler::kProduceParserCache ||
47       options == ScriptCompiler::kProduceCodeCache) {
48     source_->info->set_cached_data(&script_data);
49   }
50 
51   uintptr_t stack_limit =
52       reinterpret_cast<uintptr_t>(&stack_limit) - stack_size_ * KB;
53 
54   source_->info->set_stack_limit(stack_limit);
55   // Parser needs to stay alive for finalizing the parsing on the main
56   // thread. Passing &parse_info is OK because Parser doesn't store it.
57   source_->parser.Reset(new Parser(source_->info.get()));
58   source_->parser->ParseOnBackground(source_->info.get());
59 
60   if (script_data != NULL) {
61     source_->cached_data.Reset(new ScriptCompiler::CachedData(
62         script_data->data(), script_data->length(),
63         ScriptCompiler::CachedData::BufferOwned));
64     script_data->ReleaseDataOwnership();
65     delete script_data;
66   }
67 }
68 }  // namespace internal
69 }  // namespace v8
70