我想註冊一個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:注意到並修復。 –