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

working ffi

This commit is contained in:
lxsang
2019-05-03 13:17:45 +02:00
parent 9c1837d939
commit 0ac77828e7
4 changed files with 338 additions and 95 deletions

View File

@ -1,5 +1,5 @@
local path = "/home/blackjack/workspace/ant-http/plugins/antd-lua-plugin/lib/ffi/example/libtest.so"
require("cif")
local path = "/Users/mrsang/Google Drive/ushare/cwp/ant-http/plugins/antd-lua-plugin/lib/ffi/example/libtest.so"
local lib = nil
local fn = nil
local rettype = nil
@ -7,38 +7,54 @@ local argtype = nil
lib = FFI.load(path)
if lib then
-- now try to lookup for the greet function
echo("looking for greet")
fn = FFI.lookup(lib,"greet")
if fn then
-- now the function found
-- now the function
-- tried to called it
rettype = FFI.atomic(FFI.type.VOID) -- void
if rettype then
argtype = {
FFI.atomic(FFI.type.POINTER),
FFI.atomic(FFI.type.DOUBLE),
FFI.atomic(FFI.type.SINT64),
FFI.atomic(FFI.type.SINT8)
} -- pointer
if(argtype) then
-- call the function
local r = FFI.call(rettype, argtype, fn, {"hello world", 0.987, -76, 65})
if r then
echo("BIG SUCCESS")
else
echo("HELL FAIL")
end
else
echo("argtype not found")
end
rettype = FFI.atomic(FFI.type.UINT8) -- voidn
argtype = {
FFI.atomic(FFI.type.POINTER),
FFI.atomic(FFI.type.DOUBLE),
FFI.atomic(FFI.type.SINT64),
FFI.atomic(FFI.type.SINT8)
} -- pointer
-- call the function
local r = FFI.call(rettype, argtype, fn, {"hello world", 0.987, -76, 65})
if r then
echo(r)
else
echo("return type not found")
echo("HELL FAIL")
end
else
echo("unable to find greet")
end
fn = FFI.lookup(lib, "test_struct")
if fn then
local struct1 = FFI.struct({
FFI.atomic(FFI.type.UINT8), -- char
FFI.atomic(FFI.type.SINT32), -- char
FFI.atomic(FFI.type.SINT16), -- char
FFI.atomic(FFI.type.UINT8), -- char
})
local struct = FFI.struct({
FFI.atomic(FFI.type.UINT8), -- char
struct1,
FFI.atomic(FFI.type.SINT32), -- int
FFI.atomic(FFI.type.UINT8), -- char
})
rettype = struct --FFI.atomic(FFI.type.DOUBLE)
echo("call with struct")
local r = FFI.call(rettype,{struct},fn,{{65, {66, 2048, -97,67},-1024, 68}})
if r then echo(JSON.encode(r)) end
--echo(JSON.encode(FFI.meta(struct1)))
end
fn = FFI.lookup(lib, "test_string")
if(fn) then
local buff = FFI.new(256)
FFI.call(FFI.atomic(FFI.type.VOID),{FFI.atomic(FFI.type.POINTER), FFI.atomic(FFI.type.POINTER)}, fn, {buff,"Hello world"})
echo(FFI.string(buff))
end
FFI.unloadAll()
else
echo("unable to load the lib")
end
echo("end the day")

View File

@ -1,6 +1,34 @@
#include <stdio.h>
void greet(const char* msg, float num, int sint, char c)
typedef struct{
char a;
int b;
short c;
char d;
} inner_t;
typedef struct{
char a;
inner_t b;
int c;
char d;
} test_t;
char greet(const char* msg, float num, int sint, char c)
{
printf("%s: '%f' '%d' '%c'\n", msg, num, sint, c);
return 'A';
}
test_t test_struct(test_t data)
{
printf("data is '%c' '%c' '%d' '%d' '%c' '%d' '%c'\n", data.a, data.b.a, data.b.b, data.b.c, data.b.d, data.c, data.d);
return data;
}
void test_string(char* buff, const char* a)
{
sprintf(buff,"you say %s", a);
printf("%s\n", buff);
}