preparse-data.cc 2.79 KB
Newer Older
1
// Copyright 2010 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

lpy's avatar
lpy committed
5 6
#include "src/parsing/preparse-data.h"
#include "src/base/hashmap.h"
7
#include "src/base/logging.h"
8
#include "src/globals.h"
9
#include "src/objects-inl.h"
10 11
#include "src/parsing/parser.h"
#include "src/parsing/preparse-data-format.h"
12

13 14 15
namespace v8 {
namespace internal {

16
void ParserLogger::LogFunction(int start, int end, int num_parameters,
17
                               LanguageMode language_mode,
18
                               bool uses_super_property,
19
                               int num_inner_functions) {
20 21 22
  function_store_.Add(start);
  function_store_.Add(end);
  function_store_.Add(num_parameters);
23 24
  function_store_.Add(
      FunctionEntry::EncodeFlags(language_mode, uses_super_property));
25
  function_store_.Add(num_inner_functions);
26
}
27

28
ParserLogger::ParserLogger() {
29 30 31 32 33 34
  preamble_[PreparseDataConstants::kMagicOffset] =
      PreparseDataConstants::kMagicNumber;
  preamble_[PreparseDataConstants::kVersionOffset] =
      PreparseDataConstants::kCurrentVersion;
  preamble_[PreparseDataConstants::kFunctionsSizeOffset] = 0;
  preamble_[PreparseDataConstants::kSizeOffset] = 0;
35
  DCHECK_EQ(4, PreparseDataConstants::kHeaderSize);
36 37 38 39 40
#ifdef DEBUG
  prev_start_ = -1;
#endif
}

41
ScriptData* ParserLogger::GetScriptData() {
42
  int function_size = function_store_.size();
43
  int total_size = PreparseDataConstants::kHeaderSize + function_size;
44
  unsigned* data = NewArray<unsigned>(total_size);
45
  preamble_[PreparseDataConstants::kFunctionsSizeOffset] = function_size;
46
  MemCopy(data, preamble_, sizeof(preamble_));
47
  if (function_size > 0) {
48 49
    function_store_.WriteTo(Vector<unsigned>(
        data + PreparseDataConstants::kHeaderSize, function_size));
50
  }
51
  DCHECK(IsAligned(reinterpret_cast<intptr_t>(data), kPointerAlignment));
52 53 54 55
  ScriptData* result = new ScriptData(reinterpret_cast<byte*>(data),
                                      total_size * sizeof(unsigned));
  result->AcquireDataOwnership();
  return result;
56 57
}

58 59 60
PreParseData::FunctionData PreParseData::GetFunctionData(int start) const {
  auto it = functions_.find(start);
  if (it != functions_.end()) {
61 62 63 64 65
    return it->second;
  }
  return FunctionData();
}

66
void PreParseData::AddFunctionData(int start, FunctionData&& data) {
67
  DCHECK(data.is_valid());
68
  functions_[start] = std::move(data);
69 70
}

71
void PreParseData::AddFunctionData(int start, const FunctionData& data) {
72
  DCHECK(data.is_valid());
73 74 75 76 77 78 79 80 81 82 83
  functions_[start] = data;
}

size_t PreParseData::size() const { return functions_.size(); }

PreParseData::const_iterator PreParseData::begin() const {
  return functions_.begin();
}

PreParseData::const_iterator PreParseData::end() const {
  return functions_.end();
84
}
85

86 87
}  // namespace internal
}  // namespace v8.