Skip to content

Commit e959908

Browse files
Fix iterator
1 parent f10fa6d commit e959908

8 files changed

Lines changed: 28 additions & 25 deletions

File tree

vm/src/obj/objbytes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::cell::RefCell;
1+
use std::cell::{Cell, RefCell};
22
use std::hash::{Hash, Hasher};
33
use std::ops::Deref;
44
use std::ops::DerefMut;
@@ -197,7 +197,7 @@ fn bytes_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
197197

198198
let iter_obj = PyObject::new(
199199
PyObjectPayload::Iterator {
200-
position: 0,
200+
position: Cell::new(0),
201201
iterated_obj: obj.clone(),
202202
},
203203
vm.ctx.iter_type(),

vm/src/obj/objdict.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use std::cell::{Cell, RefCell};
2+
use std::collections::HashMap;
3+
use std::ops::{Deref, DerefMut};
4+
15
use super::objiter;
26
use super::objstr;
37
use super::objtype;
@@ -6,9 +10,6 @@ use crate::pyobject::{
610
TypeProtocol,
711
};
812
use crate::vm::{ReprGuard, VirtualMachine};
9-
use std::cell::RefCell;
10-
use std::collections::HashMap;
11-
use std::ops::{Deref, DerefMut};
1213

1314
// This typedef abstracts the actual dict type used.
1415
// pub type DictContentType = HashMap<usize, Vec<(PyObjectRef, PyObjectRef)>>;
@@ -258,7 +259,7 @@ fn dict_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
258259

259260
let iter_obj = PyObject::new(
260261
PyObjectPayload::Iterator {
261-
position: 0,
262+
position: Cell::new(0),
262263
iterated_obj: key_list,
263264
},
264265
vm.ctx.iter_type(),

vm/src/obj/objiter.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,28 +134,28 @@ fn iter_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
134134
{
135135
match iterated_obj_ref.payload {
136136
PyObjectPayload::Sequence { ref elements } => {
137-
if *position < elements.borrow().len() {
138-
let obj_ref = elements.borrow()[*position].clone();
139-
//*position += 1; // FIXME
137+
if position.get() < elements.borrow().len() {
138+
let obj_ref = elements.borrow()[position.get()].clone();
139+
position.set(position.get() + 1);
140140
Ok(obj_ref)
141141
} else {
142142
Err(new_stop_iteration(vm))
143143
}
144144
}
145145

146146
PyObjectPayload::Range { ref range } => {
147-
if let Some(int) = range.get(*position) {
148-
//*position += 1; // FIXME
147+
if let Some(int) = range.get(position.get()) {
148+
position.set(position.get() + 1);
149149
Ok(vm.ctx.new_int(int))
150150
} else {
151151
Err(new_stop_iteration(vm))
152152
}
153153
}
154154

155155
PyObjectPayload::Bytes { ref value } => {
156-
if *position < value.borrow().len() {
157-
let obj_ref = vm.ctx.new_int(value.borrow()[*position]);
158-
// *position += 1; // FIXME
156+
if position.get() < value.borrow().len() {
157+
let obj_ref = vm.ctx.new_int(value.borrow()[position.get()]);
158+
position.set(position.get() + 1);
159159
Ok(obj_ref)
160160
} else {
161161
Err(new_stop_iteration(vm))

vm/src/obj/objlist.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::cell::RefCell;
1+
use std::cell::{Cell, RefCell};
22

33
use super::objbool;
44
use super::objint;
@@ -381,7 +381,7 @@ fn list_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
381381

382382
let iter_obj = PyObject::new(
383383
PyObjectPayload::Iterator {
384-
position: 0,
384+
position: Cell::new(0),
385385
iterated_obj: list.clone(),
386386
},
387387
vm.ctx.iter_type(),

vm/src/obj/objrange.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use std::cell::Cell;
2+
use std::ops::Mul;
3+
14
use super::objint;
25
use super::objtype;
36
use crate::pyobject::{
@@ -8,7 +11,6 @@ use crate::vm::VirtualMachine;
811
use num_bigint::{BigInt, Sign};
912
use num_integer::Integer;
1013
use num_traits::{One, Signed, ToPrimitive, Zero};
11-
use std::ops::Mul;
1214

1315
#[derive(Debug, Clone)]
1416
pub struct RangeType {
@@ -247,7 +249,7 @@ fn range_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
247249

248250
Ok(PyObject::new(
249251
PyObjectPayload::Iterator {
250-
position: 0,
252+
position: Cell::new(0),
251253
iterated_obj: range.clone(),
252254
},
253255
vm.ctx.iter_type(),
@@ -261,7 +263,7 @@ fn range_reversed(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
261263

262264
Ok(PyObject::new(
263265
PyObjectPayload::Iterator {
264-
position: 0,
266+
position: Cell::new(0),
265267
iterated_obj: PyObject::new(PyObjectPayload::Range { range }, vm.ctx.range_type()),
266268
},
267269
vm.ctx.iter_type(),

vm/src/obj/objset.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Builtin set type with a sequence of unique items.
33
*/
44

5-
use std::cell::RefCell;
5+
use std::cell::{Cell, RefCell};
66
use std::collections::hash_map::DefaultHasher;
77
use std::collections::HashMap;
88
use std::hash::{Hash, Hasher};
@@ -547,7 +547,7 @@ fn set_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
547547
let set_list = vm.ctx.new_list(items);
548548
let iter_obj = PyObject::new(
549549
PyObjectPayload::Iterator {
550-
position: 0,
550+
position: Cell::new(0),
551551
iterated_obj: set_list,
552552
},
553553
vm.ctx.iter_type(),

vm/src/obj/objtuple.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::cell::RefCell;
1+
use std::cell::{Cell, RefCell};
22
use std::hash::{Hash, Hasher};
33

44
use super::objbool;
@@ -151,7 +151,7 @@ fn tuple_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
151151

152152
let iter_obj = PyObject::new(
153153
PyObjectPayload::Iterator {
154-
position: 0,
154+
position: Cell::new(0),
155155
iterated_obj: tuple.clone(),
156156
},
157157
vm.ctx.iter_type(),

vm/src/pyobject.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use num_bigint::BigInt;
3535
use num_bigint::ToBigInt;
3636
use num_complex::Complex64;
3737
use num_traits::{One, Zero};
38-
use std::cell::RefCell;
38+
use std::cell::{Cell, RefCell};
3939
use std::collections::HashMap;
4040
use std::fmt;
4141
use std::rc::{Rc, Weak};
@@ -1186,7 +1186,7 @@ pub enum PyObjectPayload {
11861186
elements: RefCell<HashMap<u64, PyObjectRef>>,
11871187
},
11881188
Iterator {
1189-
position: usize,
1189+
position: Cell<usize>,
11901190
iterated_obj: PyObjectRef,
11911191
},
11921192
EnumerateIterator {

0 commit comments

Comments
 (0)