Add strinst.__bool__ method
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -24,6 +24,7 @@ impl Str {
|
||||
attrs: Default::default(),
|
||||
},
|
||||
vtable: |obj_ref: ObjRef| vtable! {
|
||||
BOOL_MEMBER_NAME.sym => Method::new_obj(obj_ref.clone(), STR_BOOL_METH.clone()),
|
||||
STR_MEMBER_NAME.sym => Method::new_obj(obj_ref.clone(), STR_STR_METH.clone()),
|
||||
REPR_MEMBER_NAME.sym => Method::new_obj(obj_ref.clone(), STR_REPR_METH.clone()),
|
||||
INT_MEMBER_NAME.sym => Method::new_obj(obj_ref.clone(), STR_INT_METH.clone()),
|
||||
@@ -48,6 +49,18 @@ impl Debug for Str {
|
||||
// Builtin Str functions
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Str.__bool__(self) impl
|
||||
static STR_BOOL_METH: Lazy<NativeFunRef> = Lazy::new(|| {
|
||||
NativeFun::new_obj(1, |_callee, vm, args| {
|
||||
read_obj_downcast!(let str_obj: Option<&Str> = &args[0]);
|
||||
let str_obj = str_obj.ok_or_else(|| Error::ValueError {
|
||||
error: "expected Str value".to_string(),
|
||||
})?;
|
||||
vm.push(BoolRef::from(!str_obj.value.is_empty()));
|
||||
Ok(crate::vm::signal::Signal::Return)
|
||||
})
|
||||
});
|
||||
|
||||
/// Str.__str__(self) impl
|
||||
static STR_STR_METH: Lazy<NativeFunRef> = Lazy::new(|| {
|
||||
NativeFun::new_obj(1, |_callee, vm, args| {
|
||||
|
||||
Reference in New Issue
Block a user