Home | History | Annotate | Download | only in src
      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 
      7 #include "src/debug/debug.h"
      8 #include "src/parsing/parser.h"
      9 
     10 namespace v8 {
     11 namespace internal {
     12 
     13 void StreamedSource::Release() {
     14   parser.reset();
     15   info.reset();
     16   zone.reset();
     17 }
     18 
     19 BackgroundParsingTask::BackgroundParsingTask(
     20     StreamedSource* source, ScriptCompiler::CompileOptions options,
     21     int stack_size, Isolate* isolate)
     22     : source_(source), stack_size_(stack_size), script_data_(nullptr) {
     23   // We don't set the context to the CompilationInfo yet, because the background
     24   // thread cannot do anything with it anyway. We set it just before compilation
     25   // on the foreground thread.
     26   DCHECK(options == ScriptCompiler::kProduceParserCache ||
     27          options == ScriptCompiler::kProduceCodeCache ||
     28          options == ScriptCompiler::kNoCompileOptions);
     29 
     30   // Prepare the data for the internalization phase and compilation phase, which
     31   // will happen in the main thread after parsing.
     32   Zone* zone = new Zone(isolate->allocator(), ZONE_NAME);
     33   ParseInfo* info = new ParseInfo(zone);
     34   info->set_toplevel();
     35   source->zone.reset(zone);
     36   source->info.reset(info);
     37   info->set_isolate(isolate);
     38   info->set_source_stream(source->source_stream.get());
     39   info->set_source_stream_encoding(source->encoding);
     40   info->set_hash_seed(isolate->heap()->HashSeed());
     41   info->set_unicode_cache(&source_->unicode_cache);
     42   info->set_compile_options(options);
     43   info->set_allow_lazy_parsing();
     44 
     45   source_->info->set_cached_data(&script_data_);
     46   // Parser needs to stay alive for finalizing the parsing on the main
     47   // thread.
     48   source_->parser.reset(new Parser(source_->info.get()));
     49   source_->parser->DeserializeScopeChain(source_->info.get(),
     50                                          MaybeHandle<ScopeInfo>());
     51 }
     52 
     53 
     54 void BackgroundParsingTask::Run() {
     55   DisallowHeapAllocation no_allocation;
     56   DisallowHandleAllocation no_handles;
     57   DisallowHandleDereference no_deref;
     58 
     59   // Reset the stack limit of the parser to reflect correctly that we're on a
     60   // background thread.
     61   uintptr_t stack_limit = GetCurrentStackPosition() - stack_size_ * KB;
     62   source_->parser->set_stack_limit(stack_limit);
     63 
     64   // Nullify the Isolate temporarily so that the background parser doesn't
     65   // accidentally use it.
     66   Isolate* isolate = source_->info->isolate();
     67   source_->info->set_isolate(nullptr);
     68 
     69   source_->parser->ParseOnBackground(source_->info.get());
     70 
     71   if (script_data_ != nullptr) {
     72     source_->cached_data.reset(new ScriptCompiler::CachedData(
     73         script_data_->data(), script_data_->length(),
     74         ScriptCompiler::CachedData::BufferOwned));
     75     script_data_->ReleaseDataOwnership();
     76     delete script_data_;
     77     script_data_ = nullptr;
     78   }
     79   source_->info->set_isolate(isolate);
     80 }
     81 }  // namespace internal
     82 }  // namespace v8
     83