在Lua注册表中存储C结构

时间:2015-09-24 07:08:08

标签: c struct lua lua-api

当我将Lua集成到我的C程序中时,我一直在使用static指向C结构的指针来存储我需要在绑定到Lua的方法中重用的对象状态。

但是,一旦我将Lua lib从主程序中分离出来,这不起作用,所以我似乎需要使用注册表来存储我的结构。

如何将我的C结构指针存储在Lua注册表中?

这就是我目前正在做的事情:

static augeas *aug = NULL;


static int lua_aug_get(lua_State *L) {
    // Use aug to do something here
    /* return the number of results */
    return 1;
}

struct lua_State *luaopen_augeas(augeas *a) {
    lua_State *L = luaL_newstate();
    aug = a; // this clearly does not work
    luaL_openlibs(L);
    // The methods below will need to access the augeas * struct
    // so I need to push it to the registry (I guess)
    static const luaL_Reg augfuncs[] = {
        { "get", lua_aug_get },
        { "label", lua_aug_label },
        { "set", lua_aug_set },
        { NULL, NULL }
    };

    luaL_newlib(L, augfuncs);
    lua_setglobal(L, "aug");

    return L;
}

编辑:根据我在IRC上的回答,似乎我应该使用metatable,所以我现在正在研究这个问题。

2 个答案:

答案 0 :(得分:3)

如果注册表不是足够安全的地方来存储指针,您可以将其作为特定函数的upvalue推送:

static int lua_aug_get(lua_State *L) {
  augeas *aug = lua_touserdata(L, lua_upvalueindex(1));
  // Do stuff with aug
  return 1;
}

static const luaL_Reg augfuncs[] = {
    { "get", lua_aug_get },
    { "label", lua_aug_label },
    { "set", lua_aug_set },
    { NULL, NULL }
};
lua_createtable(L, 0, 0);
for (size_t i = 0; augfuncs[i].name; i++) {
    lua_pushlightuserdata(L, a);
    lua_pushcclosure(L, augfuncs[i].func, 1);
    lua_setfield(L, -2, augfuncs[i].name);
}

但可以将其存储在注册表中。它不是脚本可访问的,除了debug库,通常不会在沙箱中公开。如果其他图书馆在那里造成自私的混乱,你无论如何都会遇到麻烦。

答案 1 :(得分:1)

我设法使用Lua注册表索引并将指针推送为 light userdata:

static const char *Key = "augeas_registry_key"; // The registry key

static augeas *checkaug(lua_State *L) {
  lua_pushlightuserdata(L, (void *)&Key);        // set the registry key
  lua_gettable(L, LUA_REGISTRYINDEX);            // retrieve value
  augeas *aug = (augeas *)lua_touserdata(L, -1); // cast value
  return aug;
}

static int lua_aug_get(lua_State *L) {
  augeas *aug = checkaug(L);
  // Do stuff with aug
  return 1;
}

struct lua_State *luaopen_augeas(augeas *a) {
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    lua_pushlightuserdata(L, (void *)&Key); // set registry key
    lua_pushlightuserdata(L, (void *)a);    // push pointer
    lua_settable(L, LUA_REGISTRYINDEX);     // push to in registry

    static const luaL_Reg augfuncs[] = {
        { "get", lua_aug_get },
        { "label", lua_aug_label },
        { "set", lua_aug_set },
        { NULL, NULL }
    };

    luaL_newlib(L, augfuncs);
    lua_setglobal(L, "aug");

    return L;
}

虽然这不是很优雅,考虑到它使用共享注册表,在会话期间加载的其他库可以访问它,所以我仍然可以选择更好的选项。

相关问题