Skip to content

Is there a way to give Lua access to resource files embeded in a Rust binary? #322

Answered by khvzak
alerque asked this question in Q&A
Discussion options

You must be logged in to vote

The 1st variant should work actually (or very close to). Take a look to the code below, it works fine. No need to do wrap/unwrap/etc.

unsafe extern "C-unwind" fn luaopen_mymodule(state: *mut ffi::lua_State) -> i32 {
    ffi::lua_newtable(state);
    ffi::lua_pushstring(state, b"world\0".as_ptr() as *const _);
    ffi::lua_setfield(state, -2, b"hello\0".as_ptr() as *const _);
    1
}

fn main() -> Result<()> {
    let lua = Lua::new();

    let package: Table = lua.globals().get("package")?;
    let loaders: Table = package.get("loaders")?;
    loaders.push(Function::wrap(|lua, path: String| unsafe {
        if path == "mymodule" {
            return lua.create_c_function(luaopen_mymodule).m…

Replies: 2 comments 14 replies

Comment options

You must be logged in to vote
8 replies
@alerque
Comment options

@alerque
Comment options

@alerque
Comment options

@khvzak
Comment options

@alerque
Comment options

Comment options

You must be logged in to vote
6 replies
@khvzak
Comment options

@alerque
Comment options

@alerque
Comment options

@khvzak
Comment options

Answer selected by alerque
@alerque
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants