lualuac

How to precompile and dump Lua binary with C closures?


Currently I have a Lua-driven system with C functions connected. When it is built from source, it is constructed as below:

// bind C functions to make them callable inside Lua code
lua_pushlightuserdata(lua, some_cxx_object);
lua_pushcclosure(lua, global_function_foo, 1);
lua_setglobal(lua, "foo");

lua_pushcclosure(lua, global_function_bar, 0);
lua_setglobal(lua, "bar");

...... many more bindings ......

// compile Lua source code
auto re = luaL_loadbufferx( guts->lua, source, source_len, nullptr, nullptr );
if (re != LUA_OK)
    raise_exceptions();

// use the prepared Lua system
......

Now I want to use precompiled binaries to improve system init speed. My question is: how does lua_dump work with those C closure globals? Specifically:


Solution

  • When I'm going to compile & dump, should I bind C functions before compile?

    No, just like how you can run luac to compile a single lua script.

    When I'm going to use the precompiled binary, do I need to bind C functions again, before loading the binary?

    Yes, but not before loading, but before running, so the only difference is that you can save compilation time.