From cdc54f54cb5895c4398b0a38f2cd51d08f75101a Mon Sep 17 00:00:00 2001 From: numzero Date: Sat, 19 Apr 2025 14:57:32 +0300 Subject: [PATCH] types: add proper Function support --- src/types.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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) }