lua-users home
lua-l archive

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


I've been using it for a while with no issues, but I also do not use
any coroutines. I think when this is done the user has to be very
careful with what they put in lua_writestring and lua_writestringerror
to ensure it doesnt accidentally get called recursively.

I store a c callback function and userdata in the registry. So I only
use the lua_State to access the registry table.

Here is some pseudo code for what my codebase implements:

typedef enum
{
    LUA_STDOUT = 1,
    LUA_STDERR = 2,
} lua_Stream;

typedef int (*lua_Printer)(void *ud, lua_Steam stream, const char *format, ...);

#define LUA_PRINTER "_LUA_PRINTER"
#define LUA_PRINTER_UD "_LUA_PRINTER_UD"

int lua_writestring(lua_State *L, const char *mesage, size_t n)
{
    // not ideal to have NULL check, may be reworked
    if (L == NULL)
    {
        return fwrite((s), sizeof(char), (l), stdout);
    }
    lua_pushliteral(L, LUA_PRINTER);
    lua_gettable(L, LUA_REGISTRYINDEX);
    lua_pushliteral(L, LUA_PRINTER_UD);
    lua_gettable(L, LUA_REGISTRYINDEX);
    // some typechecking
    lua_Printer *lprinter = (lua_Printer *)lua_touserdata(L, -2);
    void *ud = (void *)lua_touserdata(L, -1);
    lua_pop(L, 2);
    return lprinter(ud, LUA_STDOUT, "%s", message);
}

int lua_writestringerror(lua_State *L, const char *s, const char *p)
{
    // not ideal to have NULL check, may be reworked
    if (L == NULL)
    {
        fprintf(stderr, (s), (p));
        return fflush(stderr);
    }
    lua_pushliteral(L, LUA_PRINTER);
    lua_gettable(L, LUA_REGISTRYINDEX);
    lua_pushliteral(L, LUA_PRINTER_UD);
    lua_gettable(L, LUA_REGISTRYINDEX);
    // some typechecking
    lua_Printer *lprinter = (lua_Printer *)lua_touserdata(L, -2);
    void *ud = (void *)lua_touserdata(L, -1);
    lua_pop(L, 2);
    return lprinter(ud, LUA_STDERR, s, p);
}

#define lua_writeline(L) lua_writestring(L, "\n", 1);

On Sat, Feb 18, 2023 at 6:11 PM bil til <biltil52@gmail.com> wrote:
>
> ... if this REALLY works 100%, I agree that this would help quite a bit.
>
> In my application I also had to fight a bit with the
> lua_writestringerror message and its possible invocation by panic and
> warnings... .
>
> (In fact I handled warnings my own way and if panic occurs, I will
> invoke a private lua_writestringerror which effectively puts the
> controller CPU into some "sure safe state", running a sort of "bios
> LUA").
>
> (I did not check completely, but I am quite sure that I also was
> nerved mainly that L was not known in this lua_writestringerror
> message, and that possibly I could find a more elegant way to handle
> this, if L would be known in lua_writestringerror... ... my problem
> is, that lua_writestringerror should run into a print function which
> is not very easy, as it needs thread wait times / thread yielding, and
> for this I need a defined Lua state L - if I get this, with
> lua_writestringerror, then I should be happy... of course in case of
> memory error (L=NULL) I still would need a special solution, with such
> a "Lua bios" or similar... but all other invocations of
> lua_writestringerror then would become much more easy ).