dynamic ffi function calling in lua

Can we dynamically create a function from string and call a ffi.C binding? example :

ffi.cdef [[

void foo_bar_A_get_info(void);
void foo_bar_B_get_info(void);

]]

some = ffi.load("some.so")
function call_fun(var)
    -- var can be A or B
    some.foo_bar_var_get_info()
end

call_fun("A")
call_fun("B")

I am getting error : missing declaration for symbol 'foo_bar_var_get_info'

I searched online a lot but could not find any way to do it, so wanted to post it here so someone can help.

Upvotes: 1

Views: 526

Answers (1)

Colonel Thirty Two
Colonel Thirty Two

Reputation: 26579

C libraries loaded from the FFI are indexed just like Lua table; you can index them with a raw value in the same way:

some["foo_bar"..some_string.."_baz"]()

Upvotes: 4

Related Questions