Re: assigning FFI metatype & gc to typedef'd struct ptr

  • From: Mike Pall <mike-1205@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Mon, 14 May 2012 11:33:20 +0200

GrrrWaaa wrote:
> According to [1] a metatype cannot be attached to a pointer
> type, but instead to the underlying object. This doesn't work
> out-of-the-box for opaque struct pointers, unless the struct is
> also typedef'd:

This is not true.

> ffi.cdef[[
> typedef struct OpaqueObject OpaqueObject;             // won't work without 
> this line
> typedef struct OpaqueObject * ObjectPtr;
> ]]
> 
> local mt = {}
> -- <configure mt here>
> ffi.metatype(ffi.typeof("OpaqueObject"), mt)

Of course the first declaration is needed here because you are
referring to "OpaqueObject", which isn't defined without this.
We're talking about C declarations, not C++. Or simply use
"struct OpaqueObject" which works without the first declaration,
because it's implicitly declared by the second declaration.

Also, you can strip that ffi.typeof(). ffi.metatype() directly
accepts an abstract type declaration (and returns a ctype object):

local ootype = ffi.metatype("struct OpaqueObject", mt)

--Mike

Other related posts: