mirror of
https://github.com/lua/lua.git
synced 2025-01-28 06:03:00 +08:00
'cpcall' reimplemented as a predefined value in the registry
This commit is contained in:
parent
99182c6872
commit
a650378822
44
lapi.c
44
lapi.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lapi.c,v 2.89 2009/08/31 14:26:28 roberto Exp roberto $
|
||||
** $Id: lapi.c,v 2.90 2009/09/17 18:04:21 roberto Exp roberto $
|
||||
** Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -854,40 +854,6 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Execute a protected C call.
|
||||
*/
|
||||
struct CCallS { /* data to `f_Ccall' */
|
||||
lua_CFunction func;
|
||||
void *ud;
|
||||
};
|
||||
|
||||
|
||||
static void f_Ccall (lua_State *L, void *ud) {
|
||||
struct CCallS *c = cast(struct CCallS *, ud);
|
||||
Closure *cl;
|
||||
cl = luaF_newCclosure(L, 0, getcurrenv(L));
|
||||
cl->c.f = c->func;
|
||||
setclvalue(L, L->top, cl); /* push function */
|
||||
api_incr_top(L);
|
||||
setpvalue(L->top, c->ud); /* push only argument */
|
||||
api_incr_top(L);
|
||||
luaD_call(L, L->top - 2, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
|
||||
struct CCallS c;
|
||||
int status;
|
||||
lua_lock(L);
|
||||
c.func = func;
|
||||
c.ud = ud;
|
||||
status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
|
||||
const char *chunkname) {
|
||||
ZIO z;
|
||||
@ -1113,3 +1079,11 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_CPCALL);
|
||||
lua_pushlightuserdata(L, &func);
|
||||
lua_pushlightuserdata(L, ud);
|
||||
return lua_pcall(L, 2, 0, 0);
|
||||
}
|
||||
|
||||
|
45
lstate.c
45
lstate.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstate.c,v 2.57 2009/07/15 17:26:14 roberto Exp roberto $
|
||||
** $Id: lstate.c,v 2.58 2009/09/17 18:04:21 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -91,18 +91,41 @@ static void freestack (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static void init_registry (lua_State *L) {
|
||||
Table *registry = luaH_new(L);
|
||||
TValue mt;
|
||||
sethvalue(L, registry(L), registry);
|
||||
luaH_resize(L, registry, LUA_RIDX_LAST, 0);
|
||||
setthvalue(L, &mt, L);
|
||||
setobj2t(L, luaH_setint(L, registry, LUA_RIDX_MAINTHREAD), &mt);
|
||||
/*
|
||||
** Calls the function in variable pointed to by userdata in first argument
|
||||
** (Userdata cannot point directly to the function because pointer to
|
||||
** function is not compatible with void*.)
|
||||
*/
|
||||
static int cpcall (lua_State *L) {
|
||||
lua_CFunction f = *(lua_CFunction *)lua_touserdata(L, 1);
|
||||
lua_remove(L, 1);
|
||||
return f(L);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** open parts that may cause memory-allocation errors
|
||||
** Create registry table and its predefined values
|
||||
*/
|
||||
static void init_registry (lua_State *L) {
|
||||
Closure *cp;
|
||||
TValue mt;
|
||||
/* create registry */
|
||||
Table *registry = luaH_new(L);
|
||||
sethvalue(L, registry(L), registry);
|
||||
luaH_resize(L, registry, LUA_RIDX_LAST, 0);
|
||||
/* registry[LUA_RIDX_MAINTHREAD] = L */
|
||||
setthvalue(L, &mt, L);
|
||||
setobj2t(L, luaH_setint(L, registry, LUA_RIDX_MAINTHREAD), &mt);
|
||||
/* registry[LUA_RIDX_CPCALL] = cpcall */
|
||||
cp = luaF_newCclosure(L, 0, hvalue(gt(L)));
|
||||
cp->c.f = cpcall;
|
||||
setclvalue(L, &mt, cp);
|
||||
setobj2t(L, luaH_setint(L, registry, LUA_RIDX_CPCALL), &mt);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** open parts of a state that may cause memory-allocation errors
|
||||
*/
|
||||
static void f_luaopen (lua_State *L, void *ud) {
|
||||
global_State *g = G(L);
|
||||
@ -118,6 +141,10 @@ static void f_luaopen (lua_State *L, void *ud) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** preinitialize a state with consistent values without allocating
|
||||
** any memory (to avoid errors)
|
||||
*/
|
||||
static void preinit_state (lua_State *L, global_State *g) {
|
||||
G(L) = g;
|
||||
L->stack = NULL;
|
||||
|
5
lua.h
5
lua.h
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lua.h,v 1.242 2009/09/14 14:30:39 roberto Exp roberto $
|
||||
** $Id: lua.h,v 1.243 2009/09/17 18:04:21 roberto Exp roberto $
|
||||
** Lua - An Extensible Extension Language
|
||||
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
|
||||
** See Copyright Notice at the end of this file
|
||||
@ -91,7 +91,8 @@ typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
|
||||
|
||||
/* predefined values in the registry */
|
||||
#define LUA_RIDX_MAINTHREAD 1
|
||||
#define LUA_RIDX_LAST LUA_RIDX_MAINTHREAD
|
||||
#define LUA_RIDX_CPCALL 2
|
||||
#define LUA_RIDX_LAST LUA_RIDX_CPCALL
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user