lua-users home
lua-l archive

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


Error messages are returned on the stack.  Here's the code I use with beta 5
for my console, which compiles and executes a string.  LoadS and getS are
taken from code supplied with the lua distribution.  You can replace with
your own code if reading from a file.  Assumes that you've already created a
lua_State with lua_open

// scripting
lua_State * g_luastate = 0;

// taken from lua distribution
typedef struct LoadS
{
    const char *s;
    size_t size;
} LoadS;

// taken from lua distribution
static const char *getS (lua_State *L, void *ud, size_t *size)
{
    LoadS *ls = (LoadS *)ud;
    (void)L;
    if (ls->size == 0) return NULL;
    *size = ls->size;
    ls->size = 0;
    return ls->s;
}

// called whenever a line is input into the console
virtual void EventInput( const char * input )
{
    LoadS ls;
    ls.s = input;
    ls.size = strlen(input);

    int status;

    // compile string
    status = lua_load( g_luastate, getS, &ls, "console" );

    switch( status )
    {
    case 0: // no errors
        break;

    case LUA_ERRSYNTAX:
    case LUA_ERRMEM:
    default:
        console->stream() << "compile: " << lua_tostring( g_luastate, 1 );
// print error message
        lua_settop( g_luastate, 0 );
        return;
    }

    // execute bytecode
    status = lua_pcall( g_luastate, 0, LUA_MULTRET, 0 );

    switch( status )
    {
    case 0: // no errors;
        break;

    case LUA_ERRRUN:
    case LUA_ERRMEM:
    case LUA_ERRERR:
    default:
        console->stream() << "run: " << lua_tostring( g_luastate, 1 );    //
print error message
        lua_settop( g_luastate, 0 );
        return;
    }
}

----- Original Message -----
From: "Nick Davies" <goodbytes@binarysurge.com>
To: "Multiple recipients of list" <lua-l@tecgraf.puc-rio.br>
Sent: Monday, March 03, 2003 6:44 PM
Subject: Re: Changelog for 5


> > Quick list of changes from 4.0  to 5.0:
> >
> > [...]
> > - better error messages
>
> Ah, yes, the elusive error messages.
>
> Quite often, my program will simply quit after a call to lua_call()
without
> giving any sort of error message, and it's usually(i.e. always) due to
some
> mistake I made in lua files(such as trying to access an entry in a table
> that doesn't exist). I assume that I'm doing things wrong here - how do I
> get error messages from Lua to appear so that I can see what's going on
> instead of relying on trial and error?
>
> Thanks in advance,
> ~Nick
>
>