Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

an easy way to check for the existence of a symbol #39

Open
gvvaughan opened this issue Jan 18, 2014 · 0 comments
Open

an easy way to check for the existence of a symbol #39

gvvaughan opened this issue Jan 18, 2014 · 0 comments

Comments

@gvvaughan
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant