[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Handling errors of modules
 
- From: "Nodir Temirhodzhaev"  <tnodir@...>
 
- Date: Tue, 18 Nov 2003 11:23:10 +0300
 
> >> Use the registry. It's there for these kind of uses.
> >
> > Is it threadsafe?
> 
> If you mean Lua threads, then yes.
I done small library:
========================
#include <lua.h>
#include <lauxlib.h>
#define ERRID_KEY	"errid"
void err_set (lua_State *L, int idx)
{
    lua_pushliteral (L, ERRID_KEY);
    lua_pushvalue (L, idx);
    lua_rawset (L, LUA_REGISTRYINDEX);
}
void err_setno (lua_State *L, int num)
{
    lua_pushliteral (L, ERRID_KEY);
    lua_pushnumber (L, num);
    lua_rawset (L, LUA_REGISTRYINDEX);
}
void err_get (lua_State *L)
{
    lua_pushliteral (L, ERRID_KEY);
    lua_rawget (L, LUA_REGISTRYINDEX);
}
static int err_id (lua_State *L)
{
    if (lua_gettop (L)) err_set (L, 1);
    else err_get (L);
    return 1;
}
LUALIB_API int luaopen_err (lua_State *L)
{
    lua_pushliteral (L, "errid");
    lua_pushcfunction (L, err_id);
    lua_settable (L, LUA_GLOBALSINDEX);
    return 0;
}
========================
And test:
========================
local function foo(a)
    errid(a)
    print("set", a)
    coroutine.yield()
    print(errid())
    return
end
local co1 = coroutine.create(foo)
local co2 = coroutine.create(foo)
coroutine.resume(co1, 11)
coroutine.resume(co2, 22)
coroutine.resume(co1)
coroutine.resume(co2)
========================
Output:
set 11
set 22
22
22
What is wrong? Thanks.