Sign in to follow this  

Get line # from within C function called from Lua

This topic is 3301 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

In my C functions that are callable from a Lua script, I'd like to include provisions for warnings that can be output to my game's console. I'd like to include as much as possible in such warnings, including the script line # and Lua module name where the call was made. I can't seem to find a way to do this. For example:
int fn(lua_State* L)
{
   if (lua_isnumber(L, 1))
   {
      foo(lua_tonumber(L, 1));
   }
   else
   {
      /* Warn that something other than a number was passed. */
   }
}

Anyone know how to get the line number and module name at the point where Lua made the call to this C function? Thanks in advance for any help.

Share this post


Link to post
Share on other sites
Use the debug.traceback function. Alternatively, just throw an error at that point with lua_error... if you can't return something reasonable that's likely the best approach.

Share this post


Link to post
Share on other sites
The documentation for lua_error says that calls to it don't return, which doesn't sound too friendly. Am I missing something? I don't want to hose up the works, just display a warning message.

Share this post


Link to post
Share on other sites
Quote:
Original post by BS-er
The documentation for lua_error says that calls to it don't return, which doesn't sound too friendly.
lua_error doesn't terminate the program. It uses longjmp for a non-local return.

Share this post


Link to post
Share on other sites
Thanks for your help and responsiveness. It looks like the lua_error approach is a dead end as well. It's not the thing to use for non-intrusive warnings. It ends the coroutine thread unfortunately. The debug.trace doesn't look promising either, as a way to get information from within the C function that Lua is calling. Sure you can make a call to it in C, but all the setup required looks like a grand advanture that may or may not be for naught. It looks INTENDED for handling a call to a Lua function that generated an error, and like most of my other attempts, I think it would yield little if used outside of its intentions.

Dang no elegant solution available by a longshot :(. I was hoping for a neat little function call.

Share this post


Link to post
Share on other sites
Well I didn't get quite what I want, but I guess I got what I need. setting up a call to debug.traceback from within the C function does give me the line number and module, along with a lot of extra stuff. It'll do :).

Share this post


Link to post
Share on other sites
If you want a neat little function call, just make one.


int fn(lua_State* L)
{
if (lua_isnumber(L, 1))
{
foo(lua_tonumber(L, 1));
}
else
{
lua_getglobal(L, "warn");
lua_call(L, 0, 0);
}
}



function warn()
tb = debug.traceback()
-- grand adventure here
end

Share this post


Link to post
Share on other sites

This topic is 3301 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this