Add test to check loading module from lua coroutine (thread)

This commit is contained in:
Alex Orlenko 2020-06-06 15:49:24 +01:00
parent 2595fe9885
commit 2eb40deafd
2 changed files with 32 additions and 0 deletions

View file

@ -4,9 +4,14 @@ fn sum(_: &Lua, (a, b): (i64, i64)) -> LuaResult<i64> {
Ok(a + b)
}
fn used_memory(lua: &Lua, _: ()) -> LuaResult<usize> {
Ok(lua.used_memory())
}
#[mlua_derive::lua_module]
fn rust_module(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?;
exports.set("sum", lua.create_function(sum)?)?;
exports.set("used_memory", lua.create_function(used_memory)?)?;
Ok(exports)
}

View file

@ -15,6 +15,33 @@ fn test_module() -> Result<()> {
.exec()
}
#[cfg(any(
feature = "lua54",
feature = "lua53",
feature = "lua52",
feature = "lua51"
))]
#[test]
fn test_module_from_thread() -> Result<()> {
let lua = make_lua()?;
lua.load(
r#"
local mod
local co = coroutine.create(function(a, b)
mod = require("rust_module")
assert(mod.sum(a, b) == a + b)
end)
coroutine.resume(co, 3, 5)
collectgarbage()
assert(mod.used_memory() > 0)
"#,
)
.exec()
}
fn make_lua() -> Result<Lua> {
let (dylib_path, dylib_ext, separator);
if cfg!(target_os = "macos") {