Re: opaque pointers and metatype

  • From: Justin Cormack <justin@xxxxxxxxxxxxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 2 Mar 2014 11:20:44 +0000

On Sun, Mar 2, 2014 at 3:09 AM, Cosmin Apreutesei
<cosmin.apreutesei@xxxxxxxxx> wrote:
> I also wanted to mention a solution that I have found for this:
>
> Replacing
>
>    typedef struct OpaqueStruct * handle;
>
> with
>
>    typedef struct {void* p} handle;
>
> and metatype'ing on handle, solves all the problems, i.e. it allows me to
>   - avoid creation of 1-element arrays
>   - hook the C methods that take handle* as the first argument into
> the metatype directly, without a Lua wrapper.
>
> What do you think, is this a good way to do it? It seems better than
> the previous one to me.
>

It still allocates a single element struct, but thats unavoidable of
course. I do think it is nicer, and I do use a fair number of dummy
structs like this. I do feel a bit type-system violating passing a
struct not a pointer but the ABI is the same so it doesn't matter.

Justin

Other related posts: