lua_checkstack and luaL_checkstack

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

lua_checkstack and luaL_checkstack

Aladdin LampÃ

Hi!
In some C source interfacing with Lua I found lots of:

lua_checkstack(L, nb);

with no return value check. Is it of any use or can I either
- remove them?
- use luaL_checkstack(L, nb, "out of memory"); instead?

What would you recommend? Is it really necessary to check the stack each time you push something to it or will the VM raise the "out of memory" error anyway?

Thank you!
Aladdin

int lua_checkstack (lua_State *L, int extra);
Ensures that there are at least extra free stack slots in the stack. It returns false if it cannot grow the stack to that size. This function never shrinks the stack; if the stack is already larger than the new size, it is left unchanged.

void luaL_checkstack (lua_State *L, int sz, const char *msg);
Grows the stack size to top + sz elements, raising an error if the stack cannot grow to that size. msg is an additional text to go into the error message.

_________________________________________________________________
Téléphonez gratuitement à tous vos proches avec Windows Live Messenger  !  Téléchargez-le maintenant ! 
http://www.windowslive.fr/messenger/1.asp

Reply | Threaded
Open this post in threaded view
|

Re: lua_checkstack and luaL_checkstack

Patrick Donnelly-3
On Thu, Sep 11, 2008 at 3:09 AM, Aladdin Lampà <[hidden email]> wrote:
>
>
> Hi!
> In some C source interfacing with Lua I found lots of:
>
> lua_checkstack(L, nb);
>
> with no return value check. Is it of any use or can I either
> - remove them?
> - use luaL_checkstack(L, nb, "out of memory"); instead?
>
> What would you recommend? Is it really necessary to check the stack each time you push something to it or will the VM raise the "out of memory" error anyway?
>
> Thank you!
> Aladdin

You should check the return value. Not doing so can lead to you
pushing items over the stack top causing a segmentation fault.
luaL_checkstack is the best option IME.

Cheers,

-- 
-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."

-Will Durant


Reply | Threaded
Open this post in threaded view
|

Re: lua_checkstack and luaL_checkstack

Aladdin LampÃ
In reply to this post by Aladdin LampÃ

Thank you Patrick.

If I understand correctly, this means that I should first have a call to luaL_checkstack() each time I use one of the following functions:
- lua_pushboolean
- lua_pushcclosure
- lua_pushcfunction
- lua_pushfstring
- lua_pushinteger
- lua_pushlightuserdata
- lua_pushliteral
- lua_pushlstring
- lua_pushnil
- lua_pushnumber
- lua_pushstring
- lua_pushthread
- lua_pushvalue
- lua_pushvfstring

Is there any way of telling the Lua interpreter once-for-all to grow the stack "when needed", "if possible" and "with a specified message in case of error" instead of a segmentation fault?

Thank you!
Aladdin

On Thu, Sep 11, 2008 at 3:09 AM, Aladdin Lampé  hotmail.fr> wrote:
>
>
> Hi!
> In some C source interfacing with Lua I found lots of:
>
> lua_checkstack(L, nb);
>
> with no return value check. Is it of any use or can I either
> - remove them?
> - use luaL_checkstack(L, nb, "out of memory"); instead?
>
> What would you recommend? Is it really necessary to check the stack each time you push something to it or
will the VM raise the "out of memory" error anyway?
>
> Thank you!
> Aladdin

You should check the return value. Not doing so can lead to you
pushing items over the stack top causing a segmentation fault.
luaL_checkstack is the best option IME.

Cheers,

--
-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."

-Will Durant



_________________________________________________________________
Installez gratuitement les 20 émôticones Windows Live Messenger les plus fous ! Cliquez ici !
http://www.emoticones-messenger.fr/

Reply | Threaded
Open this post in threaded view
|

Re: lua_checkstack and luaL_checkstack

David Jones-2

On 11 Sep 2008, at 10:32, Aladdin Lampé wrote:



Thank you Patrick.

If I understand correctly, this means that I should first have a call to luaL_checkstack() each time I use one of the following functions:
- lua_pushboolean
- lua_pushcclosure
- lua_pushcfunction
- lua_pushfstring
- lua_pushinteger
- lua_pushlightuserdata
- lua_pushliteral
- lua_pushlstring
- lua_pushnil
- lua_pushnumber
- lua_pushstring
- lua_pushthread
- lua_pushvalue
- lua_pushvfstring

Is there any way of telling the Lua interpreter once-for-all to grow the stack "when needed", "if possible" and "with a specified message in case of error" instead of a segmentation fault?

No, but it's rarely needed. Whenever Lua calls C it ensures that there are at least 20 (or LUA_MINSTACK) slots available on the Lua stack for your C code to use. Therefore if your C code doesn't push more than 20 items, you'll be fine. If your C code does push more than 20 items, but you can get some bound on the number, then you can check once at the beginning of the C function that you have enough space on the stack.

You only need to repeatedly call checkstack if you have a C function that uses an amount of Lua stack that grows without bound (for example, a loop that pushes lots of strings onto the stack, the concatenates them in one go at the end of the loop). Such cases are rare, and there are usually better ways of doing it.

Cheers,
 drj