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 126812407 : : code_offset(0), source_position(0), is_statement(false) {}
26 : PositionTableEntry(int offset, int64_t source, bool statement)
27 35975156 : : 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 5228511 : class V8_EXPORT_PRIVATE SourcePositionTableBuilder {
35 : public:
36 : enum RecordingMode { OMIT_SOURCE_POSITIONS, RECORD_SOURCE_POSITIONS };
37 :
38 : explicit SourcePositionTableBuilder(
39 : RecordingMode mode = RECORD_SOURCE_POSITIONS);
40 :
41 : void AddPosition(size_t code_offset, SourcePosition source_position,
42 : bool is_statement);
43 :
44 : Handle<ByteArray> ToSourcePositionTable(Isolate* isolate);
45 : OwnedVector<byte> ToSourcePositionTableVector();
46 :
47 : inline bool Omit() const { return mode_ == OMIT_SOURCE_POSITIONS; }
48 :
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 : enum IterationFilter { kJavaScriptOnly = 0, kExternalOnly = 1, kAll = 2 };
63 :
64 : // Used for saving/restoring the iterator.
65 : struct IndexAndPositionState {
66 : int index_;
67 : PositionTableEntry position_;
68 : IterationFilter filter_;
69 : };
70 :
71 : // We expose three flavours of the iterator, depending on the argument passed
72 : // to the constructor:
73 :
74 : // Handlified iterator allows allocation, but it needs a handle (and thus
75 : // a handle scope). This is the preferred version.
76 : explicit SourcePositionTableIterator(
77 : Handle<ByteArray> byte_array, IterationFilter filter = kJavaScriptOnly);
78 :
79 : // Non-handlified iterator does not need a handle scope, but it disallows
80 : // allocation during its lifetime. This is useful if there is no handle
81 : // scope around.
82 : explicit SourcePositionTableIterator(
83 : ByteArray byte_array, IterationFilter filter = kJavaScriptOnly);
84 :
85 : // Handle-safe iterator based on an a vector located outside the garbage
86 : // collected heap, allows allocation during its lifetime.
87 : explicit SourcePositionTableIterator(
88 : Vector<const byte> bytes, IterationFilter filter = kJavaScriptOnly);
89 :
90 : void Advance();
91 :
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 : bool is_statement() const {
101 : DCHECK(!done());
102 : return current_.is_statement;
103 : }
104 : bool done() const { return index_ == kDone; }
105 :
106 873 : IndexAndPositionState GetState() const { return {index_, current_, filter_}; }
107 :
108 : void RestoreState(const IndexAndPositionState& saved_state) {
109 873 : index_ = saved_state.index_;
110 873 : current_ = saved_state.position_;
111 873 : filter_ = saved_state.filter_;
112 : }
113 :
114 : private:
115 : static const int kDone = -1;
116 :
117 : Vector<const byte> raw_table_;
118 : Handle<ByteArray> table_;
119 : int index_ = 0;
120 : PositionTableEntry current_;
121 : IterationFilter filter_;
122 : DISALLOW_HEAP_ALLOCATION(no_gc)
123 : };
124 :
125 : } // namespace internal
126 : } // namespace v8
127 :
128 : #endif // V8_SOURCE_POSITION_TABLE_H_
|