Re: ffi.getmetatable

  • From: Peter Colberg <peter@xxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 26 Nov 2014 16:26:21 -0500

On Wed, Nov 26, 2014 at 11:51:07AM -0800, Ronan Collobert wrote:
> More generally when one hides something in the metatable (a global
> property of the “class” of a given object), right now one cannot
> retrieve it easily. Once in while, the case occurs, someone
> complains to me, and one has to play with the
> tonumber(ffi.typeof())+additional table trick.

Did you know this feature of ctypes?

  local ffi = require("ffi")
  local ctype = ffi.metatype("struct { int i; }", {
    __index = {
      class = "MyClass",
      size = function(self)
        return ffi.sizeof(self)
      end,
    }
  })

  assert(ctype.class == "MyClass")
  assert(ctype:size() == 4)

  local obj = ctype(123)
  local objtype = ffi.typeof(obj)
  assert(objtype.class == "MyClass")
  assert(objtype:size() == 4)

That is the X to your Y :-).

Peter

Other related posts: