preparse-data.h 3.22 KB
Newer Older
1
// Copyright 2011 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

5 6
#ifndef V8_PARSING_PREPARSE_DATA_H_
#define V8_PARSING_PREPARSE_DATA_H_
7

8 9
#include <unordered_map>

10
#include "src/allocation.h"
lpy's avatar
lpy committed
11
#include "src/base/hashmap.h"
12
#include "src/collector.h"
13
#include "src/messages.h"
14
#include "src/parsing/preparse-data-format.h"
15 16 17 18

namespace v8 {
namespace internal {

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
class ScriptData {
 public:
  ScriptData(const byte* data, int length);
  ~ScriptData() {
    if (owns_data_) DeleteArray(data_);
  }

  const byte* data() const { return data_; }
  int length() const { return length_; }
  bool rejected() const { return rejected_; }

  void Reject() { rejected_ = true; }

  void AcquireDataOwnership() {
    DCHECK(!owns_data_);
    owns_data_ = true;
  }

  void ReleaseDataOwnership() {
    DCHECK(owns_data_);
    owns_data_ = false;
  }

 private:
  bool owns_data_ : 1;
  bool rejected_ : 1;
  const byte* data_;
  int length_;

  DISALLOW_COPY_AND_ASSIGN(ScriptData);
};
50

51
class PreParserLogger final {
52
 public:
53
  PreParserLogger()
54
      : end_(-1),
55
        num_parameters_(-1),
56
        num_inner_functions_(-1) {}
57

58
  void LogFunction(int end, int num_parameters, int num_inner_functions) {
59
    end_ = end;
60
    num_parameters_ = num_parameters;
61
    num_inner_functions_ = num_inner_functions;
62
  }
63

64
  int end() const { return end_; }
65 66 67
  int num_parameters() const {
    return num_parameters_;
  }
68
  int num_inner_functions() const { return num_inner_functions_; }
69

70 71 72
 private:
  int end_;
  // For function entries.
73
  int num_parameters_;
74
  int num_inner_functions_;
75
};
76

77
class ParserLogger final {
78
 public:
79
  ParserLogger();
80

81
  void LogFunction(int start, int end, int num_parameters,
82
                   LanguageMode language_mode, bool uses_super_property,
83
                   int num_inner_functions);
84

85
  ScriptData* GetScriptData();
86

87
 private:
88 89 90 91 92 93 94 95
  Collector<unsigned> function_store_;
  unsigned preamble_[PreparseDataConstants::kHeaderSize];

#ifdef DEBUG
  int prev_start_;
#endif
};

96 97 98 99 100 101
class PreParseData final {
 public:
  struct FunctionData {
    int end;
    int num_parameters;
    int num_inner_functions;
102
    LanguageMode language_mode;
103
    bool uses_super_property : 1;
104

105
    FunctionData() : end(kNoSourcePosition) {}
106

107
    FunctionData(int end, int num_parameters, int num_inner_functions,
108
                 LanguageMode language_mode, bool uses_super_property)
109
        : end(end),
110
          num_parameters(num_parameters),
111 112
          num_inner_functions(num_inner_functions),
          language_mode(language_mode),
113
          uses_super_property(uses_super_property) {}
114

115 116 117 118
    bool is_valid() const {
      DCHECK_IMPLIES(end < 0, end == kNoSourcePosition);
      return end != kNoSourcePosition;
    }
119 120
  };

121 122 123 124 125 126 127 128
  FunctionData GetFunctionData(int start) const;
  void AddFunctionData(int start, FunctionData&& data);
  void AddFunctionData(int start, const FunctionData& data);
  size_t size() const;

  typedef std::unordered_map<int, FunctionData>::const_iterator const_iterator;
  const_iterator begin() const;
  const_iterator end() const;
129 130

 private:
131
  std::unordered_map<int, FunctionData> functions_;
132
};
133

134 135
}  // namespace internal
}  // namespace v8.
136

137
#endif  // V8_PARSING_PREPARSE_DATA_H_