2012/12/3 Lance:
> 首先说明一下,这个应该是与 luajit 的 namespaces 设计有关,似乎不是 openresty 的问题,但鉴于我只在使用 openresty
> 的时候才会用到 luajit,所以发到这里看看哪位有什么好办法。
[...]
> 同时 error.log 里出现如下错误
> 2012/12/04 11:54:49 [error] 10709#0: *17 lua entry thread aborted: runtime
> error: /usr/local/openresty/nginx/conf/luapkg/b.lua:15: attempt to redefine
> 'in_addr'
[...]
>
> 假如在大量使用 ffi 的场合,这种重复定义似乎很难避免,哪位有什么好办法?
>
这是使用 LuaJIT FFI 时常见的一个错误,即通过 ffi.cdef 重复定义同名的 C 函数或者 C 结构体。
一个解决的办法是总是使用 ffi.typeof 函数在定义前进行存在性测试,例如
local ffi = require "ffi"
if pcall(ffi.typeof, "struct in_addr") then
-- already defined! do nothing here...
else
-- undefined! let's define it!
ffi.cdef[[
struct in_addr {
uint32_t s_addr;
};
]]
end
Best regards,
-agentzh
--
邮件自: 列表“openresty”,专用于技术讨论!
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
详情: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
建议: 提问的智慧 http://wiki.woodpecker.org.cn/moin/AskForHelp
教程: http://agentzh.org/misc/nginx/agentzh-nginx-tutorials-zhcn.html
2012/12/4 Lance:
> 那类似
> ffi.cdef[[
>
> int inet_aton(const char *cp, struct in_addr *inp);
> ]]
>
> 这样怎么判存在呢?似乎 ffi.typeof 不行
>
对于 C 函数也很简单,可以如是判断:
local C = ffi.C
if pcall(function () return C.inet_aton end) then
-- inet_aton defined
else
-- inet_aton not defined
end
Best regards,
-agentzh