lua-users home
lua-l archive

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


Daurnimator wrote:
> It seems you can't apply a metatype to a pointer:

Yes. It's even documented. :-)

> Will metamethods on a struct activate when I do it on a pointer to
> them? (Otherwise, I would like to request some sort of automatic
> de-referencing)

Yes, unless the pre-defined pointer operations override it:

  local ffi = require("ffi")
  ffi.cdef[[typedef struct { int x; } foo_t]]
  local foo = ffi.metatype("foo_t", {
    __index = function() return 1234 end,
    __sub =   function() return 9876 end,
  })

  local s = foo()
  print(s.bar)       --> 1234
  print(s[0])        --> 1234
  print(s - s)       --> 9876

  local p = ffi.new("foo_t *")
  print(p.bar)       --> 1234
  print(p[0])        --> cdata<struct 95 &>: ... (pointer dereference)
  print(p - p)       --> 0 (pointer difference)
  -- But:
  print(p[0][0])     --> 1234

--Mike