forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjiter.rs
More file actions
187 lines (168 loc) · 6.08 KB
/
objiter.rs
File metadata and controls
187 lines (168 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Various types to support iteration.
*/
use crate::function::PyFuncArgs;
use crate::pyobject::{PyContext, PyIteratorValue, PyObjectRef, PyResult, TypeProtocol};
use crate::vm::VirtualMachine;
use super::objbool;
use super::objbytearray::PyByteArray;
use super::objbytes::PyBytes;
use super::objrange::PyRange;
use super::objsequence;
use super::objtype;
use crate::obj::objtype::PyClassRef;
/*
* This helper function is called at multiple places. First, it is called
* in the vm when a for loop is entered. Next, it is used when the builtin
* function 'iter' is called.
*/
pub fn get_iter(vm: &VirtualMachine, iter_target: &PyObjectRef) -> PyResult {
vm.call_method(iter_target, "__iter__", vec![])
// let type_str = objstr::get_value(&vm.to_str(iter_target.class()).unwrap());
// let type_error = vm.new_type_error(format!("Cannot iterate over {}", type_str));
// return Err(type_error);
}
pub fn call_next(vm: &VirtualMachine, iter_obj: &PyObjectRef) -> PyResult {
vm.call_method(iter_obj, "__next__", vec![])
}
/*
* Helper function to retrieve the next object (or none) from an iterator.
*/
pub fn get_next_object(
vm: &VirtualMachine,
iter_obj: &PyObjectRef,
) -> PyResult<Option<PyObjectRef>> {
let next_obj: PyResult = call_next(vm, iter_obj);
match next_obj {
Ok(value) => Ok(Some(value)),
Err(next_error) => {
// Check if we have stopiteration, or something else:
if objtype::isinstance(&next_error, &vm.ctx.exceptions.stop_iteration) {
Ok(None)
} else {
Err(next_error)
}
}
}
}
/* Retrieve all elements from an iterator */
pub fn get_all(vm: &VirtualMachine, iter_obj: &PyObjectRef) -> PyResult<Vec<PyObjectRef>> {
let mut elements = vec![];
loop {
let element = get_next_object(vm, iter_obj)?;
match element {
Some(v) => elements.push(v),
None => break,
}
}
Ok(elements)
}
pub fn new_stop_iteration(vm: &VirtualMachine) -> PyObjectRef {
let stop_iteration_type = vm.ctx.exceptions.stop_iteration.clone();
vm.new_exception(stop_iteration_type, "End of iterator".to_string())
}
fn contains(vm: &VirtualMachine, args: PyFuncArgs, iter_type: PyClassRef) -> PyResult {
arg_check!(
vm,
args,
required = [(iter, Some(iter_type)), (needle, None)]
);
loop {
if let Some(element) = get_next_object(vm, iter)? {
let equal = vm._eq(needle.clone(), element.clone())?;
if objbool::get_value(&equal) {
return Ok(vm.new_bool(true));
} else {
continue;
}
} else {
return Ok(vm.new_bool(false));
}
}
}
/// Common setup for iter types, adds __iter__ and __contains__ methods
pub fn iter_type_init(context: &PyContext, iter_type: &PyClassRef) {
let contains_func = {
let cloned_iter_type = iter_type.clone();
move |vm: &VirtualMachine, args: PyFuncArgs| contains(vm, args, cloned_iter_type.clone())
};
let iter_func = {
let cloned_iter_type = iter_type.clone();
move |vm: &VirtualMachine, args: PyFuncArgs| {
arg_check!(
vm,
args,
required = [(iter, Some(cloned_iter_type.clone()))]
);
// Return self:
Ok(iter.clone())
}
};
extend_class!(context, iter_type, {
"__contains__" => context.new_rustfunc(contains_func),
"__iter__" => context.new_rustfunc(iter_func)
});
}
// Sequence iterator:
fn iter_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(iter_target, None)]);
get_iter(vm, iter_target)
}
fn iter_next(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(iter, Some(vm.ctx.iter_type()))]);
if let Some(PyIteratorValue {
ref position,
iterated_obj: ref iterated_obj_ref,
}) = iter.payload()
{
if let Some(range) = iterated_obj_ref.payload::<PyRange>() {
if let Some(int) = range.get(position.get()) {
position.set(position.get() + 1);
Ok(vm.ctx.new_int(int))
} else {
Err(new_stop_iteration(vm))
}
} else if let Some(bytes) = iterated_obj_ref.payload::<PyBytes>() {
if position.get() < bytes.len() {
let obj_ref = vm.ctx.new_int(bytes[position.get()]);
position.set(position.get() + 1);
Ok(obj_ref)
} else {
Err(new_stop_iteration(vm))
}
} else if let Some(bytes) = iterated_obj_ref.payload::<PyByteArray>() {
if position.get() < bytes.value.borrow().len() {
let obj_ref = vm.ctx.new_int(bytes.value.borrow()[position.get()]);
position.set(position.get() + 1);
Ok(obj_ref)
} else {
Err(new_stop_iteration(vm))
}
} else {
let elements = objsequence::get_elements(iterated_obj_ref);
if position.get() < elements.len() {
let obj_ref = elements[position.get()].clone();
position.set(position.get() + 1);
Ok(obj_ref)
} else {
Err(new_stop_iteration(vm))
}
}
} else {
panic!("NOT IMPL");
}
}
pub fn init(context: &PyContext) {
let iter_type = &context.iter_type;
let iter_doc = "iter(iterable) -> iterator\n\
iter(callable, sentinel) -> iterator\n\n\
Get an iterator from an object. In the first form, the argument must\n\
supply its own iterator, or be a sequence.\n\
In the second form, the callable is called until it returns the sentinel.";
iter_type_init(context, iter_type);
extend_class!(context, iter_type, {
"__new__" => context.new_rustfunc(iter_new),
"__next__" => context.new_rustfunc(iter_next),
"__doc__" => context.new_str(iter_doc.to_string())
});
}