lua-users home
lua-l archive

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


On Mon, 2 Mar 2009, Patrick Donnelly wrote:

> On Mon, Mar 2, 2009 at 12:07 PM, M Joonas Pihlaja
> <jpihlaja@cc.helsinki.fi> wrote:
> >
> > Uhm.. sorry if I'm being dense, but isn't growing stack and possibly
> > throwing a memory error the very reason for calling lua_checkstack in
> > the first place?  What on earth is it supposed to do if it can't throw
> > memory errors when it can't grow the stack?
> 
> Because, in preparation for lua_xmove, you will want to ensure stack
> space on the coroutine you are moving values to. However, there is no
> method of checking the stack space on that coroutine without
> _possibly_ causing a panic due to an out of memory error.

Right, just as in your example, but I was saying that I think that 
that's a different issue which doesn't warrant changing the existing 
semantics of lua_checkstack().  Every single use of lua_checkstack() 
I've ever written assumes that it will throw a memory error as per the 
manual[1] and depends on those semantics for safety.  I expect most 
uses of lua_checkstack() are the same, so changing its behaviour in 
that respect now will likely break lots of programs.

Some other alternatives beside's Roberto's that address your problem 
without incompatibly changing existing API:

1) An alternate lua_checkstack() or lua_xmove() that converts the 
exception into an error code instead.

2) An alternate lua_checkstack() or lua_xmove() that takes an extra 
lua_State to throw exceptions on.  Namely the resuming lua_State.

Any of these choices would presumably change coroutine.resume() to 
throw on the resuming thread rather than the resumee.  This isn't IMHO 
an incompatible API change since from the point of view of the caller 
there shouldn't be any difference between running out of memory 
setting up the call of coroutine.resume() vs. pushing the arguments to 
the resumee.

Cheers,

Joonas

[1] Granted, I've never needed to call lua_checkstack on another 
thread as in your case, but that's simply because I haven't used 
coroutines so much.