What I want to avoid is to capture/ignore the exception when FFI calls a non-existent method.
For example, the following code calls the non_existent_method
. However, pcall
cannot handle the error.
local ffi = require "ffi"
local C = ffi.C
ffi.cdef [[
int non_existent_method(int num);
]]
local ok, ret = pcall(C.non_existent_method, 1)
if not ok then
return
end
I got the following error with OpenResty/lua-nginx-module.
lua entry thread aborted: runtime error: dlsym(RTLD_DEFAULT, awd): symbol not found
2
Answers
One possible solution would be wrapping the
C.non_existent_method
call with a lua function.For example
One more apporach would be to call the index metamethod directly:
You might want to wrap that into a function:
example: