Skip to content

Commit de9c11c

Browse files
committed
Move nxt function from pyobject to objiter file. Also fix formatting issues.
1 parent 0acfc10 commit de9c11c

File tree

4 files changed

+45
-74
lines changed

4 files changed

+45
-74
lines changed

vm/src/builtins.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use std::collections::HashMap;
44
use std::io::{self, Write};
55

66
use super::compile;
7+
use super::obj::objiter;
78
use super::obj::objstr;
89
use super::obj::objtype;
9-
use super::obj::objiter;
1010
use super::objbool;
1111
use super::pyobject::{
1212
AttributeProtocol, DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind,
@@ -223,11 +223,7 @@ fn builtin_issubclass(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
223223
}
224224

225225
fn builtin_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
226-
arg_check!(
227-
vm,
228-
args,
229-
required = [(iter_target, None)]
230-
);
226+
arg_check!(vm, args, required = [(iter_target, None)]);
231227
objiter::get_iter(vm, iter_target)
232228
}
233229

@@ -409,10 +405,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
409405
String::from("issubclass"),
410406
ctx.new_rustfunc(builtin_issubclass),
411407
);
412-
dict.insert(
413-
String::from("iter"),
414-
ctx.new_rustfunc(builtin_iter),
415-
);
408+
dict.insert(String::from("iter"), ctx.new_rustfunc(builtin_iter));
416409
dict.insert(String::from("len"), ctx.new_rustfunc(builtin_len));
417410
dict.insert(String::from("list"), ctx.list_type());
418411
dict.insert(String::from("locals"), ctx.new_rustfunc(builtin_locals));

vm/src/obj/objiter.rs

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@
33
*/
44

55
use super::super::pyobject::{
6-
AttributeProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef,
7-
PyResult, TypeProtocol,
6+
AttributeProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult,
7+
TypeProtocol,
88
};
99
use super::super::vm::VirtualMachine;
1010
use super::objstr;
1111
use super::objtype; // Required for arg_check! to use isinstance
1212

13+
/*
14+
* This helper function is called at multiple places. First, it is called
15+
* in the vm when a for loop is entered. Next, it is used when the builtin
16+
* function 'iter' is called.
17+
*/
1318
pub fn get_iter(vm: &mut VirtualMachine, iter_target: &PyObjectRef) -> PyResult {
1419
// Check what we are going to iterate over:
1520
let iterated_obj = if objtype::isinstance(iter_target, vm.ctx.iter_type()) {
1621
// If object is already an iterator, return that one.
17-
return Ok(iter_target.clone())
22+
return Ok(iter_target.clone());
1823
} else if objtype::isinstance(iter_target, vm.ctx.list_type()) {
1924
iter_target.clone()
2025
} else {
@@ -37,46 +42,45 @@ pub fn get_iter(vm: &mut VirtualMachine, iter_target: &PyObjectRef) -> PyResult
3742

3843
// Sequence iterator:
3944
fn iter_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
40-
arg_check!(
41-
vm,
42-
args,
43-
required = [(iter_target, None)]
44-
);
45+
arg_check!(vm, args, required = [(iter_target, None)]);
4546

4647
get_iter(vm, iter_target)
4748
}
4849

4950
fn iter_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
50-
arg_check!(
51-
vm,
52-
args,
53-
required = [(iter, Some(vm.ctx.iter_type()))]
54-
);
51+
arg_check!(vm, args, required = [(iter, Some(vm.ctx.iter_type()))]);
5552
// Return self:
5653
Ok(iter.clone())
5754
}
5855

5956
fn iter_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
60-
arg_check!(
61-
vm,
62-
args,
63-
required = [(iter, Some(vm.ctx.iter_type()))]
64-
);
57+
arg_check!(vm, args, required = [(iter, Some(vm.ctx.iter_type()))]);
6558

