3

CreateEntity 是我在项目中绑定到 Lua 的 C 函数。它将实体类名称字符串作为第一个参数,以及应传递给所选实体的构造函数的任意数量的附加参数。

例如,如果 CreateEntity 是一个普通的 Lua 函数,我可以这样做:

function CreateEntity( class, ... )  
    -- (choose a constructor function based on class)
    args = {...}
    -- (store args somewhere for whatever reason)
    TheConstructor( ... )  
end

但是我怎么能用 C Lua 函数做到这一点呢?

4

2 回答 2

4

C 函数lua_gettop将返回传递给 C 函数的参数数量。您必须从堆栈中读取所有这些并将它们存储在 C 数据结构中,或者将它们放在 Lua 注册表中(请参阅RegistryluaL_ref)并存储对它们的引用以供以后使用。下面的示例程序使用注册表方法。

#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <stdio.h>
#include <stdlib.h>

/* this function prints the name and extra variables as a demonstration */
static void
TheConstructor(lua_State *L, const char *name, int *registry, int n)
{
    int i;

    puts(name);

    for (i = 0; i < n; ++i) {
        lua_rawgeti(L, LUA_REGISTRYINDEX, registry[i]);
        puts(lua_tostring(L, -1));
    }

    free(registry);
}

static int
CreateEntity(lua_State *L)
{
    const char *NAME = luaL_checkstring(L, 1);
    int *registry;
    int i, n;

    /* remove the name parameter from the stack */
    lua_remove(L, 1);

    /* check how many arguments are left */
    n = lua_gettop(L);

    /* create an array of registry entries */
    registry = calloc(n, sizeof (int));
    for (i = n; i > 0; --i)
        registry[i-1] = luaL_ref(L, LUA_REGISTRYINDEX);

    TheContructor(L, NAME, registry, n);

    return 0;
}

int
main(int argc, char **argv[])
{
    const char TEST_CHUNK[] =
        "CreateEntity('foo', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)";
    lua_State *L;

    L = luaL_newstate();
    lua_register(L, "CreateEntity", CreateEntity);
    luaL_dostring(L, TEST_CHUNK);
    lua_close(L);

    return EXIT_SUCCESS;
}
于 2011-03-28T13:53:06.753 回答
1
args = {...}
-- (store args somewhere for whatever reason)

调用的参数在 Lua 堆栈上,您可以随意使用它们:将它们放在您自己的结构中(std::vector<boost::any>或类似的结构)或将单个参数存储在Lua 注册表中或使用参数创建 Lua 表并将其存储在注册表中。什么更适合你?

TheConstructor( ... )

我相当确信这部分在 C++ 中不可能像在 Lua 中那样。C++ 要求在编译时知道传递给函数的参数数量。

尝试在 C++ 中做这些事情是一个很大的不便。如果您告诉我们为什么希望您的 CreateEntity 函数位于 C++ 端而不是 Lua 端,也许会出现更好的解决方案。

于 2011-03-27T14:36:11.377 回答