-
-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] Mass-registration of functions / equivalent of luaL_register? #34
Comments
Your intuition is completely right :) fn func1(_: &Lua, _: ()) -> LuaResult<()> {
Ok(())
}
fn func2(_: &Lua, _: ()) -> LuaResult<()> {
Ok(())
}
#[mlua::lua_module]
fn rust_module(lua: &Lua) -> LuaResult<LuaTable> {
lua.create_table_from(vec![
("func1", lua.create_function(func1)?),
("func2", lua.create_function(func2)?),
])
} Inside init function in your module you also have access to globals and can additionally register your data. |
Thanks for the sample code— I will be doing this pattern a lot so maybe I will see if I can simply it with a macro, and if I do this maybe I will check back about whether you would like it upstream as a PR. |
Thanks. As a piece of feedback, I am looking at the mlua docs and I don't see any docs for lua_module, nor do I see docs for the "module" or "mlua_derive" features. Is there a doc somewhere I am missing? Could docs for the correct usage of lua_module be added? |
Oh, sorry, I see there are some docs in the README. Though it still doesn't seem to explain exactly what #lua_module does. |
Okay, I think I've figured out that "standalone mode" is building a normal exe with mlua and "module mode" is building a shared object to be loaded by Lua. I think the README would be clearer if you added a sentence under "Module mode" such as "Use this mode to create a compiled Lua module that can be loaded from Lua code using |
Thanks for the feedback! |
I updated README |
(This is a support request more than an "issue". If there is a better place to ask mlua questions I'd be happy to go there. I am new to both mlua and Rust. Thanks.)
In the Lua C library there is a function luaL_register that allows you to load many functions into a table at once, usually package.preload. This is useful for loading an entire C API into Lua all at once. Example:
What would be the closest mlua equivalent to this? I am looking over the docs and don't see anything specific.
My intuition is "oh, I will just make an array, and iterate over the array calling create_function". But then I run into interesting difficulties with the fact I cannot really store a heterogenous array of functions with different signatures into a single variable to iterate over, and it also seems I cannot easily create an array of mlua:;Function objects (at least, not in its own module) because of Rust rules about storing unsafe/thread-global mutable data into a static.
I'm sure I will figure something out, but I wonder, is there a "best practices" mlua pattern for this?
The text was updated successfully, but these errors were encountered: