Skip to content
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

Closed
mcclure opened this issue Feb 20, 2021 · 7 comments
Closed

Comments

@mcclure
Copy link

mcclure commented Feb 20, 2021

(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:

Screen Shot 2021-02-20 at 2 58 32 PM

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?

@khvzak
Copy link
Member

khvzak commented Feb 20, 2021

Your intuition is completely right :)
As in the example, you could just return a table of functions, that Lua will cache into package.loaded[modname] when loading your module using require

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.

@mcclure
Copy link
Author

mcclure commented Feb 20, 2021

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.

@mcclure
Copy link
Author

mcclure commented Feb 21, 2021

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?

@mcclure
Copy link
Author

mcclure commented Feb 21, 2021

Oh, sorry, I see there are some docs in the README. Though it still doesn't seem to explain exactly what #lua_module does.

@mcclure
Copy link
Author

mcclure commented Feb 21, 2021

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 require."

@khvzak
Copy link
Member

khvzak commented Feb 22, 2021

Thanks for the feedback!
I agree that documentation need to be updated. In future I'll publish a detailed manual to cover usage / options / examples.

@khvzak
Copy link
Member

khvzak commented Feb 27, 2021

I updated README

@khvzak khvzak closed this as completed Feb 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants