我想注册一个Lua C模块,我需要这样使用。Lua C模块:困惑包括成员
local harfbuzz = require 'harfbuzz'
-- initialize blob
local blob = harfbuzz.Blob.new(…)
print(blob:length())
我的理解是,我要创建一个新表和元表添加到它与方法,然后添加该表作为成员Blob
到顶级LIB表。
这是我C文件中的相关代码片段。我不太确定register_blob
函数中包含哪些内容。我尝试了一些东西,但他们没有工作。
static const struct luaL_Reg blob_methods[] = {
{ "length", blob_length },
{"__gc", blob_destroy },
{ NULL, NULL },
};
static const struct luaL_Reg blob_functions[] = {
{ "new", blob_new },
{ NULL, NULL }
};
static const struct luaL_Reg lib_table [] = {
{"version", get_harfbuzz_version},
{NULL, NULL}
};
int register_blob(lua_State *L) {
// QUESTION: What should I include here
}
int luaopen_luaharfbuzz (lua_State *L) {
lua_newtable(L);
register_blob(L);
luaL_setfuncs(L, lib_table, 0);
return 1;
}
感谢您撰写这样一个全面的答案。 – vyom
当然,您可以将注册表传递给Lua:'lua_pushvalue(L,LUA_REGISTRYINDEX)'。这就是'debug.getregistry'所做的... – siffiejoe
@siffiejoe:注意到并修复。 –