Skip to content

Instantly share code, notes, and snippets.

@titpetric
Created March 10, 2017 10:37
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save titpetric/47894158c1bfcf19e1b6353f6e4c63a9 to your computer and use it in GitHub Desktop.
Save titpetric/47894158c1bfcf19e1b6353f6e4c63a9 to your computer and use it in GitHub Desktop.
LUA FFI bridge to Go shared Lib
local ffi = require("ffi")
local awesome = ffi.load("./awesome.so")
ffi.cdef([[
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef double GoFloat64;
typedef struct { const char *p; GoInt n; } GoString;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
extern GoInt Add(GoInt p0, GoInt p1);
extern GoFloat64 Cosine(GoFloat64 p0);
extern void Sort(GoSlice p0);
extern GoInt Log(GoString p0);
]]);
io.write( string.format("awesome.Add(12, 99) = %f\n", math.floor(tonumber(awesome.Add(12,99)))) )
io.write( string.format("awesome.Cosine(1) = %f\n", tonumber(awesome.Cosine(1))) )
local nums = ffi.new("long long[5]", {12,54,0,423,9})
local numsPointer = ffi.new("void *", nums);
local typeSlice = ffi.metatype("GoSlice", {})
local slice = typeSlice(numsPointer, 5, 5)
awesome.Sort(slice)
io.write("awesome.Sort([12,54,9,423,9] = ")
for i=0,4 do
if i > 0 then
io.write(", ")
end
io.write(tonumber(nums[i]))
end
io.write("\n");
local typeString = ffi.metatype("GoString", {})
local logString = typeString("Hello LUA!", 10)
awesome.Log(logString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment