FFI to conveniently export Lua functions as a DLL?

102 views
Skip to first unread message

Björn Buckwalter

unread,
Mar 28, 2026, 4:24:52 PM (9 days ago) Mar 28
to lua-l
 Hi all,

The LuaJIT FFI library[1]  and Alien[2] make it very convenient to call external (C) libraries, by providing C declarations from header files verbatim. Is there similar tooling but for exporting Lua functions as a DLL based?

Thanks,
Bjorn

ppp vvv

unread,
Mar 30, 2026, 7:27:23 AM (7 days ago) Mar 30
to lua-l
There is already luaL_dostring in lua.dll that allows to call lua code from C, but you would need to handle arguments and return values manually.
With some preprocessor magic / C++ templates, you could automatically generate code to call corresponding lua_pushXXX() functions to pass arguments to Lua function before lua_pcall, and then lua_toXXX to get result so it would look like a C function.

Also alien.callback could convert Lua function into C function pointer.

And there is another but primitive FFI for Lua https://github.com/pavel212/uffi (sysV a bit broken, but windows should be ok).
given Lua function could be placed in LUA_REGISTRYINDEX, and then function make_cb  (https://github.com/pavel212/uffi/blob/11967d07e0fcb2ca8758b410dbae2702a812893e/src/win64.c#L112) would convert it into C function.
It would generate binary code, that could be called then as a C function pointer, and it would get/convert arguments according to provided signature, pass it to lua function, call lua function and return the result pretending it is just a C function.

суббота, 28 марта 2026 г. в 21:24:52 UTC+1, Björn Buckwalter:
Reply all
Reply to author
Forward
0 new messages