|
2 | 2 | * Various types to support iteration. |
3 | 3 | */ |
4 | 4 |
|
5 | | -use super::objbool; |
6 | 5 | use crate::pyobject::{ |
7 | 6 | PyContext, PyFuncArgs, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol, |
8 | 7 | }; |
9 | 8 | use crate::vm::VirtualMachine; |
10 | | -// use super::objstr; |
11 | | -use super::objtype; // Required for arg_check! to use isinstance |
| 9 | + |
| 10 | +use super::objbool; |
| 11 | +use super::objbytearray::PyByteArray; |
| 12 | +use super::objbytes::PyBytes; |
| 13 | +use super::objrange::PyRange; |
| 14 | +use super::objtype; |
12 | 15 |
|
13 | 16 | /* |
14 | 17 | * This helper function is called at multiple places. First, it is called |
@@ -129,38 +132,43 @@ fn iter_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { |
129 | 132 | iterated_obj: ref iterated_obj_ref, |
130 | 133 | } = iter.payload |
131 | 134 | { |
132 | | - match iterated_obj_ref.payload { |
133 | | - PyObjectPayload::Sequence { ref elements } => { |
134 | | - if position.get() < elements.borrow().len() { |
135 | | - let obj_ref = elements.borrow()[position.get()].clone(); |
136 | | - position.set(position.get() + 1); |
137 | | - Ok(obj_ref) |
138 | | - } else { |
139 | | - Err(new_stop_iteration(vm)) |
140 | | - } |
| 135 | + if let Some(range) = iterated_obj_ref.payload::<PyRange>() { |
| 136 | + if let Some(int) = range.get(position.get()) { |
| 137 | + position.set(position.get() + 1); |
| 138 | + Ok(vm.ctx.new_int(int)) |
| 139 | + } else { |
| 140 | + Err(new_stop_iteration(vm)) |
141 | 141 | } |
142 | | - |
143 | | - PyObjectPayload::Range { ref range } => { |
144 | | - if let Some(int) = range.get(position.get()) { |
145 | | - position.set(position.get() + 1); |
146 | | - Ok(vm.ctx.new_int(int)) |
147 | | - } else { |
148 | | - Err(new_stop_iteration(vm)) |
149 | | - } |
| 142 | + } else if let Some(bytes) = iterated_obj_ref.payload::<PyBytes>() { |
| 143 | + if position.get() < bytes.len() { |
| 144 | + let obj_ref = vm.ctx.new_int(bytes[position.get()]); |
| 145 | + position.set(position.get() + 1); |
| 146 | + Ok(obj_ref) |
| 147 | + } else { |
| 148 | + Err(new_stop_iteration(vm)) |
150 | 149 | } |
151 | | - |
152 | | - PyObjectPayload::Bytes { ref value } => { |
153 | | - if position.get() < value.borrow().len() { |
154 | | - let obj_ref = vm.ctx.new_int(value.borrow()[position.get()]); |
155 | | - position.set(position.get() + 1); |
156 | | - Ok(obj_ref) |
157 | | - } else { |
158 | | - Err(new_stop_iteration(vm)) |
159 | | - } |
| 150 | + } else if let Some(bytes) = iterated_obj_ref.payload::<PyByteArray>() { |
| 151 | + if position.get() < bytes.value.borrow().len() { |
| 152 | + let obj_ref = vm.ctx.new_int(bytes.value.borrow()[position.get()]); |
| 153 | + position.set(position.get() + 1); |
| 154 | + Ok(obj_ref) |
| 155 | + } else { |
| 156 | + Err(new_stop_iteration(vm)) |
160 | 157 | } |
161 | | - |
162 | | - _ => { |
163 | | - panic!("NOT IMPL"); |
| 158 | + } else { |
| 159 | + match iterated_obj_ref.payload { |
| 160 | + PyObjectPayload::Sequence { ref elements } => { |
| 161 | + if position.get() < elements.borrow().len() { |
| 162 | + let obj_ref = elements.borrow()[position.get()].clone(); |
| 163 | + position.set(position.get() + 1); |
| 164 | + Ok(obj_ref) |
| 165 | + } else { |
| 166 | + Err(new_stop_iteration(vm)) |
| 167 | + } |
| 168 | + } |
| 169 | + _ => { |
| 170 | + panic!("NOT IMPL"); |
| 171 | + } |
164 | 172 | } |
165 | 173 | } |
166 | 174 | } else { |
|
0 commit comments