q66 / cffi-lua

A portable C FFI for Lua 5.1+
MIT License
181 stars 24 forks source link

Support passing struct by value #17

Closed vsergeev closed 4 years ago

vsergeev commented 4 years ago
local ffi = jit and require('ffi') or require('cffi')

--[[
/* testlib.c */
/* gcc -shared testlib.c -o testlib.so */
#include <stdio.h>

struct foo {
    int a;
    int b;
};

void print_foo(struct foo x) {
    printf("foo<%d, %d>\n", x.a, x.b);
}

void print_const_foo(const struct foo x) {
    printf("foo<%d, %d>\n", x.a, x.b);
}
]]--

ffi.cdef[[
struct foo {
    int a;
    int b;
};

void print_foo(struct foo x);
void print_const_foo(const struct foo x);
]]

local testlib = ffi.load('./testlib.so')

local foo = ffi.new("struct foo", 1, 2)

testlib.print_foo(foo) --> expected: foo<1, 2>, get: cannot convert 'struct foo' to 'struct foo'
testlib.print_const_foo(foo) --> expected: foo<1, 2>, get: cannot convert 'struct foo' to 'struct foo'