Discussion:
lua_pcall error handler
p***@flamewars.org
2003-01-08 01:49:28 UTC
Permalink
Can anyone point me to an example of how to get a stack traceback using an
error handler and lua_pcall?

Thanks, Philip Bock
Luiz Henrique de Figueiredo
2003-01-09 00:24:08 UTC
Permalink
Post by p***@flamewars.org
Can anyone point me to an example of how to get a stack traceback using an
error handler and lua_pcall?
The standard traceback you get in the standalone interpreter when an
error happens is output using this scheme (of course!). See lcall in
lua.c in Lua 5.0 (beta). It runs code by setting the value of the
global variable _TRACEBACK as an error handler. The debug library sets
_TRACEBACK (see errorfb in ldblib.c).
--lhf
p***@flamewars.org
2003-01-09 04:36:03 UTC
Permalink
Thanks! I've followed the example in lua.c, and it seems to work. I have just
two questions. Firstly, what is lua_pushliteral? I've done this using
lua_getglobal. Any reason not to? And secondly, to do this I've had to use the
dblib, but I'm not sure I want to give my users access to it. Is there any way
to prevent them from using it? Would something like just setting the global
'debug' to nil work?

Thanks, Philip Bock
Post by Luiz Henrique de Figueiredo
Post by p***@flamewars.org
Can anyone point me to an example of how to get a stack traceback using an
error handler and lua_pcall?
The standard traceback you get in the standalone interpreter when an
error happens is output using this scheme (of course!). See lcall in
lua.c in Lua 5.0 (beta). It runs code by setting the value of the
global variable _TRACEBACK as an error handler. The debug library sets
_TRACEBACK (see errorfb in ldblib.c).
--lhf
--
Luiz Henrique de Figueiredo
2003-01-09 09:25:07 UTC
Permalink
Post by p***@flamewars.org
Thanks! I've followed the example in lua.c, and it seems to work. I have just
two questions. Firstly, what is lua_pushliteral? I've done this using
lua_getglobal. Any reason not to?
lua.c takes special care with errors and so at that point uses lua_rawget
instead of lua_gettable, which is what lua_getglobal does. lua_pushliteral is
a simple macro that avoids the call to strlen in lua_pushstring when you use
literal strings. You can use lua_getglobal.
Post by p***@flamewars.org
And secondly, to do this I've had to use the
dblib, but I'm not sure I want to give my users access to it. Is there any way
to prevent them from using it? Would something like just setting the global
'debug' to nil work?
Yes, you can set 'debug' to nil after loading dblib. But perhaps you can just
copy errorfb from ldblib.c into your code and not load dblib at all.
--lhf

Loading...