new function 'luaL_cpcall'

This commit is contained in:
Roberto Ierusalimschy 2010-01-21 14:49:21 -02:00
parent 01586d539e
commit 4f5f2fe367
5 changed files with 25 additions and 23 deletions

11
lapi.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 2.110 2010/01/11 17:38:30 roberto Exp roberto $
** $Id: lapi.c,v 2.111 2010/01/13 16:18:25 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@ -1150,12 +1150,3 @@ LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
luaC_objbarrier(L, f1, *up2);
}
#if defined(LUA_COMPAT_CPCALL)
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);
}
#endif

View File

@ -1,5 +1,5 @@
/*
** $Id: lauxlib.c,v 1.195 2009/12/17 16:20:01 roberto Exp roberto $
** $Id: lauxlib.c,v 1.196 2009/12/22 15:32:50 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@ -773,3 +773,14 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver) {
ver, *v);
}
LUALIB_API int luaL_cpcall (lua_State *L, lua_CFunction f, int nargs,
int nresults) {
nargs++; /* to include function itself */
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_CPCALL);
lua_insert(L, -nargs);
lua_pushlightuserdata(L, &f);
lua_insert(L, -nargs);
return lua_pcall(L, nargs, nresults, 0);
}

View File

@ -1,5 +1,5 @@
/*
** $Id: lauxlib.h,v 1.98 2010/01/06 15:14:15 roberto Exp roberto $
** $Id: lauxlib.h,v 1.99 2010/01/11 16:00:45 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@ -82,6 +82,8 @@ LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx,
LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
const char *msg, int level);
LUALIB_API int luaL_cpcall (lua_State *L, lua_CFunction f, int nargs,
int nresults);
/*

11
lua.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lua.c,v 1.182 2009/12/22 16:47:12 roberto Exp roberto $
** $Id: lua.c,v 1.183 2010/01/21 16:31:06 roberto Exp roberto $
** Lua stand-alone interpreter
** See Copyright Notice in lua.h
*/
@ -445,7 +445,6 @@ static int pmain (lua_State *L) {
int main (int argc, char **argv) {
static lua_CFunction ppmain = &pmain;
int status, result;
lua_State *L = luaL_newstate(); /* create state */
if (L == NULL) {
@ -453,11 +452,9 @@ int main (int argc, char **argv) {
return EXIT_FAILURE;
}
/* call 'pmain' in protected mode */
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_CPCALL); /* calling function */
lua_pushlightuserdata(L, &ppmain);
lua_pushinteger(L, argc);
lua_pushlightuserdata(L, argv);
status = lua_pcall(L, 3, 1, 0);
lua_pushinteger(L, argc); /* 1st argument */
lua_pushlightuserdata(L, argv); /* 2nd argument */
status = luaL_cpcall(L, &pmain, 2, 1);
result = lua_toboolean(L, -1); /* get result */
finalreport(L, status);
lua_close(L);

View File

@ -1,5 +1,5 @@
/*
** $Id: luaconf.h,v 1.130 2010/01/11 17:15:30 roberto Exp roberto $
** $Id: luaconf.h,v 1.131 2010/01/21 16:31:24 roberto Exp roberto $
** Configuration file for Lua
** See Copyright Notice in lua.h
*/
@ -220,11 +220,11 @@
#define LUA_COMPAT_UNPACK
/*
@@ LUA_COMPAT_CPCALL controls the presence of function 'lua_cpcall'.
@@ LUA_COMPAT_CPCALL controls the presence of macro 'lua_cpcall'.
** You can replace it with the preregistered function 'cpcall'.
*/
#define LUA_COMPAT_CPCALL
LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud);
#define lua_cpcall(L,f,u) \
(lua_pushlightuserdata(L,(u)), luaL_cpcall(L,(f),1,0))
/*
@@ LUA_COMPAT_FENV controls the presence of functions 'setfenv/getfenv'.
@ -233,6 +233,7 @@ LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud);
*/
#define LUA_COMPAT_FENV
/*
@@ LUA_COMPAT_LOG10 defines the function 'log10' in the math library.
** You can rewrite 'log10(x)' as 'log(x, 10)'.