MCPcopy Create free account
hub / github.com/dylan-sutton-chavez/edge-python / truthy

Method truthy

compiler/src/modules/vm/ops.rs:94–128  ·  view source on GitHub ↗
(&self, v: Val)

Source from the content-addressed store, hash-verified

92
93impl<'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"))?;

Callers 11

dispatch_genericMethod · 0.80
try_compare_dunderMethod · 0.80
truthy_opMethod · 0.80
contains_opMethod · 0.80
eq_opMethod · 0.80
exec_sortMethod · 0.80
dispatch_nativeMethod · 0.80
sort_ltMethod · 0.80
call_filterMethod · 0.80
call_allMethod · 0.80
call_anyMethod · 0.80

Calls 10

is_noneMethod · 0.80
is_falseMethod · 0.80
is_trueMethod · 0.80
is_intMethod · 0.80
as_intMethod · 0.80
is_floatMethod · 0.80
as_floatMethod · 0.80
borrowMethod · 0.80
getMethod · 0.45
is_emptyMethod · 0.45

Tested by

no test coverage detected