Re: function to check if a symbol in a library exists or not

  • From: Dimiter 'malkia' Stanev <malkia@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 31 Jul 2012 10:08:57 -0700

On 7/31/2012 8:01 AM, Coda Highland wrote:
On Tue, Jul 31, 2012 at 7:19 AM, Duncan Cross <duncan.cross@xxxxxxxxx> wrote:
On Tue, Jul 31, 2012 at 3:03 PM, Coda Highland <chighland@xxxxxxxxx> wrote:
On Tue, Jul 31, 2012 at 3:27 AM, Fjölnir Ásgeirsson
<fjolnir@xxxxxxxxxxxxx> wrote:
Well no. It pcalls, and catches the error. That can't possibly be as fast as just 
calling dlsym straight. (which is how you check for symbols at runtime => I'm 
not sure what the issue is)
Mike Pall said just a few e-mails ago that pcall is extremely cheap,
and it's free if it gets JIT-compiled.
Look again - he clarified that this is only true if the code getting
pcall-ed completes normally (i.e. the first return value of the pcall
is true), and not if an error is involved, which is "very expensive on
some platforms".

-Duncan

Yes, but read the rest of my e-mail: he's only calling it once anyway.
I find this to be the cleanest solution as long as you're not having
to deal with errors in a performance-sensitive place.

/s/ Adam

Consider this approach:

ffi.cdef[[
    void glBegin1(); -- declared but assuming not found in the OpenGL
]]

local ffi = require( "ffi" )
local gl = require( "ffi/OpenGL" )

print()

-- Print error messages
print('glBegin', pcall(function()return ffi.cast( "void*", gl.glBegin )end)) -- present print('glBegin1', pcall(function()return ffi.cast( "void*", gl.glBegin1 )end)) -- declared, but not present print('glBegin2', pcall(function()return ffi.cast( "void*", gl.glBegin2 )end)) -- not declared at all

print()

-- Don't print error messages, just values
print('glBegin', xpcall(function()return ffi.cast( "void*", gl.glBegin )end,function()end)) print('glBegin1', xpcall(function()return ffi.cast( "void*", gl.glBegin1 )end,function()end)) print('glBegin2', xpcall(function()return ffi.cast( "void*", gl.glBegin2 )end,function()end))

It prints out this:

glBegin    true    cdata<void *>: 0x004210e7
glBegin1 false c:/xxxxxxx/:11: cannot resolve symbol 'glBegin1': The specified procedure could not be found. glBegin2 false c:/xxxxxxx/:12: missing declaration for symbol 'glBegin2'

glBegin    true    cdata<void *>: 0x004210e7
glBegin1    false    nil
glBegin2    false    nil



Other related posts: