Example #1
0
int L_HandleError(lua_State *L, int errcode, void (*ErrorPrintFunc)(const char *))
{
    if (errcode == LUA_ERRMEM)
        L_OutOfMemFunc();

    if (errcode == LUA_ERRRUN || errcode == LUA_ERRERR)
    {
        if (lua_isboolean(L, -1))
        {
            int32_t killit = lua_toboolean(L, -1);
            lua_pop(L, 1);
            return killit;
        }
        else
        {
            const char *errstr = (lua_type(L, -1)==LUA_TSTRING) ?
                lua_tostring(L, -1) : "??? (error message not a string)";

            ErrorPrintFunc(errstr);
            if (L_ErrorFunc)
                L_ErrorFunc(errstr);
            lua_pop(L, 1);
            return -1;
        }
    }

    /* unreachable */
#ifndef NDEBUG
    Bassert(0);
#endif

    return 0;
}
Example #2
0
// size < 0: length of <buf> is determined using strlen()
// size >= 0: size given, for loading of LuaJIT bytecode
int L_RunString(L_State *estate, char *buf, int dofreebuf, int size, const char *name)
{
    int32_t i;
    lua_State *L = estate->L;

    // -- lua --
    Bassert(lua_gettop(L)==1);
    // on top: a traceback function
    Bassert(lua_iscfunction(L, 1));

    if (size < 0)
        i = luaL_loadstring(L, buf);
    else
        i = luaL_loadbuffer(L, buf, size, name);
    Bassert(lua_gettop(L)==2);
    if (dofreebuf)
        Bfree(buf);

    if (i == LUA_ERRMEM)
        L_OutOfMemFunc();

    if (i == LUA_ERRSYNTAX)
    {
        OSD_Printf(OSD_ERROR "state \"%s\" syntax error: %s\n", estate->name,
                   lua_tostring(L, -1));  // get err msg
        lua_pop(L, 1);  // pop errmsg
        return 3;
    }

    // call the lua chunk!
    i = lua_pcall(L, 0, 0, 1);
    Bassert(lua_gettop(L) == 1 + (i!=0));

    if (i != 0)
        L_HandleError(L, i, &L_ErrorPrint);

    Bassert(lua_gettop(L)==1);

    return i ? 4 : 0;
}