Lua and error line number

Started by
5 comments, last by floatingwoods 15 years, 6 months ago
Hi, I am using Lua 5.1. When I call my Lua program with lua_pcall and an error occured, I retrieve the error string with lua_tostring(L,-1). The strange thing is that sometimes the string indicates the line number where the error occured, sometimes it doesn't. How come? For instance, following Lua code: a causes following error string: attempt to call a string value But following Lua code: a=a+1 causes following error string: [string "NameOfScript"]:1:attempt to perform arithmetic on global 'a' (a nil value) The second error string contains information about where the error occured. How can I always have that information? (or am I doing something wrong?)
Advertisement
You can do that by using the last argument to the lua_pcall-function. That is the index to a Lua-function that will format the error-message to your needs. Most convenient is to use debug.traceback().

So, this would become something like:
lua_getglobal(L, "debug");        // push debug-table on the stacklua_getfield(L, "traceback");     // push debug.tracebacklua_getglobal(L, "yourFunction"); // push your functionlua_pushstring(L, "an argument"); // push argument to your functionif( lua_pcall(L, -2, 1, 0, -3) )  // call function at -2, with -3 as errfunc{   fprintf( L, "Lua error: %s\n", lua_tostring(L,-1) );}lua_pop(L,2);
are you using decoda at all? it binds itself to the vis debugging symbols and runs in debug mode so allows you to put breakpoints within your script file so you can track whatever this global a is doing in more detail maybe
Thanks for your reply DaBono.
Your solution however still not solves my problem. Following program:
a

now generates:

"stack traceback:
attempt to call a string value"

instead of:

"attempt to call a string value"

Am I missing something or doing something wrong?
This is how I make my call:

lua_getglobal(L,"debug");lua_getfield(L,-1,"traceback");luaL_loadbuffer(L,scriptText,scriptTextLength-1,getScriptName().c_str());inputArguments.pushObjectsOnStack(L);if (lua_pcall(L,inputArguments.getObjectNumber(),LUA_MULTRET,-inputArguments.getObjectNumber()-2)!=0)    	printf("%s \n",lua_tostring(L,-1));

Strange I don't that error when executing that sample script.

a

I get this error instead.

[string "Lua"](1): '=' expected near '<eof>'

Are you sure u haven't bound the variable a to some function ptr?

Good Luck!

-ddn
I know what the problem is, it got me as well. The problem is

luaL_loadbuffer(L,scriptText,scriptTextLength-1,getScriptName().c_str());


can fail, due to compilation errors. There are 2 types of errors, compilation and run time errors. When u execute the script after a compilation error there is only a string on the stack, the error string. So you get this obscure error "attempt to call a string value".

The real error of coruse is that string, which was pushed onto the stack on compilation failure. Get check if luaL_loadbuffer fails( i forget what it returns if it fails, its all in the documentation ), if so it pushes the error string on the stack and you'll need to pop it off and that will give you the line number and everything.

Good Luck!

-ddn
ddn3, Thanks a lot!!
That was a very stupid error but somehow never thought about that. Working like a charm now.
Thanks a lot for your help :)

This topic is closed to new replies.

Advertisement