lua-users home
lua-l archive

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


Am 07.06.2019 um 22:04 schrieb Andrew Gierth:
>>>>>> "David" == David Heiko Kolf <david@dkolf.de> 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