diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-01-21 14:49:21 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-01-21 14:49:21 -0200 |
| commit | 4f5f2fe36732d7809a4c0e0856cb9aca02e9ce24 (patch) | |
| tree | cf1a2ef6793c58c4424248b2aff88e457a46a000 | |
| parent | 01586d539e214f844fc34dd552ff06843ac0c4f9 (diff) | |
| download | lua-4f5f2fe36732d7809a4c0e0856cb9aca02e9ce24.tar.gz lua-4f5f2fe36732d7809a4c0e0856cb9aca02e9ce24.tar.bz2 lua-4f5f2fe36732d7809a4c0e0856cb9aca02e9ce24.zip | |
new function 'luaL_cpcall'
| -rw-r--r-- | lapi.c | 11 | ||||
| -rw-r--r-- | lauxlib.c | 13 | ||||
| -rw-r--r-- | lauxlib.h | 4 | ||||
| -rw-r--r-- | lua.c | 11 | ||||
| -rw-r--r-- | luaconf.h | 9 |
5 files changed, 25 insertions, 23 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 2.110 2010/01/11 17:38:30 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.111 2010/01/13 16:18:25 roberto Exp roberto $ |
| 3 | ** Lua API | 3 | ** Lua API |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -1150,12 +1150,3 @@ LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, | |||
| 1150 | luaC_objbarrier(L, f1, *up2); | 1150 | luaC_objbarrier(L, f1, *up2); |
| 1151 | } | 1151 | } |
| 1152 | 1152 | ||
| 1153 | |||
| 1154 | #if defined(LUA_COMPAT_CPCALL) | ||
| 1155 | LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) { | ||
| 1156 | lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_CPCALL); | ||
| 1157 | lua_pushlightuserdata(L, &func); | ||
| 1158 | lua_pushlightuserdata(L, ud); | ||
| 1159 | return lua_pcall(L, 2, 0, 0); | ||
| 1160 | } | ||
| 1161 | #endif | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.195 2009/12/17 16:20:01 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.196 2009/12/22 15:32:50 roberto Exp roberto $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -773,3 +773,14 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver) { | |||
| 773 | ver, *v); | 773 | ver, *v); |
| 774 | } | 774 | } |
| 775 | 775 | ||
| 776 | |||
| 777 | LUALIB_API int luaL_cpcall (lua_State *L, lua_CFunction f, int nargs, | ||
| 778 | int nresults) { | ||
| 779 | nargs++; /* to include function itself */ | ||
| 780 | lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_CPCALL); | ||
| 781 | lua_insert(L, -nargs); | ||
| 782 | lua_pushlightuserdata(L, &f); | ||
| 783 | lua_insert(L, -nargs); | ||
| 784 | return lua_pcall(L, nargs, nresults, 0); | ||
| 785 | } | ||
| 786 | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.h,v 1.98 2010/01/06 15:14:15 roberto Exp roberto $ | 2 | ** $Id: lauxlib.h,v 1.99 2010/01/11 16:00:45 roberto Exp roberto $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -82,6 +82,8 @@ LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, | |||
| 82 | LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, | 82 | LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, |
| 83 | const char *msg, int level); | 83 | const char *msg, int level); |
| 84 | 84 | ||
| 85 | LUALIB_API int luaL_cpcall (lua_State *L, lua_CFunction f, int nargs, | ||
| 86 | int nresults); | ||
| 85 | 87 | ||
| 86 | 88 | ||
| 87 | /* | 89 | /* |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.182 2009/12/22 16:47:12 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.183 2010/01/21 16:31:06 roberto Exp roberto $ |
| 3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -445,7 +445,6 @@ static int pmain (lua_State *L) { | |||
| 445 | 445 | ||
| 446 | 446 | ||
| 447 | int main (int argc, char **argv) { | 447 | int main (int argc, char **argv) { |
| 448 | static lua_CFunction ppmain = &pmain; | ||
| 449 | int status, result; | 448 | int status, result; |
| 450 | lua_State *L = luaL_newstate(); /* create state */ | 449 | lua_State *L = luaL_newstate(); /* create state */ |
| 451 | if (L == NULL) { | 450 | if (L == NULL) { |
| @@ -453,11 +452,9 @@ int main (int argc, char **argv) { | |||
| 453 | return EXIT_FAILURE; | 452 | return EXIT_FAILURE; |
| 454 | } | 453 | } |
| 455 | /* call 'pmain' in protected mode */ | 454 | /* call 'pmain' in protected mode */ |
| 456 | lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_CPCALL); /* calling function */ | 455 | lua_pushinteger(L, argc); /* 1st argument */ |
| 457 | lua_pushlightuserdata(L, &ppmain); | 456 | lua_pushlightuserdata(L, argv); /* 2nd argument */ |
| 458 | lua_pushinteger(L, argc); | 457 | status = luaL_cpcall(L, &pmain, 2, 1); |
| 459 | lua_pushlightuserdata(L, argv); | ||
| 460 | status = lua_pcall(L, 3, 1, 0); | ||
| 461 | result = lua_toboolean(L, -1); /* get result */ | 458 | result = lua_toboolean(L, -1); /* get result */ |
| 462 | finalreport(L, status); | 459 | finalreport(L, status); |
| 463 | lua_close(L); | 460 | lua_close(L); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: luaconf.h,v 1.130 2010/01/11 17:15:30 roberto Exp roberto $ | 2 | ** $Id: luaconf.h,v 1.131 2010/01/21 16:31:24 roberto Exp roberto $ |
| 3 | ** Configuration file for Lua | 3 | ** Configuration file for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -220,11 +220,11 @@ | |||
| 220 | #define LUA_COMPAT_UNPACK | 220 | #define LUA_COMPAT_UNPACK |
| 221 | 221 | ||
| 222 | /* | 222 | /* |
| 223 | @@ LUA_COMPAT_CPCALL controls the presence of function 'lua_cpcall'. | 223 | @@ LUA_COMPAT_CPCALL controls the presence of macro 'lua_cpcall'. |
| 224 | ** You can replace it with the preregistered function 'cpcall'. | 224 | ** You can replace it with the preregistered function 'cpcall'. |
| 225 | */ | 225 | */ |
| 226 | #define LUA_COMPAT_CPCALL | 226 | #define lua_cpcall(L,f,u) \ |
| 227 | LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud); | 227 | (lua_pushlightuserdata(L,(u)), luaL_cpcall(L,(f),1,0)) |
| 228 | 228 | ||
| 229 | /* | 229 | /* |
| 230 | @@ LUA_COMPAT_FENV controls the presence of functions 'setfenv/getfenv'. | 230 | @@ 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); | |||
| 233 | */ | 233 | */ |
| 234 | #define LUA_COMPAT_FENV | 234 | #define LUA_COMPAT_FENV |
| 235 | 235 | ||
| 236 | |||
| 236 | /* | 237 | /* |
| 237 | @@ LUA_COMPAT_LOG10 defines the function 'log10' in the math library. | 238 | @@ LUA_COMPAT_LOG10 defines the function 'log10' in the math library. |
| 238 | ** You can rewrite 'log10(x)' as 'log(x, 10)'. | 239 | ** You can rewrite 'log10(x)' as 'log(x, 10)'. |
