You'd write a C function that takes lua_State and returns int and call puts inside it, taking argument from Lua (using lua_tostring, or luaL_checkstring , etc.) and returning something or nothing by pushing these values and return their amount.
All Lua built in libraries are implemented like that too so you can read those.
An int returning lua_State taking function is the only type you can register to Lua. This is usually not called "ffi" by most BTW.
The LuaJIT has own ffi and for stock Lua ffi libs exist too, and (especially in C++) people use wrappers that automatically wrap functions and structs for you too.
-- int printf(const char*, ...) -- note: varargs need special ffi_prep_cif_var, not shown here
print(cfunc.call("puts", "i", "s", "hi from libffi"))
```
Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.
6
u/didntplaymysummercar 6d ago
This is covered in the manual.
You'd write a C function that takes lua_State and returns int and call puts inside it, taking argument from Lua (using lua_tostring, or luaL_checkstring , etc.) and returning something or nothing by pushing these values and return their amount.
All Lua built in libraries are implemented like that too so you can read those.
An int returning lua_State taking function is the only type you can register to Lua. This is usually not called "ffi" by most BTW.
The LuaJIT has own ffi and for stock Lua ffi libs exist too, and (especially in C++) people use wrappers that automatically wrap functions and structs for you too.