我正在研究在luajit ffi中包装libcurl . 我的终结者没有被召唤 .
local ffi = require("ffi")
ffi.cdef [[
typedef struct{} CURL;
CURL * curl_easy_init();
void curl_easy_cleanup(CURL *);
]]
local CURL_lib = ffi.load("../lib/libcurl.so")
local CURL_CTX
local CURL_CTX_mt = {
__gc = function() print "finalizing"; CURL_lib.curl_easy_cleanup(CURL_CTX); end
}
ffi.metatype("CURL", CURL_CTX_mt)
CURL_CTX = ffi.new("CURL[1]")
CURL_CTX = CURL_lib.curl_easy_init();
print "done"
我在这里想念的是什么? :d
BTW CURL定义为 typedef void CURL;
I 'm sure the way I am trying to do it isn' t足够干净 . 任何建议?
eureka ! : self answer -- still interested in comments if there any glaring issues.
2 回答
如果将上下文类型初始化为指针,则luajit中的编译时间类型必须为
structs
(或unions
我认为),它不再是struct
. 因此,这里存在概念不匹配 . 因此,要修复问题,请在struct
中添加void *
,在struct
上挂起metatype
,并使用void *
作为库上下文 .或者您可以在返回的指针上注册终结器,如下所示: