1
0
mirror of https://github.com/lxsang/antd-lua-plugin synced 2025-01-03 21:08:22 +01:00
antd-lua-plugin/lib/ffi/example/example.lua

87 lines
3.0 KiB
Lua
Raw Normal View History

2019-05-03 13:17:45 +02:00
local path = "/home/blackjack/workspace/ant-http/plugins/antd-lua-plugin/lib/ffi/example/libtest.so"
2019-05-08 20:55:23 +02:00
local path = "/Users/mrsang/Google Drive/ushare/cwp/ant-http/plugins/antd-lua-plugin/lib/ffi/example/libtest.so"
2019-05-01 22:06:48 +02:00
require("cif")
2019-04-30 20:09:07 +02:00
local lib = nil
local fn = nil
local rettype = nil
local argtype = nil
2019-05-01 22:06:48 +02:00
lib = FFI.load(path)
if lib then
-- now try to lookup for the greet function
fn = FFI.lookup(lib,"greet")
if fn then
2019-05-03 13:17:45 +02:00
-- now the function
2019-05-01 22:06:48 +02:00
-- tried to called it
2019-05-03 13:17:45 +02:00
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)
2019-04-30 20:09:07 +02:00
else
2019-05-03 13:17:45 +02:00
echo("HELL FAIL")
2019-04-30 20:09:07 +02:00
end
end
2019-05-03 13:17:45 +02:00
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))
2019-05-08 20:55:23 +02:00
echo(tostring(FFI.bytearray(buff,5)[1]))
FFI.byteAtPut(buff,0, 11)
echo(tostring(FFI.byteAt(buff,0)))
2019-05-03 13:17:45 +02:00
end
2019-05-08 20:55:23 +02:00
local size = 1024
local struct_ptr = FFI.new(12)
local buff = FFI.new(5)
FFI.byteAtPut(buff,0,64)
FFI.byteAtPut(buff,1,65)
FFI.byteAtPut(buff,2,66)
FFI.byteAtPut(buff,3,67)
FFI.byteAtPut(buff,4,0)
FFI.byteAtPut(struct_ptr, 0, 10)
FFI.byteAtPut(struct_ptr, 1, 100)
FFI.byteAtPut(struct_ptr, 2, 0) -- pad
FFI.byteAtPut(struct_ptr, 3, 0) -- pad
FFI.byteAtPut(struct_ptr, 4, size & 0xFF)
FFI.byteAtPut(struct_ptr, 5, (size >> 8) & 0xFF)
FFI.byteAtPut(struct_ptr, 6, (size >> 16) & 0xFF)
FFI.byteAtPut(struct_ptr, 7, (size >> 24) & 0xFF)
FFI.atPutPtr(struct_ptr, 8, buff)
2019-05-03 13:17:45 +02:00
2019-05-08 20:55:23 +02:00
fn = FFI.lookup(lib, "test_struct_ptr")
2019-05-08 18:49:40 +02:00
if(fn) then
2019-05-08 20:55:23 +02:00
echo("calling test_struct_ptr")
FFI.call(FFI.atomic(FFI.type.SINT),{FFI.atomic(FFI.type.POINTER)}, fn, {struct_ptr})
2019-05-08 18:49:40 +02:00
end
2019-05-08 20:55:23 +02:00
2019-05-01 22:06:48 +02:00
FFI.unloadAll()
2019-04-30 20:09:07 +02:00
end
echo("end the day")