Improve module mode:

- Don't hide module function inside `luaopen_%` function.
- Raise Lua exception instead of panic if module function returns error.
This commit is contained in:
Alex Orlenko
2021-06-13 22:38:51 +01:00
parent 3b9d8a7b5f
commit 08ffeb0ca9
4 changed files with 42 additions and 17 deletions
+9 -1
View File
@@ -536,7 +536,15 @@ impl Lua {
F: 'static + MaybeSend + Fn(&'callback Lua) -> Result<R>,
{
let cb = self.create_callback(Box::new(move |lua, _| func(lua)?.to_lua_multi(lua)))?;
unsafe { self.push_value(cb.call(())?).map(|_| 1) }
match cb.call(()) {
Ok(res) => unsafe { self.push_value(res)? },
Err(err) => unsafe {
self.push_value(Value::Error(err))?;
// This longjmp is undesired, but we cannot wrap it to a C
ffi::lua_error(self.state)
},
}
Ok(1)
}
/// Sets a 'hook' function that will periodically be called as Lua code executes.