background-parsing-task.cc 2.64 KB
Newer Older
1 2 3 4 5
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/background-parsing-task.h"
6

7
#include "src/objects-inl.h"
8
#include "src/parsing/parser.h"
9 10 11 12

namespace v8 {
namespace internal {

13 14 15 16 17
void StreamedSource::Release() {
  parser.reset();
  info.reset();
}

18 19 20
BackgroundParsingTask::BackgroundParsingTask(
    StreamedSource* source, ScriptCompiler::CompileOptions options,
    int stack_size, Isolate* isolate)
21
    : source_(source), stack_size_(stack_size), script_data_(nullptr) {
22 23 24 25 26 27
  // We don't set the context to the CompilationInfo yet, because the background
  // thread cannot do anything with it anyway. We set it just before compilation
  // on the foreground thread.
  DCHECK(options == ScriptCompiler::kProduceParserCache ||
         options == ScriptCompiler::kProduceCodeCache ||
         options == ScriptCompiler::kNoCompileOptions);
28

29 30
  // Prepare the data for the internalization phase and compilation phase, which
  // will happen in the main thread after parsing.
31
  ParseInfo* info = new ParseInfo(isolate->allocator());
32
  info->InitFromIsolate(isolate);
verwaest's avatar
verwaest committed
33
  info->set_toplevel();
34
  source->info.reset(info);
35 36 37 38
  info->set_source_stream(source->source_stream.get());
  info->set_source_stream_encoding(source->encoding);
  info->set_unicode_cache(&source_->unicode_cache);
  info->set_compile_options(options);
39
  info->set_allow_lazy_parsing();
40 41 42
  if (V8_UNLIKELY(FLAG_runtime_stats)) {
    info->set_runtime_call_stats(new (info->zone()) RuntimeCallStats());
  }
43

44
  source_->info->set_cached_data(&script_data_);
45 46 47
  // Parser needs to stay alive for finalizing the parsing on the main
  // thread.
  source_->parser.reset(new Parser(source_->info.get()));
48
  source_->parser->DeserializeScopeChain(source_->info.get(),
49
                                         MaybeHandle<ScopeInfo>());
50 51 52 53 54 55 56 57
}


void BackgroundParsingTask::Run() {
  DisallowHeapAllocation no_allocation;
  DisallowHandleAllocation no_handles;
  DisallowHandleDereference no_deref;

58 59
  // Reset the stack limit of the parser to reflect correctly that we're on a
  // background thread.
60
  uintptr_t stack_limit = GetCurrentStackPosition() - stack_size_ * KB;
61
  source_->parser->set_stack_limit(stack_limit);
62

63
  source_->parser->ParseOnBackground(source_->info.get());
64

65
  if (script_data_ != nullptr) {
66
    source_->cached_data.reset(new ScriptCompiler::CachedData(
67
        script_data_->data(), script_data_->length(),
68
        ScriptCompiler::CachedData::BufferOwned));
69 70 71
    script_data_->ReleaseDataOwnership();
    delete script_data_;
    script_data_ = nullptr;
72 73
  }
}
74 75
}  // namespace internal
}  // namespace v8