2004-06-16 03:02:07 +02:00
|
|
|
/*=========================================================================*\
|
|
|
|
* Simple exception support
|
|
|
|
* LuaSocket toolkit
|
|
|
|
*
|
|
|
|
* RCS ID: $Id$
|
|
|
|
\*=========================================================================*/
|
2004-06-15 08:24:00 +02:00
|
|
|
#include <stdio.h>
|
2004-07-01 07:35:35 +02:00
|
|
|
#include <lua.h>
|
|
|
|
#include <lauxlib.h>
|
2004-06-15 08:24:00 +02:00
|
|
|
|
|
|
|
#include "except.h"
|
|
|
|
|
2004-06-16 03:02:07 +02:00
|
|
|
/*=========================================================================*\
|
|
|
|
* Internal function prototypes.
|
|
|
|
\*=========================================================================*/
|
2004-06-15 08:24:00 +02:00
|
|
|
static int global_protect(lua_State *L);
|
2004-06-18 10:02:09 +02:00
|
|
|
static int global_newtry(lua_State *L);
|
2004-07-01 07:35:35 +02:00
|
|
|
static int protected_(lua_State *L);
|
2004-06-18 10:02:09 +02:00
|
|
|
static int finalize(lua_State *L);
|
2004-06-18 23:41:44 +02:00
|
|
|
static int do_nothing(lua_State *L);
|
2004-06-15 08:24:00 +02:00
|
|
|
|
2004-06-16 03:02:07 +02:00
|
|
|
/* except functions */
|
2004-06-15 08:24:00 +02:00
|
|
|
static luaL_reg func[] = {
|
2004-06-18 10:02:09 +02:00
|
|
|
{"newtry", global_newtry},
|
|
|
|
{"protect", global_protect},
|
|
|
|
{NULL, NULL}
|
2004-06-15 08:24:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
2004-06-18 23:41:44 +02:00
|
|
|
* Try factory
|
2004-06-18 10:02:09 +02:00
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int finalize(lua_State *L) {
|
2004-06-21 00:19:54 +02:00
|
|
|
if (!lua_toboolean(L, 1)) {
|
2004-06-18 10:02:09 +02:00
|
|
|
lua_pushvalue(L, lua_upvalueindex(1));
|
|
|
|
lua_pcall(L, 0, 0, 0);
|
|
|
|
lua_settop(L, 2);
|
|
|
|
lua_error(L);
|
|
|
|
return 0;
|
|
|
|
} else return lua_gettop(L);
|
|
|
|
}
|
|
|
|
|
2004-06-18 23:41:44 +02:00
|
|
|
static int do_nothing(lua_State *L) {
|
|
|
|
(void) L;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-18 10:02:09 +02:00
|
|
|
static int global_newtry(lua_State *L) {
|
2004-06-18 23:41:44 +02:00
|
|
|
lua_settop(L, 1);
|
|
|
|
if (lua_isnil(L, 1)) lua_pushcfunction(L, do_nothing);
|
2004-06-18 10:02:09 +02:00
|
|
|
lua_pushcclosure(L, finalize, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Protect factory
|
2004-06-15 08:24:00 +02:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-01 07:35:35 +02:00
|
|
|
static int protected_(lua_State *L) {
|
2004-06-15 08:24:00 +02:00
|
|
|
lua_pushvalue(L, lua_upvalueindex(1));
|
|
|
|
lua_insert(L, 1);
|
|
|
|
if (lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0) != 0) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_insert(L, 1);
|
|
|
|
return 2;
|
|
|
|
} else return lua_gettop(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int global_protect(lua_State *L) {
|
2004-07-01 07:35:35 +02:00
|
|
|
lua_pushcclosure(L, protected_, 1);
|
2004-06-15 08:24:00 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Init module
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
int except_open(lua_State *L) {
|
|
|
|
luaL_openlib(L, NULL, func, 0);
|
|
|
|
return 0;
|
|
|
|
}
|