lua.rs

 1use mlua::Result;
 2
 3use crate::*;
 4
 5impl Runtime for Lua {
 6    type Plugin = LuaPlugin;
 7    type Error = mlua::Error;
 8
 9    fn init(module: Self::Plugin) -> Result<Self> {
10        let lua = Lua::new();
11
12        // for action in module.actions {
13        //     action(&mut lua).ok()?;
14        // }
15
16        lua.load(&module.source).exec()?;
17        return Ok(lua);
18    }
19
20    fn constant<T: DeserializeOwned>(&mut self, handle: &Handle) -> Result<T> {
21        let val: Value = self.globals().get(handle.inner())?;
22        Ok(self.from_value(val)?)
23    }
24
25    fn call<A: Serialize, R: DeserializeOwned>(&mut self, handle: &Handle, arg: A) -> Result<R> {
26        let fun: Function = self.globals().get(handle.inner())?;
27        let arg: Value = self.to_value(&arg)?;
28        let result = fun.call(arg)?;
29        Ok(self.from_value(result)?)
30    }
31
32    fn register_handle<T: AsRef<str>>(&mut self, name: T) -> bool {
33        self.globals()
34            .contains_key(name.as_ref().to_string())
35            .unwrap_or(false)
36    }
37}
38
39pub struct LuaPlugin {
40    // name: String,
41    source: String,
42    // actions: Vec<Box<dyn FnOnce(&mut Lua) -> Result<(), ()>>>,
43}
44
45impl LuaPlugin {
46    pub fn new(
47        // name: String,
48        source: String,
49    ) -> LuaPlugin {
50        LuaPlugin {
51            // name,
52            source,
53            // actions: Vec::new(),
54        }
55    }
56
57    // pub fn setup(mut self, action: fn(&mut Lua) -> Result<(), ()>) -> LuaPlugin {
58    //     let action = Box::new(action);
59    //     self.actions.push(action);
60    //     self
61    // }
62}