使用 Lua C API 在子表中插入函数

Insert function inside subtable using Lua C API

我正在制作自己的游戏引擎,使用 Lua C API。我得到了这样的 Lua table 层次结构:

my_lib = {
  system = { ... },
  keyboard = { ... },
  graphics = { ... },
  ...
}

我还有一些C函数,我想注册,类似这样的:

inline static int lua_mylib_do_cool_things(lua_State *L) {
    mylib_do_cool_things(luaL_checknumber(L, 1));
    return 0;
}

那么,我怎样才能像my_lib子table的会员那样注册呢?

my_lib = {
  system = { do_cool_things, ... },
  keyboard = { ... }
  graphics = { ...}
}

现在只知道注册全局table会员的方法,是这样的:

inline void mylib_registerFuncAsTMem(const char *table, lua_CFunction func, const char *index) {
    lua_getglobal(mylib_luaState, table);
    lua_pushstring(mylib_luaState, index);
    lua_pushcfunction(mylib_luaState, func);
    lua_rawset(mylib_luaState, -3);
}

但是子 table 呢?

将多个 Lua C 函数注册到 table(使用 Lua 5.1)的简单方法是使用 luaL_register.

首先,实现你的 Lua 功能,它们应该采用 lua_CFunction.

的形式
static int graphics_draw(lua_State *L) {
    return luaL_error(L, "graphics.draw unimplemented");
}

static int system_wait(lua_State *L) {
    return luaL_error(L, "system.wait unimplemented");
}

接下来,使用您的 Lua 函数及其名称(键)为每个子 table 创建一个 luaL_Reg 结构。

static const struct luaL_Reg module_graphics[] = {
    {"draw", graphics_draw},        
    // add more graphic functions here.. 
    {NULL, NULL}  // terminate the list with sentinel value
};

static const struct luaL_Reg module_system[] = {
    {"wait", system_wait},        
    {NULL, NULL}
};

然后,在您 return 所在的函数中,您的模块 table 创建每个子 table 和 register 它的函数。

int luaopen_game(lua_State *L) {    
    lua_newtable(L);  // create the module table

    lua_newtable(L);                          // create the graphics table
    luaL_register(L, NULL, module_graphics);  // register functions into graphics table
    lua_setfield(L, -2, "graphics");          // add graphics table to module

    lua_newtable(L);                          // create the system table
    luaL_register(L, NULL, module_system);    // register functions into system table
    lua_setfield(L, -2, "system");            // add system table to module

    // repeat the same process for other sub-tables

    return 1;  // return module table
}

这将导致模块 table 具有以下结构:

game = {
    graphics = {
        draw -- C function graphics_draw
    },
    system = {
        wait -- C function system_wait
    }
}