Jump to content
  • Advertisement
Sign in to follow this  
floatingwoods

Lua and error line number

This topic is 3697 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?)

Share this post


Link to post
Share on other sites
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 stack
lua_getfield(L, "traceback"); // push debug.traceback
lua_getglobal(L, "yourFunction"); // push your function
lua_pushstring(L, "an argument"); // push argument to your function
if( 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);

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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));


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!