在Lua表中注册C函数

top*_*dev 10 c++ lua lua-c++-connection

如何在Lua中注册C函数,但不是在全局上下文中,而是作为表字段?

RBe*_*eig 20

这是luaL_register()针对一个或多个功能的目的.规范用法是用C编写的模块的设置的一部分:

/* actual definitions of modA() and modB() are left as an exercise. */

/* list of functions in the module */
static const luaL_reg modfuncs[] =
{
    { "a", modA},
    { "b", modB},
    { NULL, NULL }
};

/* module loader function called eventually by require"mod" */  
int luaopen_mod(lua_State *L) {
    luaL_register(L, "mod", modfuncs);
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

这创建了一个名为"mod"的模块,它有两个名为mod.a和的函数mod.b.

引用手册luaL_register(L,libname,l):

当使用libname等于 调用时NULL,它只是将列表中的所有函数l(请参阅luaL_Reg)注册到堆栈顶部的表中.

当使用非null调用时libname, luaL_register创建一个新表t,将其设置为全局变量libname的值,将其设置为值package.loaded[libname],并在其中注册列表中的所有函数l.如果package.loaded[libname]在变量中或变量中 有表 libname,则重用此表而不是创建新表.

在任何情况下,函数都将表保留在堆栈的顶部.

luaL_register()NULL只要表位于堆栈顶部,就可以通过传递第二个参数将C函数放在任何表中.

  • 仅供参考... luaL_Register已被弃用于5.2 .. luaL_newlib和luaL_setfuncs似乎是替代品. (5认同)

top*_*dev 5

void register_c_function(char const * const tableName, char const * const funcName, CFunctionSignature funcPointer)
{
    lua_getfield(lstate, LUA_GLOBALSINDEX, tableName);  // push table onto stack
    if (!lua_istable(lstate, -1))                       // not a table, create it
    {
        lua_createtable(lstate, 0, 1);      // create new table
        lua_setfield(lstate, LUA_GLOBALSINDEX, tableName);  // add it to global context

        // reset table on stack
        lua_pop(lstate, 1);                 // pop table (nil value) from stack
        lua_getfield(lstate, LUA_GLOBALSINDEX, tableName);  // push table onto stack
    }

    lua_pushstring(lstate, funcName);       // push key onto stack
    lua_pushcfunction(lstate, funcPointer); // push value onto stack
    lua_settable(lstate, -3);               // add key-value pair to table

    lua_pop(lstate, 1);                     // pop table from stack
}
Run Code Online (Sandbox Code Playgroud)

  • 为什么不使用luaL_register? (4认同)