[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Luajit cdata with metatables....
- From: Daurnimator <quae@...>
- Date: Sun, 3 Apr 2011 16:02:00 +1000
On 31 March 2011 01:54, Daurnimator <quae@daurnimator.com> wrote:
> (sorry, this question was sent to old list address; and was meant to
> be posted before my follow up question: "luajit FFI cdata problem")
>
> Hi List,
>
> I had a search of the list archives and nothing came up on the topic...
>
> I would like to attach metamethods to some luajit2 ffi cdata
> objects... Whats the best way to go about this?
>
> eg:
> local foo = ffi.new("int[3]",5,6,7)
> local t = { [0]=x , y , z }
> setmetatable(foo,{__index=function(ob,key) return ob[t[key]] end })
>
> First thought to come to mind is newproxy... but luajit probably has a
> smarter/better way?
>
> Daurn.
>
> Addendum:
> I was trying to use 2 tables as a back and forth conversion of cdata
> and userdata; but cdata as keys in a table seems to be a bad idea.
> local edge_methods = {} -- methods to support.
>
> local tocdata = setmetatable ( { } , { __mode = "k" } )
> local touserdata
> touserdata = setmetatable ( { } , {
> __mode = "kv" ;
> __index = function ( t , cdata )
> local v = newproxy ( true )
> getmetatable ( v ).__index = function ( udata , k )
> local method = edge_methods [ k ]
> if method then
> return method
> else
> local cdata = tocdata [ udata ] [ k ]
> local udata = touserdata [ cdata ]
> return udata
> end
> end
> getmetatable ( v ).__newindex = function ( udata , k , v )
> tocdata [ udata ] [ k ] = v
> end
> getmetatable ( v ).__tostring = function ( udata )
> return "Dart" .. tostring ( tocdata [ udata ] ):match("cdata(<.*>)")
> end
>
> tocdata [ v ] = cdata
> rawset ( t , cdata , v )
> return v
> end ;
> } )
>
Anyone care to help?
Daurn