lua-users home
lua-l archive

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


I know metamethods for ctypes are a TODO, but I was just wondering if
the __gc metamethod is one of the ones that will be supported?  Or
whether there's some reason why this would not be possible.

My use case is that I want to expose a refcounted C structure to Lua via
the FFI.  When Lua no longer needs the structure, I want to unref it
using a C function.

One other question: I would prefer to make the Lua API for these
structure members look like regular field accesses, ie:

  -- User doesn't know it, but foo is actually a ctype.
  foo = MyStructure()
  print(foo.bar)
  foo.bar = 5

However I need to hook in some of my own code in some of these
operations, like when "foo.bar = 5" happens I need it to also execute
"foo.has_bar = true", where has_bar is a separate structure member.
I could do this as a function:

  function MyStructure:set_bar(bar)
    self.bar = bar
    self.has_bar = true
  end

I guess I'm wondering if there is any way to get this same efficiency
(since I expect LuaJIT FFI will optimize set_bar to be basically perfect)
while providing users the "foo.bar = 5" syntax instead of foo:set_bar(5).
If not, I'll just go with the method syntax, since I don't want to paint
myself into an efficiency corner just to get nicer syntax.

Josh