lua-users home
lua-l archive

Re: FFI metamethods on C struct pointer

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sat, Jun 11, 2011 at 9:20 AM, Mike Pall <mikelu-1106@mike.de> wrote:
>> am i missing something obvious?
>
> Well, that should work just fine. How did you define the struct?
> You realize that each "struct {...}" defines a _unique_ struct,
> even if it has the same members? Better use a named struct or a
> typedef everywhere.
that's good news, at least there's some hope that if i bang my head
hard enough, something should fall off. :-)
yes, i use typedef everywhere, and ffi.typeof() as soon as possible:
i also tried def
> Maybe posting a simplified excerpt of your code would help.
> Quite often the problem is solved during that process. :-)
of course, i did some minimal tests that _do_ work, so i then spent
some hours trying to make it fail.
for example, this works perfectly:
local ffi = require "ffi"
ffi.cdef [[
	typedef struct bstr {
		size_t len;
		const char *str;
	} bstr;
]]
bstr_t = ffi.typeof ('bstr')
ffi.metatype (bstr_t, {
	__tostring = function (b)
		return ffi.string (b.str, b.len)
	end,
})
local b = bstr_t(3, 'hi!')
print (type (b), b)
===> cdata hi!
i guess the main difference is that here i'm using the FFI constructor
(bstr_t(...)) and not receiving a struct pointer from a C function.
i'm trying to find some constructor-like function in the stdlib to use
as example.
-- 
Javier

AltStyle によって変換されたページ (->オリジナル) /