[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua calling C question
- From: Robert Aronsson <robert.aronsson@...>
- Date: Sun, 1 May 2005 03:14:28 +0200
Its almost there :)
concider this class (C++)
class TLuaSocket
{
// Function reads iDataSize bytes from a socket and place it in pData, pData
// must be allocated by calling lua program so we get proper
garbage collection
int Read(char *pData,int iDataSize);
}
Now, lets pretend that I complied this with tolua++, then I could
write a lua snippet like this and execute it
socket = TLuaSocket:new()
a = string.rep(" ", 1024)
socket:Read(a,1024);
print(a)
Its vey possible that I've missed some vital point here, but is this
(string:rep(x.y)) the _only_ way to allocate a char buffer in lua ?
/RA
On 4/30/05, Asko Kauppi <asko.kauppi@sci.fi> wrote:
>
> If you mean allocating a buffer where the data is read (by C) and using
> it then in Lua, why not simply give a proper userdata block length (you
> can use a structure with 'data[0]' as a "growing tail" in most
> compilers). Then, you'd attack __index metamethod to the userdata so
> it seems like a usual Lua (read-only) table to Lua scripts. Would this
> be enough?
>
> Other alternative (since it's char*) would be pushing a specific length
> string, but this might need an interim buffer malloced/freed by you
> (since Lua strings are read-only).
>
> Did I even touch close? :)
>
> btw, Why is the iMaxSize a reference (you're using C++ here, right?).
> That is, is the 'Read' function actually setting/writing it, is it
> input only, or mixed input-output. Please specify.
>
> -ak
>
> 30.4.2005 kello 13:26, Robert Aronsson kirjoitti:
>
> Hi guys,
> >
> > Excuse me if this has been posted before but I cant find it in the
> > archives.
> >
> > I need to call a c function from lua that looks like this
> >
> > bool Read(char *pData,unsigned int &iMaxSize);
> >
> > The Read function will read some data of max iMaxSize and place it in
> > the pre-allocated pData char pointer, returns true on success or false
> > on failure.
> >
> > My question is, how do I pre-allocate an array in Lua that I can use
> > to send into this C function? (without doing a for loop that adds
> > strings to a variable for example)
> >
> > Best Regards,
> > Robert Aronsson
> >
>
>