lua-users home
lua-l archive

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


On Sat, Jun 11, 2011 at 10:49 AM, Javier Guerra Giraldez
<javier@guerrag.com> wrote:
> 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!


ah, but this doesn't:

local ffi = require "ffi"

ffi.cdef [[
	struct _IO_FILE;
	typedef struct _IO_FILE FILE;

	FILE *fopen (__const char *__restrict __filename,
		    __const char *__restrict __modes);
]]

local file_t = ffi.typeof ('FILE')

ffi.metatype (file_t, {
	__tostring = function (f)
		return "it's a FILE"
	end,
})

local f = ffi.C.fopen ('txt', 'r')

print (type(f), f)

===> cdata   cdata<struct _IO_FILE *>: 0x40353b68

it never calls my __tostring() metamethod. it seems that it's because
the cdata contains a FILE*, and not a FILE.  but i can't do:

ffi.metatype ('FILE *')

nor:

local file_tp = ffi.typeof ('FILE *')
ffi.metatype (file_tp, {.....})


-- 
Javier