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 { PositionTableEntryPositionTableEntry24 PositionTableEntry() 25 : code_offset(0), source_position(0), is_statement(false) {} PositionTableEntryPositionTableEntry26 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 bool is_statement; 32 }; 33 34 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 private: 48 void AddEntry(const PositionTableEntry& entry); 49 Omit()50 inline bool Omit() const { return mode_ == OMIT_SOURCE_POSITIONS; } 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 // Used for saving/restoring the iterator. 63 struct IndexAndPosition { 64 int index_; 65 PositionTableEntry position_; 66 }; 67 68 // We expose three flavours of the iterator, depending on the argument passed 69 // to the constructor: 70 71 // Handlified iterator allows allocation, but it needs a handle (and thus 72 // a handle scope). This is the preferred version. 73 explicit SourcePositionTableIterator(Handle<ByteArray> byte_array); 74 75 // Non-handlified iterator does not need a handle scope, but it disallows 76 // allocation during its lifetime. This is useful if there is no handle 77 // scope around. 78 explicit SourcePositionTableIterator(ByteArray* byte_array); 79 80 // Handle-safe iterator based on an a vector located outside the garbage 81 // collected heap, allows allocation during its lifetime. 82 explicit SourcePositionTableIterator(Vector<const byte> bytes); 83 84 void Advance(); 85 code_offset()86 int code_offset() const { 87 DCHECK(!done()); 88 return current_.code_offset; 89 } source_position()90 SourcePosition source_position() const { 91 DCHECK(!done()); 92 return SourcePosition::FromRaw(current_.source_position); 93 } is_statement()94 bool is_statement() const { 95 DCHECK(!done()); 96 return current_.is_statement; 97 } done()98 bool done() const { return index_ == kDone; } 99 GetState()100 IndexAndPosition GetState() const { return {index_, current_}; } 101 RestoreState(const IndexAndPosition & saved_state)102 void RestoreState(const IndexAndPosition& saved_state) { 103 index_ = saved_state.index_; 104 current_ = saved_state.position_; 105 } 106 107 private: 108 static const int kDone = -1; 109 110 Vector<const byte> raw_table_; 111 Handle<ByteArray> table_; 112 int index_ = 0; 113 PositionTableEntry current_; 114 DisallowHeapAllocation no_gc; 115 }; 116 117 } // namespace internal 118 } // namespace v8 119 120 #endif // V8_SOURCE_POSITION_TABLE_H_ 121