lua-users home
lua-l archive

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

 So i have a little code that figures out the current stack level...

	int lbserver::_get_stack_level( lua_State* L )
		lua_Debug info;
		memset( &info, 0, sizeof( info ) );
		int levels = 0;
		while ( lua_getstack( L, levels, &info ) == 1 ) {

		// returns 0 if we're at the bottom of the 
		// stack and levels - 1 for the top.
		assert( levels > 0 );
		return levels - 1;

 I use this when a client initially connects to the debug server.  Every
thing worked great until I switched from my test script to a bigger embedded
lua app.  My hook would sometimes generate negative stack levels, so I added
in an assert and found that at times the stack level doesn't match my

	if ( ar->event == LUA_HOOKCALL ) {


		// This should never fail... but does!
		assert( _level == _get_stack_level( L ) );

	} else if ( ar->event == LUA_HOOKRET || 
			ar->event == LUA_HOOKTAILRET ) {
		// This should never fail... but does!
		assert( _level == _get_stack_level( L ) );


 Currently I have this happening from within a tail call, so I guess I don't
really understand how they work.  Isn't a call stack from within a tail call
still supposed to include the caller?  If not how does a debugger properly
report a call stack for tail calls?

  Tom Spilman
  Co-owner | Programmer