Re: How to call functions from a static library in Luajit

  • From: bellasys <bellasys@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sat, 24 Sep 2016 18:29:25 -0700

On Sat, Sep 24, 2016 at 11:25 AM, Demi Obenour <demiobenour@xxxxxxxxx>
wrote:

FFI is much faster (as fast as a C function call).  C API calls must go
through the interpreter.

On Sep 24, 2016 10:51 AM, "Fu, Qiaobin" <qiaobinf@xxxxxx> wrote:

Thanks very much for the detailed explanation, @Bellasys! And the
comments from @Coda + @mejedi.

Indeed, I am trying to integrate Lua to DPDK application, which is for
fast packet processing (14.88 Million packets per second or more). So, I
need to make a choice between the Lua FFI and the  C binding APIs for
better performance.

If there are any comparison between these methods, it would be perfect
for our project. Thanks for the help!

Best,
Qiaobin


On Sep 23, 2016, at 12:11 AM, bellasys <bellasys@xxxxxxxxx> wrote:

FWIW, there are C bindings via API, and FFI bindings. Using C bindings is
said to be faster (better performance) and this approach will certainly
allow you to achieve calls to a static library. Note C and C++ bindings are
supported via the API.

http://luajit.org/ext_c_api.html

That is a really short read. You are essentially creating wrapper
functions, and then according to the paragraph below, notice the 2 main
options for subsequent function calls:

"...This mode defines a wrapper function for calls to C functions. If
called withLUAJIT_MODE_ON, the stack index at idx must be a lightuserdata 
object
holding a pointer to the wrapper function. From now on all C functions are
called through the wrapper function. If called with LUAJIT_MODE_OFF this
mode is turned off and all C functions are directly called."

I apologize in case you MUST use FFI because in that case my answer is
not a solution.

I really don't know the answer to your specific question because I've
never tried via FFI. I have used the FFI library many times with OpenResty
projects (OpenResty is currently limited to Lua 5.1, although does use
modern LuaJIT, 2.1).

In case anyone from the OpenResty community is trying to do fancy C
bindings, note the 5.3 (or 5.2) compat mode, and the use of a different
function other than lua_requiref() which is useful (or would be useful...)
for Qiaobin. the call is to luaL_requiref() in compat modes, or if you can
use Lua 5.3 it's just lua_requiref() to call libraries. Pretty simple with
the C API from there.

https://github.com/keplerproject/lua-compat-5.3/blob/ba06518
9ae67822ac9c69cc6ba633e3e8e123ef6/c-api/compat-5.3.c#L548

Hopefully this helps you get started.




On Thu, Sep 22, 2016 at 3:41 PM, Fu, Qiaobin <qiaobinf@xxxxxx> wrote:

Hello,

I am a newer for Lua and LuaJIT. Currently, I have a need in a situation
that C host program calls Lua scripts, and the Lua scripts calls C
functions from C modules via Luajit FFI. As far as I know, the FFI library
only allows us to load shared libraries. However, if I want to call C
functions in a static library, is there any way to achieve this via LuaJIT?

I have saw a thread here: //www.freelists.org
/post/luajit/how-to-use-a-library-with-FFI-while-statically-linked. But
I am not quite sure whether it exactly matches mine or not. Any suggestions
are appreciated.

Best,
Qiaobin




--

Be Miraculous!

~Max



After the many explanations (actually after reviewing Mike Pall's
implementation detail), it's clear FFI will be faster, thanks.

@Paul mega-thanks for that link, I'm going to review it as well to see if I
can learn anything to get even more performance in my project!!



-- 

Be Miraculous!

~Max

Other related posts: