On Tuesday, May 6, 2014 10:53:03 AM UTC+3, Alexandr R. Ogurtzoff wrote:
Thank you.
I'm going to port some Lua libraries to FFI and share to the community
Great to hear that.
Are you looking for porting Lua code to LuaJIT enhanced code (mainly for performance, and memory usage reasons), or are you planning to interface pre-made C-libs with LuaJIT FFI instead? You mentioned FFI, so I guess you are planning to make Lua libs out of C-libs through FFI.
I have done a few small libs, that might help you get going:
1. cJSON: https://github.com/bungle/lua-resty-cjson/blob/master/lib/resty/cjson.lua (https://github.com/bungle/lua-resty-cjson/blob/master/README.md)
2. scrypt: https://github.com/bungle/lua-resty-scrypt/blob/master/lib/resty/scrypt.lua
3. libXL (still in its early steps): https://github.com/bungle/lua-resty-libxl/blob/master/lib/resty/libxl.lua
Basically the thing goes like this:
1. Grap some C-lib
2. Check its headers, and define them in Lua
3. Load that lib with LuaJIT (ffi.load) (it finds them with full path or from package.cpath)
4. Start interfacing C-lib (usually some type juggling is needed, but it's a pretty straight forward)
You don't need to have great knowledge of C when doing this.
Why would you want to do this?
1. There are a lot of great C-libs around, now you just need to have nice Luafied wrapper around it. LuaJITs FFI is a lot easier to work with than the official PuC Lua's C API.
2. It's a lot of fun. This is what drives PHP too. Big strength of PHP is that there are already many bindings to these premade and tried C-libs.
3. There are some performance gains compared to implementing the same with pure Lua (without LuaJIT optimizations)
4. Lua is a great Language, but it needs more libs/bindings to be really usefull in different contexts.
5. You learn.
If you were thinking about the first scenario, that is writing Pure LuaJIT optimized code without C-libs involved, you may look at for example luajit-msgpack-pure:
https://github.com/catwell/luajit-msgpack-pure/blob/master/luajit-msgpack-pure.lua
Kinda like C-code with inline assembler language, or C# or some other with unsafe code regions, but this time Lua and C code mixed, and matched with LuaJIT. With this you need to know a lot more about C-language.
Why would you want to do this?
1. Less dependencies, when you can archieve comparable or better performance with Pure LuaJIT code (compared to LuaJIT FFI with C-.ibs), less installing anything (just copy that .lua file somewhere, and be done with it, less problems with different architectures.
2. If you have a nice pure lua library, but you want to optimize it.
3. You are good in both C, and Lua (well, Lua is a very easy to learn), and want to utilize your knowledge to bring high performant, but more easy to use and abstracted libraries to community, for your own projects, for your higher level coding colleagues...
I'm a little bit curious what you have on your mind? Nice to see new contributions happening in OpenResty/Lua(JIT) scene.
Regards
Aapo