Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Is it possible to use Lua/LuaJIT in the opposite FFI direction (aka instead of invoking FFI functions, providing them)

aka, upon DLL invocation (on Win32 they call it DllMain I guess, I've seen it call ctor/dtor on *nix), spawn the runtime, and expose FFI functions?

http://www.drewtech.com/support/passthru.html

This spec is really big on FFI exposing functions. I always find it an edge case when trying to play with certain technologies (like the one in the article).



Lua (and therefore LuaJIT) can easily be driven from a C program, if that's what you mean.

LuaJIT also allows you to pass a Lua function as a callback to a C function expecting one, subject to some limitations.

I'm not sure if either of those answers your question though.


Sorry for the late reply, lost the tab for a bit ._.

Something that might be relevant is that that LuaJIT can JIT-optimize FFI calls from Lua but can't/doesn't optimise calls into Lua made via C.

I might be able to go digging for the reference (at this point figure it's best to just reply for now, not sure if you'll see this) but I've read that "the approach" recommended to solve this problem is to move the main `for(;;)` / `while(1)` loop into Lua and have LuaJIT repeatedly FFI-call C, because that's the path that can go the fastest.


You could export a varargs function and use this to call the appropriate Lua function, I guess.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: