1 Star 3 Fork 0

赵建辉/lua-ffi

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README
MIT

lua-ffi

license PRs Welcome Issue Welcome Release Version Build Status 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 */
    };

    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

Basic types supported

void bool char short int long float double

int8_t int16_t int32_t int64_t uint8_t uint16_t uint32_t uint64_t

ino_t dev_t gid_t mode_t nlink_t uid_t off_t pid_t size_t ssize_t useconds_t suseconds_t blksize_t blkcnt_t time_t

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

Testing

Acknowledgements

This project was inspired by the following repositories:

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

MIT License Copyright (c) 2022 Jianhui Zhao <zhaojh329@gmail.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Lua-ffi is a portable C FFI for Lua, based on libffi and aiming to be mostly compatible with LuaJIT FFI. expand collapse
C and 4 more languages
MIT
Cancel

Releases

No release

Contributors

All

Activities

Load More
can not load any more
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/zhaojh329/lua-ffi.git
git@gitee.com:zhaojh329/lua-ffi.git
zhaojh329
lua-ffi
lua-ffi
main

Search