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
9namespace v8 {
10namespace internal {
11namespace interpreter {
12
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
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
30bool BytecodeArrayRandomIterator::IsValid() const {
31  return current_index_ >= 0 &&
32         static_cast<size_t>(current_index_) < offsets_.size();
33}
34
35void BytecodeArrayRandomIterator::UpdateOffsetFromIndex() {
36  if (IsValid()) {
37    SetOffset(offsets_[current_index_]);
38  }
39}
40
41}  // namespace interpreter
42}  // namespace internal
43}  // namespace v8
44