当我将Lua整合到我的C程序中时,我一直在使用指向C结构的指针来存储我需要在与Lua绑定的方法中重用的对象州。在Lua注册表中存储C结构
但是,一旦我从主程序中分离出我的Lua库文件,这就不起作用,所以它似乎需要使用注册表来存储我的结构。
我该如何去存储我的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,所以我目前正在研究这个。