lua-users home
lua-l archive

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


Hi,

Iam a newbie to lua, so pls let me know if the  basic
approach itself is wrong in lua and c++ ,or iam
missing something.
I have commented for some appoaches i had taken and
errors i have received.

problem:

1.I need to push error stack trace from lua to c++ and
write it to a log file from c++
Iam not able to communicate b/w lua and c++ properly

2.Iam attaching the code for ur ref.

------------------------------------------------------
local Results = { xpcall( dofile( "./scripts/tr.lua"
),debug.traceback ) }

Iam able to write the debug,traceback info to a .txt
file directly from lua
I need this trace to be passed through c++ code to
write it to a log file.

-----------------------------------------------------
Code:



-- My lua script
local Results = { xpcall( dofile( "./scripts/tr.lua"
),debug.traceback ) }
local Result1 = { xpcall( dofile( "./scripts/a.lua"
),lscript:screrrHandler() ) }
local Result2 = { xpcall( dofile( "./scripts/b.lua"
),lscript:scrHook() ) }


//My C++ code

Function called when Lua generates errors during
execution.

static int screrrHandler( lua_State * state  )
{
	lua_Debug		debugInfo;
	const char		*str;
	int			idx = 0;

	// Get the state information about  error occured in
the script.
	memset( (void*) &debugInfo, 0, sizeof( lua_Debug ) );
	if( lua_getstack( state, 0, &debugInfo ) )
	{
		while( str = lua_getlocal( state, &debugInfo, idx )
)
		{
			++idx;
		}
		lua_getinfo( state, "nl", &debugInfo );
	}

	return 0;
}


// Debugging hook function used to trace through
script execution.

static void scrHook( lua_State * state, lua_Debug * ar
)
{

	lua_gettop( state );

	// Not working properly
	// Get the state information about where the error
occured in the script.
	lua_getinfo( state, "nSl", ar );
	if( !ar->source || !ar->name )
	{
		return;
	}


}

namespace lscript
{

//Initialize script

bool lnScript::init()
{
	L = lua_open();
	if( !L )
		return false;
	luaL_openlibs( L );

	//not sure if this is right approach
	//lua_register( L, "screrrHandler", screrrHandler );
//pops error
	lua_sethook( L, scrHook, LUA_MASKLINE, 0 );

	//Does not work as scrHook 2nd parm cannot be
converted to lua_CFunction
	//more over we need 2 args for scrHook whick
lua_register does not accept.
	//Ignore this approach
	//lua_register( L, "screrrHandler", screrrHandler );
	//lua_register( L, "scrHook", scrHook );



	return true;
}


//Execute Lua code either from a buffer or from file

bool lnScript::exec( const char *script, bool isFile )
{
	int	err;

		// load the file into the interpreter.
		err = luaL_loadfile( L, script );

		// Load our error handler so that we can trace back
on script errors
		int base = lua_gettop( L );  // function index

		// following error is popped  here
		//-error C2664: 'lua_pushcclosure' : cannot convert
parameter 2 from 'int' to 'lua_CFunction'
		lua_pushcfunction( L, screrrHandler( L ) );

		lua_insert( L, base );

		//Does not work as scrHook 2nd parm cannot be
converted to lua_CFunction
		//more over we need 2 args for scrHook whick
lua_register does not accept.
		//Ignore this approach

		//lua_register( L, "screrrHandler", screrrHandler );
		//lua_register( L, "scrHook", scrHook );

		err = lua_pcall( L, 0, 0, 0 );

return true;
}

}

_________________________________________________________________
Catch all the cricketing action right here. Live score, match reports, photos et al. http://content.msn.co.in/Sports/Cricket/Default.aspx