summaryrefslogtreecommitdiff
path: root/examples/module/src
diff options
context:
space:
mode:
authorAlex Orlenko <zxteam@protonmail.com>2021-05-18 20:07:34 +0100
committerAlex Orlenko <zxteam@protonmail.com>2021-05-18 20:07:34 +0100
commite8de2a458ad8dea6847f17b3c7d3fb564109a3f3 (patch)
tree248ccb47af7ca0701e3f30a9847c22f7995667a7 /examples/module/src
parent973b5c3bf5a80748d79500673f765773f45a1d9d (diff)
downloadmlua-e8de2a458ad8dea6847f17b3c7d3fb564109a3f3.zip
Allow multiple entrypoints in a single module share the same Lua state.
Previously it would initialize different Lua instances. Fixes #49.
Diffstat (limited to 'examples/module/src')
-rw-r--r--examples/module/src/lib.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/examples/module/src/lib.rs b/examples/module/src/lib.rs
index 38cc0fd..dcd487a 100644
--- a/examples/module/src/lib.rs
+++ b/examples/module/src/lib.rs
@@ -8,10 +8,27 @@ fn used_memory(lua: &Lua, _: ()) -> LuaResult<usize> {
Ok(lua.used_memory())
}
+fn check_userdata(_: &Lua, ud: MyUserData) -> LuaResult<i32> {
+ Ok(ud.0)
+}
+
#[mlua::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)?)?;
+ exports.set("check_userdata", lua.create_function(check_userdata)?)?;
+ Ok(exports)
+}
+
+#[derive(Clone, Copy)]
+struct MyUserData(i32);
+
+impl LuaUserData for MyUserData {}
+
+#[mlua::lua_module]
+fn rust_module_second(lua: &Lua) -> LuaResult<LuaTable> {
+ let exports = lua.create_table()?;
+ exports.set("userdata", lua.create_userdata(MyUserData(123))?)?;
Ok(exports)
}