jmckaskill / luaffi

Standalone FFI library for calling C functions from lua. Compatible with the luajit FFI interface.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

metatype initialization issue

justincormack opened this issue · comments

this code

local ffi = require "ffi"

local fd = ffi.metatype("struct {int fileno;}", {})

local test = fd(2);

print(test.fileno)

Works fine in luajit, and prints 2.

In luaffi I get

lua: unable to convert argument 2 from lua<number> to cdata<struct 2>
stack traceback:
    [C]: in function 'fd'
    tmp.lua:5: in main chunk
    [C]: ?

Unfortunately I use this idiom a lot as thats the only sane way to have garbage collection on file descriptors, by adding __gc method to fd. There would be a fair amount of workaround to avoid this usage, by explicitly initializing ie this which does work:

local ffi = require "ffi"

local fd = ffi.metatype("struct {int fileno;}", {})

local test = fd();

test.fileno = 2

print(test.fileno)

Thanks for the report. This is actually a dup of #25 where the ffi.new, and ctype() code does not handle pre-flattened arguments.

For example if I change:

  local test = fd(2);

to

local test = fd({2});

Then it works.