Borrow list object mutably and append item

This commit is contained in:
Windel Bouwman committed 2018-08-14 21:52:00 +02:00
1 parent 4ada085234
commit 34bba7a257
2 files changed
+17 -10

No files matched your search

+5 -1
View File
@@ -30,8 +30,12 @@ fn dir_locals(vm: &mut VirtualMachine) -> PyObjectRef {
}
fn dir_object(vm: &mut VirtualMachine, _obj: PyObjectRef) -> PyObjectRef {
// TODO: Implement dir for objects
let d = vm.new_dict();
// TODO: loop over dict of instance, next of class?
// TODO: Implement dir for objects
// for i in obj.iter_items() {
// d.set_item(k, v);
// }
d
}
+12 -9
View File
@@ -24,17 +24,20 @@ pub fn set_item(
}
fn append(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
// TODO: Implement objlist::append
trace!("list.append called with: {:?}", args);
if let PyObjectKind::List { ref elements } = args.args[0].borrow().kind {
warn!("implement append here: {:?}", elements);
// elements.push(args.args[1].clone());
Ok(vm.get_none())
if args.args.len() == 2 {
let l = args.args[0].clone();
let o = args.args[1].clone();
trace!("list.append called with: {:?}", args);
let mut list_obj = l.borrow_mut();
if let PyObjectKind::List { ref mut elements } = list_obj.kind {
elements.push(o);
Ok(vm.get_none())
} else {
Err(vm.new_exception("list.append is called with no list".to_string()))
}
} else {
unimplemented!("Raise error when list.append is called with no list")
// Err()
Err(vm.new_exception("list.append requires two arguments".to_string()))
}
// Ok(vm.new_bound_method(args.args[0].clone(), args.args[1].clone()))
}
pub fn create_type(type_type: PyObjectRef) -> PyObjectRef {