source-position-table.h 3.68 KB
Newer Older
1 2 3 4
// Copyright 2016 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.

5 6
#ifndef V8_SOURCE_POSITION_TABLE_H_
#define V8_SOURCE_POSITION_TABLE_H_
7 8

#include "src/assert-scope.h"
9
#include "src/checks.h"
10
#include "src/globals.h"
11
#include "src/source-position.h"
12
#include "src/zone/zone-containers.h"
13 14 15 16

namespace v8 {
namespace internal {

17
class ByteArray;
marja's avatar
marja committed
18 19
template <typename T>
class Handle;
20
class Isolate;
21
class Zone;
22

23 24
struct PositionTableEntry {
  PositionTableEntry()
25
      : code_offset(0), source_position(0), is_statement(false) {}
26
  PositionTableEntry(int offset, int64_t source, bool statement)
27
      : code_offset(offset), source_position(source), is_statement(statement) {}
28

29
  int code_offset;
30
  int64_t source_position;
31 32 33
  bool is_statement;
};

34
class V8_EXPORT_PRIVATE SourcePositionTableBuilder {
35
 public:
36 37
  enum RecordingMode { OMIT_SOURCE_POSITIONS, RECORD_SOURCE_POSITIONS };

38 39
  explicit SourcePositionTableBuilder(
      RecordingMode mode = RECORD_SOURCE_POSITIONS);
yangguo's avatar
yangguo committed
40

41 42
  void AddPosition(size_t code_offset, SourcePosition source_position,
                   bool is_statement);
43

44
  Handle<ByteArray> ToSourcePositionTable(Isolate* isolate);
45
  OwnedVector<byte> ToSourcePositionTableVector();
46

47 48
  inline bool Omit() const { return mode_ == OMIT_SOURCE_POSITIONS; }

49
 private:
50
  void AddEntry(const PositionTableEntry& entry);
51

52
  RecordingMode mode_;
53
  std::vector<byte> bytes_;
54
#ifdef ENABLE_SLOW_DCHECKS
55
  std::vector<PositionTableEntry> raw_entries_;
56
#endif
57
  PositionTableEntry previous_;  // Previously written entry, to compute delta.
58 59
};

60
class V8_EXPORT_PRIVATE SourcePositionTableIterator {
61
 public:
62 63
  enum IterationFilter { kJavaScriptOnly = 0, kExternalOnly = 1, kAll = 2 };

64
  // Used for saving/restoring the iterator.
65
  struct IndexAndPositionState {
66 67
    int index_;
    PositionTableEntry position_;
68
    IterationFilter filter_;
69 70
  };

71
  // We expose three flavours of the iterator, depending on the argument passed
72 73 74 75
  // to the constructor:

  // Handlified iterator allows allocation, but it needs a handle (and thus
  // a handle scope). This is the preferred version.
76 77
  explicit SourcePositionTableIterator(
      Handle<ByteArray> byte_array, IterationFilter filter = kJavaScriptOnly);
78 79 80 81

  // Non-handlified iterator does not need a handle scope, but it disallows
  // allocation during its lifetime. This is useful if there is no handle
  // scope around.
82 83
  explicit SourcePositionTableIterator(
      ByteArray byte_array, IterationFilter filter = kJavaScriptOnly);
84

85 86
  // Handle-safe iterator based on an a vector located outside the garbage
  // collected heap, allows allocation during its lifetime.
87 88
  explicit SourcePositionTableIterator(
      Vector<const byte> bytes, IterationFilter filter = kJavaScriptOnly);
89

90 91
  void Advance();

92
  int code_offset() const {
93
    DCHECK(!done());
94
    return current_.code_offset;
95
  }
96
  SourcePosition source_position() const {
97
    DCHECK(!done());
98
    return SourcePosition::FromRaw(current_.source_position);
99 100 101
  }
  bool is_statement() const {
    DCHECK(!done());
102
    return current_.is_statement;
103
  }
104
  bool done() const { return index_ == kDone; }
105

106
  IndexAndPositionState GetState() const { return {index_, current_, filter_}; }
107

108
  void RestoreState(const IndexAndPositionState& saved_state) {
109 110
    index_ = saved_state.index_;
    current_ = saved_state.position_;
111
    filter_ = saved_state.filter_;
112 113
  }

114
 private:
115 116
  static const int kDone = -1;

117
  Vector<const byte> raw_table_;
118 119
  Handle<ByteArray> table_;
  int index_ = 0;
120
  PositionTableEntry current_;
121
  IterationFilter filter_;
122
  DISALLOW_HEAP_ALLOCATION(no_gc)
123 124 125 126 127
};

}  // namespace internal
}  // namespace v8

128
#endif  // V8_SOURCE_POSITION_TABLE_H_