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_INTERPRETER_BYTECODE_ARRAY_RANDOM_ITERATOR_H_ 6 #define V8_INTERPRETER_BYTECODE_ARRAY_RANDOM_ITERATOR_H_ 7 8 #include <memory> 9 10 #include "src/interpreter/bytecode-array-accessor.h" 11 #include "src/zone/zone-containers.h" 12 #include "src/zone/zone.h" 13 14 namespace v8 { 15 namespace internal { 16 namespace interpreter { 17 18 class V8_EXPORT_PRIVATE BytecodeArrayRandomIterator final 19 : public BytecodeArrayAccessor { 20 public: 21 BytecodeArrayRandomIterator( 22 std::unique_ptr<AbstractBytecodeArray> bytecode_array, Zone* zone); 23 24 BytecodeArrayRandomIterator(Handle<BytecodeArray> bytecode_array, Zone* zone); 25 26 BytecodeArrayRandomIterator(const BytecodeArrayRandomIterator&) = delete; 27 BytecodeArrayRandomIterator& operator=(const BytecodeArrayRandomIterator&) = 28 delete; 29 30 BytecodeArrayRandomIterator& operator++() { 31 ++current_index_; 32 UpdateOffsetFromIndex(); 33 return *this; 34 } 35 BytecodeArrayRandomIterator& operator--() { 36 --current_index_; 37 UpdateOffsetFromIndex(); 38 return *this; 39 } 40 41 BytecodeArrayRandomIterator& operator+=(int offset) { 42 current_index_ += offset; 43 UpdateOffsetFromIndex(); 44 return *this; 45 } 46 47 BytecodeArrayRandomIterator& operator-=(int offset) { 48 current_index_ -= offset; 49 UpdateOffsetFromIndex(); 50 return *this; 51 } 52 current_index()53 int current_index() const { return current_index_; } 54 size()55 size_t size() const { return offsets_.size(); } 56 GoToIndex(int index)57 void GoToIndex(int index) { 58 current_index_ = index; 59 UpdateOffsetFromIndex(); 60 } GoToStart()61 void GoToStart() { 62 current_index_ = 0; 63 UpdateOffsetFromIndex(); 64 } GoToEnd()65 void GoToEnd() { 66 DCHECK_LT(offsets_.size() - 1, static_cast<size_t>(INT_MAX)); 67 current_index_ = static_cast<int>(offsets_.size() - 1); 68 UpdateOffsetFromIndex(); 69 } 70 71 bool IsValid() const; 72 73 private: 74 ZoneVector<int> offsets_; 75 int current_index_; 76 77 void Initialize(); 78 void UpdateOffsetFromIndex(); 79 }; 80 81 } // namespace interpreter 82 } // namespace internal 83 } // namespace v8 84 85 #endif // V8_INTERPRETER_BYTECODE_ARRAY_RANDOM_ITERATOR_H_ 86