Skip to content

OneLuaPro/lua-ffi

 
 

Repository files navigation

lua-ffi(中文)

license PRs Welcome Issue Welcome Release Version Build Status Downloads visitors

A foreign function interface (FFI) is a mechanism by which a program written in one programming language can call routines or make use of services written or compiled in another one. An FFI is often used in contexts where calls are made into binary dynamic-link library.

Lua-ffi is a portable lightweight C FFI for Lua, based on libffi and aiming to be mostly compatible with LuaJIT FFI, but written from scratch in C language.

Features

  • portable - Used in Lua5.1, Lua5.2, Lua5.3 and Lua5.4.
  • lightweight - Written in C language, very small, only about 50KB.

Example

local ffi = require 'ffi'

ffi.cdef([[
    struct timeval {
        time_t      tv_sec;     /* seconds */
        suseconds_t tv_usec;    /* microseconds */
    };

    struct timezone {
        int tz_minuteswest;     /* minutes west of Greenwich */
        int tz_dsttime;         /* type of DST correction */
    };

    int gettimeofday(struct timeval *tv, struct timezone *tz);
    char *strerror(int errnum);
]])

local function strerror(errno)
    return ffi.string(ffi.C.strerror(errno))
end

local tv = ffi.new('struct timeval')

if ffi.C.gettimeofday(tv, nil) < 0 then
    print('gettimeofday fail:', strerror(ffi.errno()))
else
    print('tv.tv_sec:', tv.tv_sec, 'tv.tv_usec:', tv.tv_usec)
end

Requirements

  • libffi - A portable foreign-function interface library.
  • Lua 5.1 or newer (tested up to and including 5.4).

Build

Ubuntu

sudo apt install -y liblua5.3-dev libffi-dev
git clone https://github.com/zhaojh329/lua-ffi.git
cd lua-ffi && mkdir build && cd build
cmake .. && sudo make install

OpenWrt

Languages  --->
    Lua  --->
        <>  lua-ffi............ A portable lightweight C FFI for lua5.1, based on libffi
        <>  lua-ffi-lua5.3..... A portable lightweight C FFI for lua5.3, based on libffi
        <*> lua-ffi-lua5.4..... A portable lightweight C FFI for lua5.4, based on libffi

Acknowledgements

This project was inspired by the following repositories:

Thanks to the authors of these repositories for their excellent work.

About

A portable lightweight C FFI for Lua, based on libffi and aiming to be mostly compatible with LuaJIT FFI.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • HTML 57.2%
  • C 31.5%
  • Lua 7.0%
  • CMake 3.1%
  • Other 1.2%