preparse-data.h 4.82 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_PREPARSE_DATA_H_
#define V8_PREPARSE_DATA_H_
7

8 9
#include "src/allocation.h"
#include "src/hashmap.h"
10
#include "src/preparse-data-format.h"
11
#include "src/utils-inl.h"
12 13 14 15

namespace v8 {
namespace internal {

16 17
class ScriptData;

18 19 20 21

// Abstract interface for preparse data recorder.
class ParserRecorder {
 public:
22
  ParserRecorder() { }
23 24 25 26 27 28
  virtual ~ParserRecorder() { }

  // Logs the scope and some details of a function literal in the source.
  virtual void LogFunction(int start,
                           int end,
                           int literals,
29
                           int properties,
30
                           StrictMode strict_mode) = 0;
31 32 33 34 35 36 37

  // Logs an error message and marks the log as containing an error.
  // Further logging will be ignored, and ExtractData will return a vector
  // representing the error only.
  virtual void LogMessage(int start,
                          int end,
                          const char* message,
38 39
                          const char* argument_opt,
                          bool is_reference_error) = 0;
40 41 42
 private:
  DISALLOW_COPY_AND_ASSIGN(ParserRecorder);
};
43 44


45 46
class SingletonLogger : public ParserRecorder {
 public:
47 48 49
  SingletonLogger()
      : has_error_(false), start_(-1), end_(-1), is_reference_error_(false) {}
  virtual ~SingletonLogger() {}
50

51
  void Reset() { has_error_ = false; }
52

53 54 55 56 57
  virtual void LogFunction(int start,
                           int end,
                           int literals,
                           int properties,
                           StrictMode strict_mode) {
58
    DCHECK(!has_error_);
59 60 61 62 63
    start_ = start;
    end_ = end;
    literals_ = literals;
    properties_ = properties;
    strict_mode_ = strict_mode;
64
  }
65

66 67 68 69 70 71
  // Logs an error message and marks the log as containing an error.
  // Further logging will be ignored, and ExtractData will return a vector
  // representing the error only.
  virtual void LogMessage(int start,
                          int end,
                          const char* message,
72 73
                          const char* argument_opt,
                          bool is_reference_error) {
74 75 76 77 78 79
    if (has_error_) return;
    has_error_ = true;
    start_ = start;
    end_ = end;
    message_ = message;
    argument_opt_ = argument_opt;
80
    is_reference_error_ = is_reference_error;
81 82
  }

83
  bool has_error() const { return has_error_; }
84

85 86 87
  int start() const { return start_; }
  int end() const { return end_; }
  int literals() const {
88
    DCHECK(!has_error_);
89 90
    return literals_;
  }
91
  int properties() const {
92
    DCHECK(!has_error_);
93 94
    return properties_;
  }
95
  StrictMode strict_mode() const {
96
    DCHECK(!has_error_);
97 98
    return strict_mode_;
  }
99
  int is_reference_error() const { return is_reference_error_; }
100
  const char* message() {
101
    DCHECK(has_error_);
102 103
    return message_;
  }
104
  const char* argument_opt() const {
105
    DCHECK(has_error_);
106 107
    return argument_opt_;
  }
108

109 110 111 112 113 114 115 116 117 118 119
 private:
  bool has_error_;
  int start_;
  int end_;
  // For function entries.
  int literals_;
  int properties_;
  StrictMode strict_mode_;
  // For error messages.
  const char* message_;
  const char* argument_opt_;
120
  bool is_reference_error_;
121
};
122 123


124
class CompleteParserRecorder : public ParserRecorder {
125
 public:
126 127 128 129 130 131 132
  struct Key {
    bool is_one_byte;
    Vector<const byte> literal_bytes;
  };

  CompleteParserRecorder();
  virtual ~CompleteParserRecorder() {}
133

134 135 136 137
  virtual void LogFunction(int start,
                           int end,
                           int literals,
                           int properties,
138
                           StrictMode strict_mode) {
139 140 141 142
    function_store_.Add(start);
    function_store_.Add(end);
    function_store_.Add(literals);
    function_store_.Add(properties);
143
    function_store_.Add(strict_mode);
144 145 146 147 148 149 150 151
  }

  // Logs an error message and marks the log as containing an error.
  // Further logging will be ignored, and ExtractData will return a vector
  // representing the error only.
  virtual void LogMessage(int start,
                          int end,
                          const char* message,
152 153
                          const char* argument_opt,
                          bool is_reference_error_);
154
  ScriptData* GetScriptData();
155

156
  bool HasError() {
157 158
    return static_cast<bool>(preamble_[PreparseDataConstants::kHasErrorOffset]);
  }
159
  Vector<unsigned> ErrorMessageData() {
160
    DCHECK(HasError());
161 162
    return function_store_.ToVector();
  }
163

164
 private:
165 166
  void WriteString(Vector<const char> str);

167 168 169
  // Write a non-negative number to the symbol store.
  void WriteNumber(int number);

170 171 172 173 174 175 176 177 178 179 180
  Collector<unsigned> function_store_;
  unsigned preamble_[PreparseDataConstants::kHeaderSize];

#ifdef DEBUG
  int prev_start_;
#endif
};


} }  // namespace v8::internal.

181
#endif  // V8_PREPARSE_DATA_H_