lua-users home
lua-l archive

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


On Sat, Feb 18, 2023 at 05:48:41PM -0800, Nathan Page wrote:
> Hi All,
> 
> I've made a patch to lua that I thought may come in handy for embedded
> use cases. It essentially adds a lua_State argument to
> 
> - lua_writestring
> - lua_writestringerror
> - lua_writeline
> 
> and subsequently modifies all occurrences of the api in the source
> code to add the state. For a standard lua installation this doesn't
> change anything. Now the writestring methods could access lua state to
> make more flexible output handling.

For Lua 5.4 none of this should be necessary if you use lua_atpanic and
lua_setwarnf. Both already take a context argument--a lua_State for the
panic handler, and an opaque context for the warning handler.

lua_writestringerror is used only used by 1) the default panic and warning
handlers, 2) the iteractive debugger, and 3) some command-line interpreter
option argument handlers. #3 can't be reached in an embedded scenario. For
#2 even if you wanted an interactive debugger in an embedded context, the
entire routine, db_debug in ldblib.c, is only 13 lines of C code, and it's
only accessible as the global debug.debug in Lua script; it can be trivially
replaced with your own implementation.

lua_writestring is only used by 1) the global function 'print' and 2) the
interpreter for the -V option flag. In both cases printing to stdout is the
intended behavior.

Importantly, in a context where a process has multiple Lua VM contexts you
almost certainly want to install your own panic handler regardless of where
messages are printed, because if the panic handler returns then Lua calls
abort(). (See luaD_throw in ldo.c.)