1
0
mirror of https://github.com/lxsang/antd-lua-plugin synced 2025-07-24 01:40:09 +02:00

ffi via libffi (contd)

This commit is contained in:
lxsang
2019-04-30 20:09:07 +02:00
parent ef51f7f700
commit 0ac583407d
6 changed files with 397 additions and 1 deletions

6
lib/ffi/example/Makefile Normal file
View File

@ -0,0 +1,6 @@
CC = gcc
main: lib.o
$(CC) -shared lib.o -o libtest.so
%.o: %.c
$(CC) -fPIC -c $< -o $@

View File

@ -0,0 +1,45 @@
local ffi = require("ffi")
local path = "/home/mrsang/Desktop/testffi/libtest.so"
local lib = nil
local fn = nil
local rettype = nil
local argtype = nil
if ffi then
-- now ffi exist
-- try to load the test library
lib = ffi.dlopen(path)
if lib then
-- now try to lookup for the greet function
echo("looking for greet")
fn = ffi.dlsym(lib,"greet")
if fn then
-- now the function found
-- tried to called it
rettype = ffi.atomic_type(0) -- void
if rettype then
argtype = ffi.atomic_type(20) -- pointer
if(argtype) then
-- call the function
local r = ffi.call(rettype, {argtype}, fn, {"hello world"})
if r then
echo("BIG SUCCESS")
else
echo("HELL FAIL")
end
else
echo("argtype not found")
end
else
echo("return type not found")
end
else
echo("unable to find greet")
end
ffi.dlclose(lib)
else
echo("unable to load the lib")
end
else
echo("cannot find ffi")
end
echo("end the day")

6
lib/ffi/example/lib.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
void greet(const char* msg)
{
printf("%s\n", msg);
}