Overriding FFI ctype constructors?

  • From: Claire Lewis <claire_lewis@xxxxxxxxxxx>
  • To: <luajit@xxxxxxxxxxxxx>
  • Date: Fri, 8 Jun 2012 15:28:41 +1030


Is there some way to provide my own constructor for a ctype in FFI? 
Specifically for an opaque type that I have a C call to create, such as:


  ffi.cdef[[
    struct OpaqueType;
    struct OpaqueType *NewOpaqueType();
    ...
  ]]


  OpaqueType = ffi.typeof("struct OpaqueType;")


  myot = OpaqueType(); -- error, unknown size

Ideally, I'd like to be able to set up OpaqueType to call NewOpaqueType(), or a 
Lua function so I can do the same, and massage arguments as needed.

Currently I'm doing so by doubling up symbols:

  _OpaqueType = ffi.typeof("struct OpaqueType;") -- type
  OpaqueType = ffi.test.NewOpaqueType -- constructor

But this is not as clean as I would like; I'd prefer to have the ctype still be 
represented by the same name, as with other structs.

Thankyou,

- Claire                                          

Other related posts: