use crate::host::{self, PyHost, PyObj};
use fusevm::Value;
pub fn entries(h: &mut PyHost) -> Vec<(String, Value)> {
vec![
(
"reduce".to_string(),
h.alloc(PyObj::Builtin("functools.reduce".into())),
),
(
"partial".to_string(),
h.alloc(PyObj::Builtin("functools.partial".into())),
),
(
"lru_cache".to_string(),
h.alloc(PyObj::Builtin("functools.lru_cache".into())),
),
(
"cache".to_string(),
h.alloc(PyObj::Builtin("functools.cache".into())),
),
]
}
pub fn call(
fname: &str,
args: &[Value],
kwargs: &[(String, Value)],
) -> Option<Result<Value, String>> {
match fname {
"reduce" => Some(reduce(args)),
"partial" => Some(partial(args, kwargs)),
"lru_cache" => Some(lru_cache(args, kwargs)),
"cache" => Some(lru_cache_wrap(args, &[])),
"__lru_wrap" => Some(lru_cache_wrap(args, kwargs)),
_ => None,
}
}
fn partial(args: &[Value], kwargs: &[(String, Value)]) -> Result<Value, String> {
let func = args
.first()
.cloned()
.ok_or_else(|| host::type_error("partial() takes at least 1 argument (0 given)"))?;
Ok(host::make_partial(
func,
args[1..].to_vec(),
kwargs.to_vec(),
))
}
fn lru_cache(args: &[Value], kwargs: &[(String, Value)]) -> Result<Value, String> {
if let Some(first) = args.first() {
let is_callable = host::with_host(|h| {
matches!(
h.get(first),
Some(PyObj::Func(_))
| Some(PyObj::Builtin(_))
| Some(PyObj::BoundMethod { .. })
| Some(PyObj::Class(_))
| Some(PyObj::Partial { .. })
| Some(PyObj::LruCache { .. })
)
});
if is_callable {
return Ok(host::make_lru_cache(first.clone(), Some(128)));
}
}
let maxsize = resolve_maxsize(args, kwargs);
let wrapper = host::with_host(|h| h.alloc(PyObj::Builtin("functools.__lru_wrap".into())));
let ms_kw = match maxsize {
Some(n) => ("maxsize".to_string(), Value::Int(n as i64)),
None => ("maxsize".to_string(), Value::Undef),
};
Ok(host::make_partial(wrapper, vec![], vec![ms_kw]))
}
fn lru_cache_wrap(args: &[Value], kwargs: &[(String, Value)]) -> Result<Value, String> {
let func = args
.first()
.cloned()
.ok_or_else(|| host::type_error("lru_cache decorator expects a callable"))?;
let maxsize = match kwargs.iter().find(|(k, _)| k == "maxsize").map(|(_, v)| v) {
Some(Value::Int(n)) if *n >= 0 => Some(*n as usize),
_ => None,
};
Ok(host::make_lru_cache(func, maxsize))
}
fn resolve_maxsize(args: &[Value], kwargs: &[(String, Value)]) -> Option<usize> {
let v = kwargs
.iter()
.find(|(k, _)| k == "maxsize")
.map(|(_, v)| v.clone())
.or_else(|| args.first().cloned());
match v {
Some(Value::Int(n)) if n >= 0 => Some(n as usize),
_ => None,
}
}
fn reduce(args: &[Value]) -> Result<Value, String> {
let func = args
.first()
.cloned()
.ok_or_else(|| host::type_error("reduce expected at least 2 arguments, got 0"))?;
let iterable = args
.get(1)
.ok_or_else(|| host::type_error("reduce expected at least 2 arguments, got 1"))?;
let items = host::iter_vec(iterable)?;
let mut iter = items.into_iter();
let mut acc = match args.get(2) {
Some(init) => init.clone(),
None => iter
.next()
.ok_or_else(|| host::type_error("reduce() of empty iterable with no initial value"))?,
};
for it in iter {
acc = host::invoke(&func, vec![acc, it], vec![])?;
}
Ok(acc)
}