lua-users home
lua-l archive

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

On Sunday 20, Josh Haberman wrote:
> 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.

You can wrap it in a newproxy() and set the __gc metamethod in the newproxy's 

> 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(
> = 5
> However I need to hook in some of my own code in some of these
> operations, like when " = 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)
> = 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 " = 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.

How about something like this?

-- warning un-tested code.
function MyStructure()
  -- create cdata
  local cdata ='MyStructure')
  local self = newproxy(true)
  local meta = getmetatable(self)
  meta.cdata = cdata
  -- maybe '__index' can set to 'cdata'
  meta.__index = function(tab, key)
    return cdata[key]
  meta.__newindex = function(tab, key, value)
    cdata[key] = value
    cdata["has_" .. key] = true
  meta.__gc = function()
    -- TODO: unref cdata.
  return self -- we return the newproxy() object here, not the cdata.

Robert G. Jakabosky