diff --git a/src/types.rs b/src/types.rs index 45dbe19..1c1b2dd 100644 --- a/src/types.rs +++ b/src/types.rs @@ -37,6 +37,7 @@ impl_from!(Number, i32); impl_from!(String, String); impl_from!(String, &str); impl_from!(Table, Table); +impl_from!(Function, Function); pub type Value = Option; @@ -79,7 +80,7 @@ impl Table { #[derive(Clone)] pub struct Function { - name: &'static str, + name: String, inner: Rc Value>, } @@ -104,6 +105,12 @@ impl Hash for Function { } impl Function { + pub fn new(name: Option, f: impl Fn(&[Value]) -> Value + 'static) -> Self { + let inner = Rc::new(f); + let name = name.unwrap_or_else(|| format!("function {:?}", inner.as_ref() as *const _)); + Self { name, inner } + } + pub fn call(&self, args: &[Value]) -> Value { (self.inner)(args) }