1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef vm_BytecodeIterator_h
8 #define vm_BytecodeIterator_h
9 
10 #include "vm/BytecodeLocation.h"
11 
12 namespace js {
13 
14 class BytecodeIterator {
15   BytecodeLocation current_;
16 
17  public:
18   inline explicit BytecodeIterator(const JSScript* script);
19 
BytecodeIterator(BytecodeLocation loc)20   explicit BytecodeIterator(BytecodeLocation loc) : current_(loc) {}
21 
22   BytecodeIterator& operator=(const BytecodeIterator&) = default;
23 
24   bool operator==(const BytecodeIterator& other) const {
25     return other.current_ == current_;
26   }
27 
28   bool operator!=(const BytecodeIterator& other) const {
29     return !(other.current_ == current_);
30   }
31 
32   const BytecodeLocation& operator*() const { return current_; }
33 
34   const BytecodeLocation* operator->() const { return &current_; }
35 
36   // Pre-increment
37   BytecodeIterator& operator++() {
38     current_ = current_.next();
39     return *this;
40   }
41 
42   // Post-increment
43   BytecodeIterator operator++(int) {
44     BytecodeIterator previous(*this);
45     current_ = current_.next();
46     return previous;
47   }
48 };
49 
50 // Given a JSScript, allow the construction of a range based for-loop
51 // that will visit all script locations in that script.
52 class AllBytecodesIterable {
53   const JSScript* script_;
54 
55  public:
AllBytecodesIterable(const JSScript * script)56   explicit AllBytecodesIterable(const JSScript* script) : script_(script) {}
57 
58   BytecodeIterator begin();
59   BytecodeIterator end();
60 };
61 
62 // Construct a range based iterator that will visit all bytecode locations
63 // between two given bytecode locations.
64 // `beginLoc_` is the bytecode location where the iterator will start, and
65 // `endLoc_` is the bytecode location where the iterator will end.
66 class BytecodeLocationRange {
67   BytecodeLocation beginLoc_;
68   BytecodeLocation endLoc_;
69 
70  public:
BytecodeLocationRange(BytecodeLocation beginLoc,BytecodeLocation endLoc)71   explicit BytecodeLocationRange(BytecodeLocation beginLoc,
72                                  BytecodeLocation endLoc)
73       : beginLoc_(beginLoc), endLoc_(endLoc) {
74 #ifdef DEBUG
75     MOZ_ASSERT(beginLoc.hasSameScript(endLoc));
76 #endif
77   }
78 
79   BytecodeIterator begin();
80   BytecodeIterator end();
81 };
82 
83 }  // namespace js
84 
85 #endif
86