forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjlist.rs
More file actions
143 lines (129 loc) · 4.57 KB
/
objlist.rs
File metadata and controls
143 lines (129 loc) · 4.57 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
use super::super::pyobject::{
AttributeProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
};
use super::super::vm::VirtualMachine;
use super::objsequence::{seq_equal, PySliceableSequence};
use super::objstr;
use super::objtype;
// set_item:
pub fn set_item(
vm: &mut VirtualMachine,
l: &mut Vec<PyObjectRef>,
idx: PyObjectRef,
obj: PyObjectRef,
) -> PyResult {
match &(idx.borrow()).kind {
PyObjectKind::Integer { value } => {
let pos_index = l.get_pos(*value);
l[pos_index] = obj;
Ok(vm.get_none())
}
_ => panic!(
"TypeError: indexing type {:?} with index {:?} is not supported (yet?)",
l, idx
),
}
}
pub fn get_elements(obj: &PyObjectRef) -> Vec<PyObjectRef> {
if let PyObjectKind::List { elements } = &obj.borrow().kind {
elements.to_vec()
} else {
panic!("Cannot extract list elements from non-list");
}
}
fn list_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(zelf, Some(vm.ctx.list_type())), (other, None)]
);
let result = if objtype::isinstance(other.clone(), vm.ctx.list_type()) {
let zelf = get_elements(zelf);
let other = get_elements(other);
seq_equal(vm, zelf, other)?
} else {
false
};
Ok(vm.ctx.new_bool(result))
}
fn list_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(o, Some(vm.ctx.list_type())), (o2, None)]
);
if objtype::isinstance(o2.clone(), vm.ctx.list_type()) {
let e1 = get_elements(o);
let e2 = get_elements(o2);
let elements = e1.iter().chain(e2.iter()).map(|e| e.clone()).collect();
Ok(vm.ctx.new_list(elements))
} else {
Err(vm.new_type_error(format!("Cannot add {:?} and {:?}", o, o2)))
}
}
fn list_str(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(o, Some(vm.ctx.list_type()))]);
let elements = get_elements(o);
let mut str_parts = vec![];
for elem in elements {
match vm.to_str(elem) {
Ok(s) => str_parts.push(objstr::get_value(&s)),
Err(err) => return Err(err),
}
}
let s = format!("[{}]", str_parts.join(", "));
Ok(vm.new_str(s))
}
pub fn append(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("list.append called with: {:?}", args);
arg_check!(
vm,
args,
required = [(list, Some(vm.ctx.list_type())), (x, None)]
);
let mut list_obj = list.borrow_mut();
if let PyObjectKind::List { ref mut elements } = list_obj.kind {
elements.push(x.clone());
Ok(vm.get_none())
} else {
Err(vm.new_type_error("list.append is called with no list".to_string()))
}
}
fn clear(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("list.clear called with: {:?}", args);
arg_check!(vm, args, required = [(list, Some(vm.ctx.list_type()))]);
let mut list_obj = list.borrow_mut();
if let PyObjectKind::List { ref mut elements } = list_obj.kind {
elements.clear();
Ok(vm.get_none())
} else {
Err(vm.new_type_error("list.clear is called with no list".to_string()))
}
}
fn list_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("list.len called with: {:?}", args);
arg_check!(vm, args, required = [(list, Some(vm.ctx.list_type()))]);
let elements = get_elements(list);
Ok(vm.context().new_int(elements.len() as i32))
}
fn reverse(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("list.reverse called with: {:?}", args);
arg_check!(vm, args, required = [(list, Some(vm.ctx.list_type()))]);
let mut list_obj = list.borrow_mut();
if let PyObjectKind::List { ref mut elements } = list_obj.kind {
elements.reverse();
Ok(vm.get_none())
} else {
Err(vm.new_type_error("list.reverse is called with no list".to_string()))
}
}
pub fn init(context: &PyContext) {
let ref list_type = context.list_type;
list_type.set_attr("__eq__", context.new_rustfunc(list_eq));
list_type.set_attr("__add__", context.new_rustfunc(list_add));
list_type.set_attr("__len__", context.new_rustfunc(list_len));
list_type.set_attr("__str__", context.new_rustfunc(list_str));
list_type.set_attr("append", context.new_rustfunc(append));
list_type.set_attr("clear", context.new_rustfunc(clear));
list_type.set_attr("reverse", context.new_rustfunc(reverse));
}