66-
let next_obj: Option<PyObjectRef> = {
67-
// We require a mutable pyobject here to update the iterator:
68-
let mut iterator: &mut PyObject = &mut iter.borrow_mut();
69-
iterator.nxt()
70-
};
71-
72-
// Return next item, or StopIteration
73-
match next_obj {
74-
Some(value) => Ok(value),
75-
None => {
76-
let stop_iteration_type = vm.ctx.exceptions.stop_iteration.clone();
77-
let stop_iteration = vm.new_exception(stop_iteration_type, "End of iterator".to_string());
78-
Err(stop_iteration)
59+
if let PyObjectKind::Iterator {
60+
ref mut position,
61+
iterated_obj: ref iterated_obj_ref,
62+
} = iter.borrow_mut().kind
63+
{
64+
let iterated_obj = &*iterated_obj_ref.borrow_mut();
65+
match iterated_obj.kind {
66+
PyObjectKind::List { ref elements } => {
67+
if *position < elements.len() {
68+
let obj_ref = elements[*position].clone();
69+
*position += 1;
70+
Ok(obj_ref)
71+
} else {
72+
let stop_iteration_type = vm.ctx.exceptions.stop_iteration.clone();
73+
let stop_iteration =
74+
vm.new_exception(stop_iteration_type, "End of iterator".to_string());
75+
Err(stop_iteration)
76+
}
77+
}
78+
_ => {
79+
panic!("NOT IMPL");
80+
}
7981
}
82+
} else {
83+
panic!("NOT IMPL");
8084
}
8185
}
8286

@@ -86,4 +90,3 @@ pub fn init(context: &PyContext) {
8690
iter_type.set_attr("__iter__", context.new_rustfunc(iter_iter));
8791
iter_type.set_attr("__next__", context.new_rustfunc(iter_next));
8892
}
89-

vm/src/pyobject.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -758,35 +758,6 @@ impl PyObject {
758758
}
759759
}
760760

761-
// Implement iterator protocol:
762-
pub fn nxt(&mut self) -> Option<PyObjectRef> {
763-
match self.kind {
764-
PyObjectKind::Iterator {
765-
ref mut position,
766-
iterated_obj: ref iterated_obj_ref,
767-
} => {
768-
let iterated_obj = &*iterated_obj_ref.borrow_mut();
769-
match iterated_obj.kind {
770-
PyObjectKind::List { ref elements } => {
771-
if *position < elements.len() {
772-
let obj_ref = elements[*position].clone();
773-
*position += 1;
774-
Some(obj_ref)
775-
} else {
776-
None
777-
}
778-
}
779-
_ => {
780-
panic!("NOT IMPL");
781-
}
782-
}
783-
}
784-
_ => {
785-
panic!("NOT IMPL");
786-
}
787-
}
788-
}
789-
790761
// Move this object into a reference object, transferring ownership.
791762
pub fn into_ref(self) -> PyObjectRef {
792763
Rc::new(RefCell::new(self))

vm/src/vm.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ impl VirtualMachine {
832832
Ok(iter_obj) => {
833833
self.push_value(iter_obj);
834834
None
835-
},
835+
}
836836
Err(err) => Some(Err(err)),
837837
}
838838
}
@@ -851,12 +851,16 @@ impl VirtualMachine {
851851
}
852852
Err(next_error) => {
853853
// Check if we have stopiteration, or something else:
854-
if objtype::isinstance(&next_error, self.ctx.exceptions.stop_iteration.clone()) {
854+
if objtype::isinstance(
855+
&next_error,
856+
self.ctx.exceptions.stop_iteration.clone(),
857+
) {
855858
// Pop iterator from stack:
856859
self.pop_value();
857860

858861
// End of for loop
859-
let end_label = if let Block::Loop { start: _, end } = self.last_block() {
862+
let end_label = if let Block::Loop { start: _, end } = self.last_block()
863+
{
860864
*end
861865
} else {
862866
panic!("Wrong block type")

0 commit comments

Comments
 (0)