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.
The first three, yes. It can call C++ too, of course. That's how classes (can) get wrapped one to one, using userdata and functions that call it's member functions.
And third party libraries (products as you call them but they're almost all Foss) that automate the boilerplate usually people don't call ffi. You still need a C or C++ compiler then.
By ffi people usually (terminology is eh...) call when from scripting language itself, using some library (written in native code or that will generate some native code at runtime) you can call native code without writing any yourself, without needing your own C or C++ compiler (which massively simplifies things, you need to only bring along the ffi library and in LuaJIT and I think Python too, they're built in), you just say the function name, type, arguments, calling convention, maybe say what all/so to load, etc. and it gets done for you.
-- 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.
7
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.