diff --git a/vm/src/obj/objbool.rs b/vm/src/obj/objbool.rs index f584c0d38..e21a3a329 100644 --- a/vm/src/obj/objbool.rs +++ b/vm/src/obj/objbool.rs @@ -6,12 +6,7 @@ use crate::pyobject::{ }; use crate::vm::VirtualMachine; -use super::objdict::PyDict; -use super::objfloat::PyFloat; use super::objint::PyInt; -use super::objlist::PyList; -use super::objstr::PyString; -use super::objtuple::PyTuple; use super::objtype; impl IntoPyObject for bool { @@ -27,25 +22,6 @@ impl TryFromObject for bool { } pub fn boolval(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult { - if let Some(s) = obj.payload::() { - return Ok(!s.value.is_empty()); - } - if let Some(value) = obj.payload::() { - return Ok(*value != PyFloat::from(0.0)); - } - if let Some(dict) = obj.payload::() { - return Ok(!dict.entries.borrow().is_empty()); - } - if let Some(i) = obj.payload::() { - return Ok(!i.value.is_zero()); - } - if let Some(list) = obj.payload::() { - return Ok(!list.elements.borrow().is_empty()); - } - if let Some(tuple) = obj.payload::() { - return Ok(!tuple.elements.borrow().is_empty()); - } - Ok(if let Ok(f) = vm.get_method(obj.clone(), "__bool__") { let bool_res = vm.invoke(f, PyFuncArgs::default())?; match bool_res.payload::() { diff --git a/vm/src/obj/objdict.rs b/vm/src/obj/objdict.rs index 036e26ec0..0c8d5fe5e 100644 --- a/vm/src/obj/objdict.rs +++ b/vm/src/obj/objdict.rs @@ -175,6 +175,10 @@ fn dict_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { } impl PyDictRef { + fn bool(self, _vm: &VirtualMachine) -> bool { + !self.entries.borrow().is_empty() + } + fn len(self, _vm: &VirtualMachine) -> usize { self.entries.borrow().len() } @@ -300,6 +304,7 @@ impl PyDictRef { pub fn init(context: &PyContext) { extend_class!(context, &context.dict_type, { + "__bool__" => context.new_rustfunc(PyDictRef::bool), "__len__" => context.new_rustfunc(PyDictRef::len), "__contains__" => context.new_rustfunc(PyDictRef::contains), "__delitem__" => context.new_rustfunc(PyDictRef::delitem), diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index ded687668..1abeabba2 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -120,6 +120,10 @@ impl PyFloatRef { } } + fn bool(self, _vm: &VirtualMachine) -> bool { + self.value != 0.0 + } + fn divmod(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { if objtype::isinstance(&other, &vm.ctx.float_type()) || objtype::isinstance(&other, &vm.ctx.int_type()) @@ -366,6 +370,7 @@ pub fn init(context: &PyContext) { "__abs__" => context.new_rustfunc(PyFloatRef::abs), "__add__" => context.new_rustfunc(PyFloatRef::add), "__radd__" => context.new_rustfunc(PyFloatRef::add), + "__bool__" => context.new_rustfunc(PyFloatRef::bool), "__divmod__" => context.new_rustfunc(PyFloatRef::divmod), "__floordiv__" => context.new_rustfunc(PyFloatRef::floordiv), "__new__" => context.new_rustfunc(PyFloatRef::new_float), diff --git a/vm/src/obj/objlist.rs b/vm/src/obj/objlist.rs index 9a91d051b..dd5eb055b 100644 --- a/vm/src/obj/objlist.rs +++ b/vm/src/obj/objlist.rs @@ -94,6 +94,10 @@ impl PyListRef { } } + fn bool(self, _vm: &VirtualMachine) -> bool { + !self.elements.borrow().is_empty() + } + fn clear(self, _vm: &VirtualMachine) { self.elements.borrow_mut().clear(); } @@ -419,6 +423,7 @@ pub fn init(context: &PyContext) { extend_class!(context, list_type, { "__add__" => context.new_rustfunc(PyListRef::add), "__iadd__" => context.new_rustfunc(PyListRef::iadd), + "__bool__" => context.new_rustfunc(PyListRef::bool), "__contains__" => context.new_rustfunc(PyListRef::contains), "__eq__" => context.new_rustfunc(PyListRef::eq), "__lt__" => context.new_rustfunc(PyListRef::lt), diff --git a/vm/src/obj/objstr.rs b/vm/src/obj/objstr.rs index a7cdc1a41..67ee8e5e7 100644 --- a/vm/src/obj/objstr.rs +++ b/vm/src/obj/objstr.rs @@ -57,6 +57,10 @@ impl PyStringRef { } } + fn bool(self, _vm: &VirtualMachine) -> bool { + !self.value.is_empty() + } + fn eq(self, rhs: PyObjectRef, vm: &VirtualMachine) -> bool { if objtype::isinstance(&rhs, &vm.ctx.str_type()) { self.value == get_value(&rhs) @@ -632,6 +636,7 @@ pub fn init(context: &PyContext) { extend_class!(context, str_type, { "__add__" => context.new_rustfunc(PyStringRef::add), + "__bool__" => context.new_rustfunc(PyStringRef::bool), "__contains__" => context.new_rustfunc(PyStringRef::contains), "__doc__" => context.new_str(str_doc.to_string()), "__eq__" => context.new_rustfunc(PyStringRef::eq), diff --git a/vm/src/obj/objtuple.rs b/vm/src/obj/objtuple.rs index 65a823db4..92a90913a 100644 --- a/vm/src/obj/objtuple.rs +++ b/vm/src/obj/objtuple.rs @@ -101,6 +101,10 @@ impl PyTupleRef { } } + fn bool(self, _vm: &VirtualMachine) -> bool { + !self.elements.borrow().is_empty() + } + fn count(self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult { let mut count: usize = 0; for element in self.elements.borrow().iter() { @@ -230,6 +234,7 @@ tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object."; context.set_attr(tuple_type, "__add__", context.new_rustfunc(PyTupleRef::add)); + context.set_attr(tuple_type, "__bool__", context.new_rustfunc(PyTupleRef::bool)); context.set_attr(tuple_type, "__eq__", context.new_rustfunc(PyTupleRef::eq)); context.set_attr(tuple_type,"__contains__",context.new_rustfunc(PyTupleRef::contains)); context.set_attr(tuple_type,"__getitem__",context.new_rustfunc(PyTupleRef::getitem));