lua-users home
lua-l archive

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

In my view, crossing lua/c bound is a expensive thing.
May you have to implement whole module in c, and wrap some simple interfaces to lua .
Take care of GC, do not make a lot of garbage.

On Mon, Dec 14, 2009 at 15:43, Dimitris Papavasiliou <> wrote:
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.


On Sun, Dec 13, 2009 at 12:35 AM, Jim Pryor <> 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)

local function byte_array_set(self, k, val)

local function byte_array_len(self)

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)

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

Jim Pryor

Linker Lin