Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How difficult is the LuaJIT FFI?

Tags:

c

lua

ffi

luajit

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?

like image 724
Moe Avatar asked Apr 17 '11 08:04

Moe


2 Answers

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

like image 80
Necrolis Avatar answered Sep 20 '22 05:09

Necrolis


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))
like image 42
Diego Pino Avatar answered Sep 19 '22 05:09

Diego Pino