lua-users home
lua-l archive

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


Daurnimator wrote:
> Ended up with this: https://gist.github.com/902845
> You now have pointer objects, which are small wrappers around cdata.
>
> @Mike Pall: any comments on the efficiency of this?

Oh dear, all these abstractions certainly look inefficient. I'm
not so sure what the compiler would make of this in a real-world
use case.

> Could we perhaps get a newuserdata(size) function built into the ffi?
> (so, pretty much newproxy with a size argument)

I'm reluctant to add that, since it's a temporary workaround.

My plan is to add metamethods for cdata objects. This is a much
better solution, but I don't know when I get around to implement
it. It would look something like this:

  local point
  local mt = {
    __add = function(a, b) return point(a.x+b.x, a.y+b.y) end,
    ... etc. ...
  }
  point = ffi.metatype("struct { int x, y; }", mt)

  local a = point(1, 10)
  local b = point(20, 3)
  local c = a + b
  print(c.x, c.y)   --> 21 13

You can only set a metatable for structs/unions or complex/vector
types (you can wrap other types in a struct, if needed). The
metatable can only be set once for each type. It automatically
applies to all instances of this type. Neither the metatable nor
the __index table must be modified afterwards. The names of the
declared fields have precedence; __index and __newindex will only
be consulted if the key doesn't match a declared field name.

Would that work for your use case?

--Mike