Line data Source code
1 : // Copyright 2016 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_SOURCE_POSITION_TABLE_H_
6 : #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 : namespace v8 {
15 : namespace internal {
16 :
17 : class ByteArray;
18 : template <typename T>
19 : class Handle;
20 : class Isolate;
21 : class Zone;
22 :
23 : struct PositionTableEntry {
24 : PositionTableEntry()
25 126559966 : : code_offset(0), source_position(0), is_statement(false) {}
26 : PositionTableEntry(int offset, int64_t source, bool statement)
27 35936532 : : code_offset(offset), source_position(source), is_statement(statement) {}
28 :
29 : int code_offset;
30 : int64_t source_position;
31 : bool is_statement;
32 : };
33 :
34 5239200 : class V8_EXPORT_PRIVATE SourcePositionTableBuilder {
35 : public:
36 : enum RecordingMode {
37 : // Indicates that source positions are never to be generated. (Resulting in
38 : // an empty table).
39 : OMIT_SOURCE_POSITIONS,
40 : // Indicates that source positions are not currently required, but may be
41 : // generated later.
42 : LAZY_SOURCE_POSITIONS,
43 : // Indicates that source positions should be immediately generated.
44 : RECORD_SOURCE_POSITIONS
45 : };
46 :
47 : explicit SourcePositionTableBuilder(
48 : RecordingMode mode = RECORD_SOURCE_POSITIONS);
49 :
50 : void AddPosition(size_t code_offset, SourcePosition source_position,
51 : bool is_statement);
52 :
53 : Handle<ByteArray> ToSourcePositionTable(Isolate* isolate);
54 : OwnedVector<byte> ToSourcePositionTableVector();
55 :
56 : inline bool Omit() const { return mode_ != RECORD_SOURCE_POSITIONS; }
57 : inline bool Lazy() const { return mode_ == LAZY_SOURCE_POSITIONS; }
58 :
59 : private:
60 : void AddEntry(const PositionTableEntry& entry);
61 :
62 : RecordingMode mode_;
63 : std::vector<byte> bytes_;
64 : #ifdef ENABLE_SLOW_DCHECKS
65 : std::vector<PositionTableEntry> raw_entries_;
66 : #endif
67 : PositionTableEntry previous_; // Previously written entry, to compute delta.
68 : };
69 :
70 : class V8_EXPORT_PRIVATE SourcePositionTableIterator {
71 : public:
72 : enum IterationFilter { kJavaScriptOnly = 0, kExternalOnly = 1, kAll = 2 };
73 :
74 : // Used for saving/restoring the iterator.
75 : struct IndexAndPositionState {
76 : int index_;
77 : PositionTableEntry position_;
78 : IterationFilter filter_;
79 : };
80 :
81 : // We expose three flavours of the iterator, depending on the argument passed
82 : // to the constructor:
83 :
84 : // Handlified iterator allows allocation, but it needs a handle (and thus
85 : // a handle scope). This is the preferred version.
86 : explicit SourcePositionTableIterator(
87 : Handle<ByteArray> byte_array, IterationFilter filter = kJavaScriptOnly);
88 :
89 : // Non-handlified iterator does not need a handle scope, but it disallows
90 : // allocation during its lifetime. This is useful if there is no handle
91 : // scope around.
92 : explicit SourcePositionTableIterator(
93 : ByteArray byte_array, IterationFilter filter = kJavaScriptOnly);
94 :
95 : // Handle-safe iterator based on an a vector located outside the garbage
96 : // collected heap, allows allocation during its lifetime.
97 : explicit SourcePositionTableIterator(
98 : Vector<const byte> bytes, IterationFilter filter = kJavaScriptOnly);
99 :
100 : void Advance();
101 :
102 : int code_offset() const {
103 : DCHECK(!done());
104 : return current_.code_offset;
105 : }
106 : SourcePosition source_position() const {
107 : DCHECK(!done());
108 : return SourcePosition::FromRaw(current_.source_position);
109 : }
110 : bool is_statement() const {
111 : DCHECK(!done());
112 : return current_.is_statement;
113 : }
114 : bool done() const { return index_ == kDone; }
115 :
116 860 : IndexAndPositionState GetState() const { return {index_, current_, filter_}; }
117 :
118 : void RestoreState(const IndexAndPositionState& saved_state) {
119 860 : index_ = saved_state.index_;
120 860 : current_ = saved_state.position_;
121 860 : filter_ = saved_state.filter_;
122 : }
123 :
124 : private:
125 : static const int kDone = -1;
126 :
127 : Vector<const byte> raw_table_;
128 : Handle<ByteArray> table_;
129 : int index_ = 0;
130 : PositionTableEntry current_;
131 : IterationFilter filter_;
132 : DISALLOW_HEAP_ALLOCATION(no_gc)
133 : };
134 :
135 : } // namespace internal
136 : } // namespace v8
137 :
138 : #endif // V8_SOURCE_POSITION_TABLE_H_
|