commit a650378822f48e81978e626329bbd96373035eb2
parent 99182c6872f173e8e91426a0dba1468769818681
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Mon, 21 Sep 2009 09:09:28 -0300
'cpcall' reimplemented as a predefined value in the registry
Diffstat:
M | lapi.c | | | 44 | +++++++++----------------------------------- |
M | lstate.c | | | 33 | ++++++++++++++++++++++++++++++--- |
M | lua.h | | | 5 | +++-- |
3 files changed, 42 insertions(+), 40 deletions(-)
diff --git a/lapi.c b/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);
+}
+
diff --git a/lstate.c b/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) {
}
+/*
+** 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);
+}
+
+
+/*
+** Create registry table and its predefined values
+*/
static void init_registry (lua_State *L) {
- Table *registry = luaH_new(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 that may cause memory-allocation errors
+** 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;
diff --git a/lua.h b/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