10

I recently looked into Lua and it seems really nice. The only annoying thing is its lack of (standard) libraries. But with the JIT compiler comes along a nice FFI C interface.

Coming from a java background, i tried to avoid C as much as possible, so my question: has anyone some experience with LuaJIT, especially its FFI interface, and how difficult is it to set up a library for someone with little to no knowledge in C?

RBerteig
  • 41,948
  • 7
  • 88
  • 128
Moe
  • 1,021
  • 1
  • 9
  • 16

2 Answers2

16

Seemed really simple to me, and Mike Pall has some nice tutorials on it here, the lua mailing list also includes some good examples, so check out the archives as well

Michal Kottman
  • 16,375
  • 3
  • 47
  • 62
Necrolis
  • 25,836
  • 3
  • 63
  • 101
2

how difficult is it to set up a library for someone with little to no knowledge in C?

Really easy. First, you need to declare the functions that you'd like to use. Then, load the target library and assign it to a Lua variable. Use that variable to call the foreign functions.

Here's an example on using function powf from C's math library.

local ffi = require("ffi")

-- Whatever you need to use, have to be declared first
ffi.cdef([[
   double powf(double x, double y); 
]])

-- Name of library to load, i.e: -lm (math)
local math = ffi.load("m")

-- Call powf
local n, m = 2.5, 3.5
print(math.powf(n, m))
Diego Pino
  • 11,278
  • 1
  • 55
  • 57
  • Unfortunatelly the example does not work. ;) `powf` takes and returns `float`s not `double`s. A valid declaration should read: ```ffi.cdef([[ float powf(float x, float y); ]])``` – jpc Oct 30 '18 at 23:41
  • You're right. `powf` takes floats as arguments, not double. Thanks for pointing that out. – Diego Pino Nov 05 '18 at 08:12