lua-users home
lua-l archive

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


On Sun, May 29, 2011 at 5:24 PM, Duncan Cross <duncan.cross@gmail.com> wrote:
On Sun, May 29, 2011 at 5:13 PM, Justin Cormack
<justin@specialbusservice.com> wrote:
> On 29 May 2011, at 16:58, Duncan Cross <duncan.cross@gmail.com> wrote:
>
>> On Sun, May 29, 2011 at 4:54 PM, Justin Cormack
>> <justin@specialbusservice.com> wrote:
>>>>> Why cant you just add a __tobuffer metamethod to all the types?
>>>>
>>>> getmetatable(cdata) always seems to return 'ffi', currently.
>>>>
>>>
>>> You have to use ffi.metatype() to set a metatable. eg
>>> fd_t = ffi.metatype("struct {int fd;}", {__index = {close = close}, __gc =
>>> close})
>>> This can only be done on creation, but works nicely.
>>
>> Yep, I'm using ffi.metatype - but the metatable you set is still not
>> retrieved by getmetatable(), it still just returns 'ffi' for me. (Is
>> it different for you? Maybe this is a bug?)
>>
>> -Duncan
>>
>
> Why try to get the metatable, just pcall the method to see if it is there. I think it is a feature not a bug in this case.
>

Oh, I see what you mean now. Yes, you're right, it's fair enough for
cdata structs to have to define a method for this. (I would still
quite like to be able to support arbitrary cdata arrays, though.)


You can tell if something is a cdata struct or array by using pcall (a > nil) as a test, as only pointer types can be compared with nil (ie NULL), and arrays and structs will give an exception. There might be another way, but this works. Then if it is a struct or array you can do ffi.sizeof.

ie print(ffi.new("int[3]") > nil) throws an exception but print(ffi.cast("void *", ffi.new("int[3]")) > nil) is true.

Justin