(&self, v: Val)
| 92 | |
| 93 | impl<'a> VM<'a> { |
| 94 | pub fn truthy(&self, v: Val) -> bool { |
| 95 | if v.is_none() || v.is_false() { return false; } |
| 96 | if v.is_true() { return true; } |
| 97 | if v.is_int() { return v.as_int() != 0; } |
| 98 | if v.is_float() { return v.as_float() != 0.0; } |
| 99 | match self.heap.get(v) { |
| 100 | HeapObj::Str(s) => !s.is_empty(), |
| 101 | HeapObj::Bytes(b) => !b.is_empty(), |
| 102 | HeapObj::LongInt(i) => *i != 0, |
| 103 | HeapObj::List(l) => !l.borrow().is_empty(), |
| 104 | HeapObj::Tuple(t) => !t.is_empty(), |
| 105 | HeapObj::Dict(d) => !d.borrow().is_empty(), |
| 106 | HeapObj::Set(s) => !s.borrow().is_empty(), |
| 107 | HeapObj::FrozenSet(s) => !s.is_empty(), |
| 108 | HeapObj::Range(s,e,st) => if *st > 0 { s < e } else { s > e }, |
| 109 | HeapObj::Type(_) => true, |
| 110 | HeapObj::Func(_, _, _) => true, |
| 111 | HeapObj::Slice(..) => true, |
| 112 | HeapObj::BoundMethod(..) => true, |
| 113 | HeapObj::NativeFn(_) => true, |
| 114 | HeapObj::Class(..) => true, |
| 115 | HeapObj::BoundUserMethod(..) => true, |
| 116 | HeapObj::Super(..) => true, |
| 117 | HeapObj::Property(..) => true, |
| 118 | HeapObj::PropertySetter(..) => true, |
| 119 | HeapObj::StaticMethod(..) => true, |
| 120 | HeapObj::Instance(..) => true, |
| 121 | HeapObj::Coroutine(..) => true, |
| 122 | HeapObj::Module(..) => true, |
| 123 | HeapObj::Extern(_) => true, |
| 124 | HeapObj::ExcInstance(..) => true, |
| 125 | HeapObj::Ellipsis => true, |
| 126 | HeapObj::NotImplemented => true, |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | pub fn bitwise_op(&mut self, a: Val, b: Val, op: impl Fn(i128, i128) -> i128) -> Result<Val, VmErr> { |
| 131 | let ai = as_i128(a, &self.heap).ok_or(cold_type("bitwise op requires integer operands"))?; |
no test coverage detected