开发者

How difficult is the LuaJIT FFI?

开发者 https://www.devze.com 2023-02-25 04:07 出处:网络
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.

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开发者_StackOverflow 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?


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


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))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号