5.4.0-alpha: accessing the Lua state in lua_WarnFunction

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

5.4.0-alpha: accessing the Lua state in lua_WarnFunction

David Heiko Kolf-2
Hello,

in the manual to Lua 5.4.0-alpha-rc2 I noticed that the lua_WarnFunction
that is passed to lua_setwarnf does not get the Lua state as a
parameter. Obviously I could still pass the Lua state myself though the
"ud" parameter.

Is there a reason why the Lua state isn't passed by default? Is it safe
to access the Lua state in the warning function?

I am asking because I have an application where the Lua interpreter can
be used with different user interfaces which are loaded as modules.
Direct text output should not happen, I'd prefer to call a Lua function
for warnings or save the warnings in a Lua table.

Best regards,

David

Reply | Threaded
Open this post in threaded view
|

Re: 5.4.0-alpha: accessing the Lua state in lua_WarnFunction

Andrew Gierth
>>>>> "David" == David Heiko Kolf <[hidden email]> writes:

 David> Hello,

 David> in the manual to Lua 5.4.0-alpha-rc2 I noticed that the
 David> lua_WarnFunction that is passed to lua_setwarnf does not get the
 David> Lua state as a parameter. Obviously I could still pass the Lua
 David> state myself though the "ud" parameter.

 David> Is there a reason why the Lua state isn't passed by default? Is
 David> it safe to access the Lua state in the warning function?

The WarnFunction is called from somewhere inside the garbage collector
with the state locked, which I'm pretty sure implies that it's not legal
to call any API function from inside it. So the only thing you could
possibly access safely would be any extraspace value you stored with the
state, and in that case you might just as easily pass that, or its
address, as your "ud" parameter.

--
Andrew.

Reply | Threaded
Open this post in threaded view
|

Re: 5.4.0-alpha: accessing the Lua state in lua_WarnFunction

David Heiko Kolf-2
Am 07.06.2019 um 22:04 schrieb Andrew Gierth:

>>>>>> "David" == David Heiko Kolf <[hidden email]> writes:
>
>  David> Hello,
>
>  David> in the manual to Lua 5.4.0-alpha-rc2 I noticed that the
>  David> lua_WarnFunction that is passed to lua_setwarnf does not get the
>  David> Lua state as a parameter. Obviously I could still pass the Lua
>  David> state myself though the "ud" parameter.
>
>  David> Is there a reason why the Lua state isn't passed by default? Is
>  David> it safe to access the Lua state in the warning function?
>
> The WarnFunction is called from somewhere inside the garbage collector
> with the state locked, which I'm pretty sure implies that it's not legal
> to call any API function from inside it. So the only thing you could
> possibly access safely would be any extraspace value you stored with the
> state, and in that case you might just as easily pass that, or its
> address, as your "ud" parameter.

Well, it is called after the GC called the __gc finalizer metamethod
(lgc.c, function GCTM). So it was calling Lua functions at roughly this
place already. But yes, at that moment the state is locked.

Anyway, it would probably be good if it is documented what the
lua_WarnFunction is and isn't allowed to do.

The default function (warnf in lauxlib.c) is accessing the memory of a
userdata directly, I guess I will have to do something similar even
though I would have preferred to be able to store it directly in a Lua
table to avoid having to deal with an intermediate memory storage.

Best regards,

David

Reply | Threaded
Open this post in threaded view
|

Re: 5.4.0-alpha: accessing the Lua state in lua_WarnFunction

David Heiko Kolf-2
In reply to this post by David Heiko Kolf-2
Hello,

after considering the lua_WarnFunction further points came up for me:


- It is not possible to change the behavior from within Lua. If I was
running a Lua program using the standard interpreter and I don't want
random outputs from warnings to stderr, I would have to load a C-module.


- Writing a custom lua_WarnFunction I have to be extremely careful that
the output function wouldn't trigger events that call Lua code again, as
the state is still locked.

For example, I might call the MessageBox function in Windows which opens
a modal dialog. While the dialog is open, if I am not mistaken, events
like drawing requests and timers would still get dispatched to other
windows of the application, which might call Lua code again. Yes, there
are solutions to avoid that, but it quickly gets complicated.


- It is not clear to me when a script could be expected to call the
"warn" function.

In the Lua core the warn function is something very serious only called
when an error happens that cannot be dealt with. Almost like a call to
the panic function. But when would a normal Lua script get into the
situation that it couldn't call "error"?

I am afraid that the warn function, based on its name, might get used in
libraries as an "almost-error" more for debugging purposes even though
it is much more difficult to handle properly in many applications.


Maybe it would be possible to provide a normal Lua callback for GC
errors? After all, the Lua __gc metamethod function was called just
prior to it.

Best regards,

David