lua-users home
lua-l archive

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


Thanks for everyone's help in the past, this time I have a, hopefully, simple question.

How do I run both an _ALERT and a _TRACEBACK function?

For certian types of errors such as running code like "aa aa" the code in block 1 below works best and gives a more meaningful error message (1:"= expected near aa"). In other cases where multiple functions are called the _TRACEBACK gives the best results since you can more easily debug the problem. But, using the _TRACEBACK function with code like "aa aa" gives the cryptic error "error while running chunk" which is no help at all without a line number.

The problem I have is that I cannot seem to string the two together. I suppose it's a problem with not using the stack correctly, but I don't know what I'm doing wrong.

Thanks for any help,
    John Labenski




For example run the code "aa aa"

BLOCK 1 message :
My error: hello:1: `=' expected near `aa'

BLOCK 2 message :
"Lua error:
attempt to call a table value
stack traceback:"

// Code to run the string
int ret_val = luaL_loadbuffer(m_lua_State, wx2lua(script), script.Len(), "=hello");

************ BLOCK 1 ***********************
// This code copied from src/lib/lauxlib.c

if (ret_val == 0)  // parse OK?
   ret_val = lua_pcall(m_lua_State, 0, LUA_MULTRET, 0);  // call main

if (ret_val != 0)
{
  lua_getglobal(m_lua_State, "_ALERT");

  if (0 && lua_isfunction(m_lua_State, -1)) // not run!
  {
    lua_insert(m_lua_State, -2);
    lua_call(m_lua_State, 1, 0);
  }
  else // no _ALERT function; print it on stderr
  {
     //fprintf(stderr, "%s\n", lua_tostring(L, -2));
     fprintf(stderr, "My error: %s\n", lua_tostring(m_lua_State, -2));
     lua_pop(m_lua_State, 2);  // remove error message and _ALERT
  }
}
************ END BLOCK 1 ***********************

************ BLOCK 2 ***********************
// This code copied from src/lua/lua.c

//if (ret_val == 0)
{

  int narg = 0;
  int clear = 1;

  int base = lua_gettop(m_lua_State) - narg;  // function index
  lua_pushliteral(m_lua_State, "_TRACEBACK");
  lua_rawget(m_lua_State, LUA_GLOBALSINDEX);  // get traceback function
  lua_insert(m_lua_State, base);  // put it under chunk and args
  L = m_lua_State;
  //signal(SIGINT, laction);
  ret_val = lua_pcall(m_lua_State, narg, (clear ? 0 : LUA_MULTRET), base);
  //signal(SIGINT, SIG_DFL);
  lua_remove(m_lua_State, base);  // remove traceback function

  if (ret_val)
  {
     const char *msg = lua_tostring(m_lua_State, -1);
     if (msg == NULL) msg = "(error with no message)";
     printf("Lua error: \n%s\n", msg);
     lua_pop(m_lua_State, 1);
  }
}
************ END BLOCK 2 ***********************