lua-users home
lua-l archive

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


Hello Jim,

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.

Dimitris.

On Sun, Dec 13, 2009 at 12:35 AM, Jim Pryor <lists+lua@jimpryor.net> wrote:
On Sat, Dec 12, 2009 at 07:08:27PM +0200, Dimitris Papavasiliou wrote:
>    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?

Here's a sketch. You may want to do some of this in C.

local function byte_array_get(self, k)
   ...
end

local function byte_array_set(self, k, val)
   ...
end

local function byte_array_len(self)
   ...
end

local byte_array_prototype=newproxy(true)
local mt = getmetatable(byte_array_prototype)
mt.__index = byte_array_get
mt.__newindex = byte_array_set
mt.__len = byte_array_len

function byte_array()
   return newproxy(byte_array_prototype)
end

local b = byte_array()
b[1]=2
print(b[1]) -- 2
print(#b) -- 1

--
Jim Pryor
profjim@jimpryor.net