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:
6
lib/ffi/example/Makefile
Normal file
6
lib/ffi/example/Makefile
Normal file
@ -0,0 +1,6 @@
|
||||
CC = gcc
|
||||
main: lib.o
|
||||
$(CC) -shared lib.o -o libtest.so
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -fPIC -c $< -o $@
|
45
lib/ffi/example/example.lua
Normal file
45
lib/ffi/example/example.lua
Normal 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
6
lib/ffi/example/lib.c
Normal file
@ -0,0 +1,6 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void greet(const char* msg)
|
||||
{
|
||||
printf("%s\n", msg);
|
||||
}
|
Reference in New Issue
Block a user