lua-users home
lua-l archive

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


Hi Peter
 
Thanks for the prompt reply, and correcting my misunderstanding. I have now written that code and tested with a string > 2000 chars, it was fairly simple to write and worked perfectly without me needing to expand the default stack.
 
At first glance it is a little counter intuitive knowing you are passing a ton of data to Lua via stack api operations and still only needing a small amount of stack. That's my excuse anyways :)
 
Regards Geoff
 
> Date: Sat, 25 Jun 2011 12:59:47 +0200
> From: peter@peterodding.com
> To: lua-l@lists.lua.org
> Subject: Re: Guidance on how to write this Lua C Binding ?
>
> Hi Jeff,
>
> > I want to write a C function that takes a UTF8 Lua string and in C break
> > it down into codepoints, each codepoint I want to insert into a Lua
> > table using the API and return that table to Lua. I am OK with
> > the UTF8 parsing (I have already written that) what I am unsure about is
> > how to return the table to Lua when it has potentially 1000 or so
> > codepoints in the table ?
> >
> > I have previously written a binding that populates a small table and
> > returns it to Lua, this was maybe 5 elements max, so I didnt even need
> > to worry about the stack size.
> >
> > My first question is, is this same technique still valid and sound if
> > you have to potentially push a few thousand elements onto the stack to
> > return a big table ?
>
> If you extract a code point from the string and immediately add it to
> the table, the code point won't be on the stack right? Tables take up
> one slot of the stack no matter how many values they contain. So as long
> as you add each code point to the table immediately you shouldn't need
> to worry about the size of the Lua stack.
>
> > if the answer to that is yes, can you point me at an example of how to
> > grow the stack to suit as needed, and what do I need to do to shrink the
> > stack when I have finished with this function ?
>
> As I tried to explain above you don't need to grow the stack to do what
> you want to do. However should you need to grow the stack you can call
> lua_checkstack() to ensure enough space on the stack:
>
> http://www.lua.org/manual/5.1/manual.html#lua_checkstack
>
> > If however returning a big table by pushing it all on the stack, is not
> > sound practice, how should this be achieved instead ? Any links to study ?
>
> The table can be as big as you want it to, there's nothing unsound about
> this! :-)
>
> - Peter
>