[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Wrapping a Lua table around a C array
- From: Jim Pryor <lists+lua@...>
- Date: Sat, 12 Dec 2009 17:35:53 -0500
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