Attribute Macro mlua::lua_module

source ·
#[lua_module]
Available on crate feature module only.
Expand description

Registers Lua module entrypoint.

You can register multiple entrypoints as required.

use mlua::{Lua, Result, Table};

#[mlua::lua_module]
fn my_module(lua: &Lua) -> Result<Table> {
    let exports = lua.create_table()?;
    exports.set("hello", "world")?;
    Ok(exports)
}

Internally in the code above the compiler defines C function luaopen_my_module.

You can also pass options to the attribute:

  • name - name of the module, defaults to the name of the function
#[mlua::lua_module(name = "alt_module")]
fn my_module(lua: &Lua) -> Result<Table> {
    ...
}
  • skip_memory_check - skip memory allocation checks for some operations.

In module mode, mlua runs in unknown environment and cannot say are there any memory limits or not. As result, some operations that require memory allocation runs in protected mode. Setting this attribute will improve performance of such operations with risk of having uncaught exceptions and memory leaks.

#[mlua::lua_module(skip_memory_check)]
fn my_module(lua: &Lua) -> Result<Table> {
    ...
}