Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
zhaojh329 committed Jun 23, 2024
0 parents commit 5a8a795
Show file tree
Hide file tree
Showing 16 changed files with 3,304 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Environment

* arch:
* os:
* version:

## Description

```
Formating code blocks by wrapping them with pairs of ```
```
32 changes: 32 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: build
on: push
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- version: 5.1
pkg: liblua5.1-0-dev
macro: USE_LUA51
- version: 5.2
pkg: liblua5.2-dev
macro: USE_LUA52
- version: 5.3
pkg: liblua5.3-dev
macro: USE_LUA53
- version: 5.4
pkg: liblua5.4-dev
macro: USE_LUA54
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: build
run: |
sudo apt update
sudo apt install -y libffi-dev lua${{ matrix.version }} ${{ matrix.pkg }}
cmake . -D${{ matrix.macro }}=ON && make && sudo make install
gcc -shared -fPIC tests/test.c -o libtest.so
lua${{ matrix.version }} ./tests/basetype.lua
lua${{ matrix.version }} ./tests/test.lua
66 changes: 66 additions & 0 deletions .github/workflows/formal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Test Formalities

on:
pull_request:

permissions:
contents: read

jobs:
build:
name: Test Formalities
runs-on: ubuntu-latest
strategy:
fail-fast: false

steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0

- name: Determine branch name
run: |
BRANCH="${GITHUB_BASE_REF#refs/heads/}"
echo "Building for $BRANCH"
echo "BRANCH=$BRANCH" >> $GITHUB_ENV
- name: Test formalities
run: |
source .github/workflows/scripts/ci_helpers.sh
RET=0
for commit in $(git rev-list HEAD ^origin/$BRANCH); do
info "=== Checking commit '$commit'"
if git show --format='%P' -s $commit | grep -qF ' '; then
err "Pull request should not include merge commits"
RET=1
fi
author="$(git show -s --format=%aN $commit)"
if echo $author | grep -q '\S\+\s\+\S\+'; then
success "Author name ($author) seems ok"
else
err "Author name ($author) need to be your real name 'firstname lastname'"
RET=1
fi
subject="$(git show -s --format=%s $commit)"
if echo "$subject" | grep -q -e '^[0-9A-Za-z,+/_\.-]\+: ' -e '^Revert '; then
success "Commit subject line seems ok ($subject)"
else
err "Commit subject line MUST start with '<area>: ' ($subject)"
RET=1
fi
body="$(git show -s --format=%b $commit)"
sob="$(git show -s --format='Signed-off-by: %aN <%aE>' $commit)"
if echo "$body" | grep -qF "$sob"; then
success "Signed-off-by match author"
else
err "Signed-off-by is missing or doesn't match author (should be '$sob')"
RET=1
fi
done
exit $RET
26 changes: 26 additions & 0 deletions .github/workflows/scripts/ci_helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

color_out() {
printf "\e[0;$1m%s\e[0;0m\n" "$2"
}

success() {
color_out 32 "$1"
}

info() {
color_out 36 "$1"
}

err() {
color_out 31 "$1"
}

warn() {
color_out 33 "$1"
}

err_die() {
err "$1"
exit 1
}
86 changes: 86 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
cmake_minimum_required(VERSION 3.5)

project(lua-ffi C)

include(FindPkgConfig)

# Version settings
set(LUA_FFI_VERSION_MAJOR 1)
set(LUA_FFI_VERSION_MINOR 0)
set(LUA_FFI_VERSION_PATCH 0)

# Define options for selecting Lua versions
option(USE_LUA51 "Force select Lua5.1")
option(USE_LUA52 "Force select Lua5.2")
option(USE_LUA53 "Force select Lua5.3")
option(USE_LUA54 "Force select Lua5.4")

# Helper function to find and include Lua
function(find_and_include_lua version)
pkg_search_module(LUA lua-${version})
if (LUA_FOUND)
include_directories(${LUA_INCLUDE_DIRS})
else()
message(FATAL_ERROR "Liblua${version} is required.")
endif()
endfunction()

if (LUA_INCLUDE_DIR)
include_directories(${LUA_INCLUDE_DIR})
else()
# Check which Lua version to use
if(USE_LUA51)
find_and_include_lua(5.1)
set(LUA_VERSION_MAJOR 5)
set(LUA_VERSION_MINOR 1)
elseif(USE_LUA52)
find_and_include_lua(5.2)
set(LUA_VERSION_MAJOR 5)
set(LUA_VERSION_MINOR 2)
elseif(USE_LUA53)
find_and_include_lua(5.3)
set(LUA_VERSION_MAJOR 5)
set(LUA_VERSION_MINOR 3)
elseif(USE_LUA54)
find_and_include_lua(5.4)
set(LUA_VERSION_MAJOR 5)
set(LUA_VERSION_MINOR 4)
else()
find_package(Lua REQUIRED)
include_directories(${LUA_INCLUDE_DIR})
endif()
endif()

find_package(FLEX REQUIRED)

pkg_search_module(LIBFFI libffi)
if (NOT LIBFFI_FOUND)
message(FATAL_ERROR "libffi is required.")
endif()

add_compile_options(-D_GNU_SOURCE -DLUA_USE_LINUX -Os -Wall -Werror --std=gnu99 -fno-strict-aliasing)

# configure a header file to pass some of the CMake settings to the source code
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)

include_directories(${LIBFFI_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})

if (NOT LUA_INSTALL_PREFIX)
if (LUA_VERSION_MAJOR AND LUA_VERSION_MINOR)
set(LUA_INSTALL_PREFIX lib/lua/${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR})
else()
set(LUA_INSTALL_PREFIX lib/lua)
endif()
endif()

flex_target(cparser lex.l ${CMAKE_CURRENT_BINARY_DIR}/lex.c
DEFINES_FILE ${CMAKE_CURRENT_BINARY_DIR}/lex.h)

add_library(lffi MODULE ffi.c ${CMAKE_CURRENT_BINARY_DIR}/lex.c)
target_link_libraries(lffi PRIVATE ${LIBFFI_LIBRARIES})
set_target_properties(lffi PROPERTIES OUTPUT_NAME ffi PREFIX "")

install(
TARGETS lffi
DESTINATION ${LUA_INSTALL_PREFIX}
)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Jianhui Zhao <[email protected]>

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.
117 changes: 117 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# lua-ffi

[1]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=plastic
[2]: /LICENSE
[3]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=plastic
[4]: https://github.com/zhaojh329/lua-ffi/pulls
[5]: https://img.shields.io/badge/Issues-welcome-brightgreen.svg?style=plastic
[6]: https://github.com/zhaojh329/lua-ffi/issues/new
[7]: https://img.shields.io/badge/release-1.0.0-blue.svg?style=plastic
[8]: https://github.com/zhaojh329/lua-ffi/releases
[9]: https://github.com/zhaojh329/lua-ffi/workflows/build/badge.svg

[![license][1]][2]
[![PRs Welcome][3]][4]
[![Issue Welcome][5]][6]
[![Release Version][7]][8]
![Build Status][9]
![visitors](https://visitor-badge.laobi.icu/badge?page_id=zhaojh329.lua-ffi)

[LuaJIT]: https://luajit.org/
[cffi-lua]: https://github.com/q66/cffi-lua
[libffi]: https://sourceware.org/libffi/

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

```c
// create a file named add.c and compile it to a dynamic lib.
// gcc -o libadd.so -fPIC -shared add.c
int add(int x, int y)
{
return x + y;
}
```
```lua
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);
int add(int x, int y);
]])
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
local mylib = ffi.load('./libadd.so')
print('add:', mylib.add(1, 2))
```

## 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](/tests)

## Acknowledgements

This project was inspired by the following repositories:

- [cffi-lua]

Thanks to the authors of these repositories for their excellent work.
14 changes: 14 additions & 0 deletions config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* SPDX-License-Identifier: MIT */
/*
* Author: Jianhui Zhao <[email protected]>
*/

#ifndef _LUA_FFI_CONFIG_H
#define _LUA_FFI_CONFIG_H

#define LUA_FFI_VERSION_MAJOR @LUA_FFI_VERSION_MAJOR@
#define LUA_FFI_VERSION_MINOR @LUA_FFI_VERSION_MINOR@
#define LUA_FFI_VERSION_PATCH @LUA_FFI_VERSION_PATCH@
#define LUA_FFI_VERSION_STRING "@LUA_FFI_VERSION_MAJOR@.@LUA_FFI_VERSION_MINOR@.@LUA_FFI_VERSION_PATCH@"

#endif
Loading

0 comments on commit 5a8a795

Please sign in to comment.