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-iterator.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 BytecodeArrayIterator { 20 public: 21 BytecodeArrayRandomIterator(Handle<BytecodeArray> bytecode_array, Zone* zone); 22 23 BytecodeArrayRandomIterator(const BytecodeArrayRandomIterator&) = delete; 24 BytecodeArrayRandomIterator& operator=(const BytecodeArrayRandomIterator&) = 25 delete; 26 27 BytecodeArrayRandomIterator& operator++() { 28 ++current_index_; 29 UpdateOffsetFromIndex(); 30 return *this; 31 } 32 BytecodeArrayRandomIterator& operator--() { 33 --current_index_; 34 UpdateOffsetFromIndex(); 35 return *this; 36 } 37 38 BytecodeArrayRandomIterator& operator+=(int offset) { 39 current_index_ += offset; 40 UpdateOffsetFromIndex(); 41 return *this; 42 } 43 44 BytecodeArrayRandomIterator& operator-=(int offset) { 45 current_index_ -= offset; 46 UpdateOffsetFromIndex(); 47 return *this; 48 } 49 current_index()50 int current_index() const { return current_index_; } 51 size()52 int size() const { return static_cast<int>(offsets_.size()); } 53 GoToIndex(int index)54 void GoToIndex(int index) { 55 current_index_ = index; 56 UpdateOffsetFromIndex(); 57 } GoToStart()58 void GoToStart() { 59 current_index_ = 0; 60 UpdateOffsetFromIndex(); 61 } GoToEnd()62 void GoToEnd() { 63 current_index_ = size() - 1; 64 UpdateOffsetFromIndex(); 65 } 66 67 bool IsValid() const; 68 69 private: 70 ZoneVector<int> offsets_; 71 int current_index_; 72 73 void Initialize(); 74 void UpdateOffsetFromIndex(); 75 }; 76 77 } // namespace interpreter 78 } // namespace internal 79 } // namespace v8 80 81 #endif // V8_INTERPRETER_BYTECODE_ARRAY_RANDOM_ITERATOR_H_ 82