mink/
func.rs

1use crate::value::Value;
2use crate::vm::Mink;
3
4pub struct Function {
5    pub name: String,
6    arg_count: Option<u32>,
7    inner: fn (&Mink, Vec<Value>) -> Value,
8}
9
10impl Function {
11    pub fn new(name: impl Into<String>, arg_count: Option<u32>, func: fn (&Mink, Vec<Value>) -> Value) -> Self {
12        Self {
13            name: name.into(),
14            arg_count,
15            inner: func,
16        }
17    }
18
19    pub(crate) fn exec(&self, vm: &Mink, args: Vec<Value>) -> Value {
20        if let Some(expected_arg_count) = self.arg_count.clone() {
21            let arg_count = args.len() as u32;
22            if arg_count != expected_arg_count {
23                panic!("Function '{}' expected {} args, found {}", self.name, expected_arg_count, arg_count);
24            }
25        }
26
27        let func = &self.inner;
28        func(vm, args)
29    }
30}