有關這個問題的代碼是在這裏:https://github.com/jchester/lua-polarssl/tree/master/src爲Lua包裝一個C庫:我如何創建函數的嵌套表?
目前我正在試圖包裹PolarSSL庫(http://polarssl.org)的一部分給我訪問SHA-512 HMACs(luacrypto做不提供這個)。
我的目標的API的形式爲:
a_sha512_hash = polarssl.hash.sha512('text')
或更充分
local polarssl = require 'polarssl'
local hash = polarssl.hash
a_sha512_hash = hash.sha512('test')
如果你是指在鏈接polarssl.c上面,你會看到我已經編寫了包裝PolarSSL代碼的函數。然後,我想這樣來構建功能表:
LUA_API int luaopen_polarssl(lua_State *L) {
static const struct luaL_Reg core[] = {
{ NULL, NULL }
};
static const struct luaL_Reg hash_functions[] = {
{ "sha512", hash_sha512 },
{ "sha384", hash_sha384 },
{ NULL, NULL }
};
static const struct luaL_Reg hmac_functions[] = {
{ "sha512", hmac_sha512 },
{ "sha384", hmac_sha384 },
{ NULL, NULL }
};
luaL_register(L, CORE_MOD_NAME, core);
luaL_register(L, HASH_MOD_NAME, hash_functions);
luaL_register(L, HMAC_MOD_NAME, hmac_functions);
return 1;
}
其中CORE_MOD_NAME = 'polarssl',HASH_MOD_NAME = 'polarssl.hash',HMAC_MOD_NAME = 'polarssl.hmac'。
當我運行在這個問題的頂部類似的Lua代碼的測試腳本,我得到這個:
lua: test.lua:23: attempt to index global 'polarssl' (a nil value)
stack traceback:
test.lua:23: in main chunk
[C]: ?
我試圖尋找如何實現這個module.submodule方法的例子(例如naim vs luasockets),但每個人似乎都有不同的實現方式。我完全失去了。
我無法鏈接到naim和luasockets,因爲我點擊了鏈接上的<10點限制。 – 2012-03-02 03:18:58
看起來業力就像美味的糖果一樣分發,所以更新後的鏈接。 – 2012-03-02 03:31:40