alien
alien copied to clipboard
an easy way to check for the existence of a symbol
I'm writing some Lua code (GNU Zile) that would gain a huge speed boost by using memrchr
to scan back through an alien.buffer
-- but memrchr
is a GNU extension not available on Mac OS X among others. I have a brute force reverse search implemented in Lua over an alien.array
, but I'd like to have that as a fallback preferring memrchr
from libc when available.
Unfortunately, alien
throws an error during parsing:
$ lua52 -l alien -e 'print (tostring (alien.default.memchr)))'
alien function memchr, library default
$ lua52 -l alien -e 'print (tostring (alien.default.memrchr)))'
/usr/local/opt/lua52/bin/lua: dlsym(RTLD_DEFAULT, memrchr): symbol not found
stack traceback:
[C]: in function '__index'
(command line):1: in main chunk
[C]: in ?
$ lua52 -l alien -e 'if false then print (tostring (alien.default.memrchr))) end'
/usr/local/opt/lua52/bin/lua: dlsym(RTLD_DEFAULT, memrchr): symbol not found
stack traceback:
[C]: in function '__index'
(command line):1: in main chunk
[C]: in ?
The best workaround I have is to write a test function in a string, and call loadstring() inside pcall to protect from the parse-time error.
Much better would be simply return nil
for missing symbols, and then I could write much cleaner code:
local def = alien.default
if def.memrchr ~= nil then
def.memrchr:types ("pointer", "pointer", "int", "size_t")
else
function memrchr (buf, ch, o) ... end
end
Thoughts? I'll try to write a patch if you agree this is a good way forward.