[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Wrapping a Lua table around a C array
- From: Dimitris Papavasiliou <dpapavas@...>
- Date: Mon, 14 Dec 2009 09:43:15 +0200
thanks for the reply but that's the approach I've tried so far although I implemented it in C. The problem with this approach is that this proxy does not completely behave like a table. I need to find the length of the array from within C when reading the data so I use lua_objlen for this. (Remember that I need to be able to accept normal tables as well as wrapped C arrays). Unfortunately objlen doesn't honor the __len metamethod so the result won't be correct.
On Sun, Dec 13, 2009 at 12:35 AM, Jim Pryor <firstname.lastname@example.org>
On Sat, Dec 12, 2009 at 07:08:27PM +0200, Dimitris Papavasiliou wrote:Here's a sketch. You may want to do some of this in C.
> So the question is: apart from the obvious solutions of implementing my
> own lua_objlen, hacking the internals of Lua etc. can you think of a slick
> way to create a table or userdata object that will contain a reference to
> a string with binary data and provide set, get and length access to it so
> that it can pose as a normal Lua table?
local function byte_array_get(self, k)
local function byte_array_set(self, k, val)
local function byte_array_len(self)
local mt = getmetatable(byte_array_prototype)
mt.__index = byte_array_get
mt.__newindex = byte_array_set
mt.__len = byte_array_len
local b = byte_array()
print(b) -- 2
print(#b) -- 1