1 // Copyright 2015 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 #include "src/interpreter/bytecode-array-random-iterator.h" 6 #include "src/objects/code-inl.h" 7 #include "src/objects/objects-inl.h" 8 9 namespace v8 { 10 namespace internal { 11 namespace interpreter { 12 BytecodeArrayRandomIterator(Handle<BytecodeArray> bytecode_array,Zone * zone)13BytecodeArrayRandomIterator::BytecodeArrayRandomIterator( 14 Handle<BytecodeArray> bytecode_array, Zone* zone) 15 : BytecodeArrayIterator(bytecode_array, 0), offsets_(zone) { 16 offsets_.reserve(bytecode_array->length() / 2); 17 Initialize(); 18 } 19 Initialize()20void BytecodeArrayRandomIterator::Initialize() { 21 // Run forwards through the bytecode array to determine the offset of each 22 // bytecode. 23 while (!done()) { 24 offsets_.push_back(current_offset()); 25 Advance(); 26 } 27 GoToStart(); 28 } 29 IsValid() const30bool BytecodeArrayRandomIterator::IsValid() const { 31 return current_index_ >= 0 && 32 static_cast<size_t>(current_index_) < offsets_.size(); 33 } 34 UpdateOffsetFromIndex()35void BytecodeArrayRandomIterator::UpdateOffsetFromIndex() { 36 if (IsValid()) { 37 SetOffset(offsets_[current_index_]); 38 } 39 } 40 41 } // namespace interpreter 42 } // namespace internal 43 } // namespace v8 44