forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjbool.rs
More file actions
99 lines (91 loc) · 3.18 KB
/
objbool.rs
File metadata and controls
99 lines (91 loc) · 3.18 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
use super::obj::objtype;
use super::pyobject::{
AttributeProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
};
use super::vm::VirtualMachine;
pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> Result<bool, PyObjectRef> {
let result = match obj.borrow().kind {
PyObjectKind::Boolean { value } => value,
PyObjectKind::Integer { value } => value != 0,
PyObjectKind::Float { value } => value != 0.0,
PyObjectKind::List { ref elements } => !elements.is_empty(),
PyObjectKind::Tuple { ref elements } => !elements.is_empty(),
PyObjectKind::Dict { ref elements } => !elements.is_empty(),
PyObjectKind::String { ref value } => !value.is_empty(),
PyObjectKind::None { .. } => false,
_ => {
if let Ok(f) = objtype::get_attribute(vm, obj.clone(), &String::from("__bool__")) {
match vm.invoke(f, PyFuncArgs::default()) {
Ok(result) => match result.borrow().kind {
PyObjectKind::Boolean { value } => value,
_ => return Err(vm.new_type_error(String::from("TypeError"))),
},
Err(err) => return Err(err),
}
} else {
true
}
}
};
Ok(result)
}
pub fn init(context: &PyContext) {
let ref bool_type = context.bool_type;
bool_type.set_attr("__new__", context.new_rustfunc(bool_new));
bool_type.set_attr("__str__", context.new_rustfunc(bool_str));
bool_type.set_attr("__eq__", context.new_rustfunc(bool_eq));
}
fn bool_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(zelf, Some(vm.ctx.bool_type())), (other, None)]
);
let result = if objtype::isinstance(other.clone(), vm.ctx.bool_type()) {
get_value(zelf) == get_value(other)
} else {
false
};
Ok(vm.ctx.new_bool(result))
}
pub fn not(vm: &mut VirtualMachine, obj: &PyObjectRef) -> PyResult {
if objtype::isinstance(obj.clone(), vm.ctx.bool_type()) {
let value = get_value(obj);
Ok(vm.ctx.new_bool(!value))
} else {
Err(vm.new_type_error(format!("Can only invert a bool, on {:?}", obj)))
}
}
// Retrieve inner int value:
pub fn get_value(obj: &PyObjectRef) -> bool {
if let PyObjectKind::Boolean { value } = &obj.borrow().kind {
*value
} else {
panic!("Inner error getting inner boolean");
}
}
fn bool_str(vm: &mut VirtualMachine, args: PyFuncArgs) -> Result<PyObjectRef, PyObjectRef> {
arg_check!(vm, args, required = [(obj, Some(vm.ctx.bool_type()))]);
let v = get_value(obj);
let s = if v {
"True".to_string()
} else {
"False".to_string()
};
Ok(vm.new_str(s))
}
fn bool_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(_zelf, Some(vm.ctx.type_type()))],
optional = [(val, None)]
);
Ok(match val {
Some(val) => {
let bv = boolval(vm, val.clone())?;
vm.new_bool(bv.clone())
}
None => vm.context().new_bool(false),
})
}