diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-09-21 09:09:52 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-09-21 09:09:52 -0300 |
| commit | a650378822f48e81978e626329bbd96373035eb2 (patch) | |
| tree | 67a2595d55e90b0c13db752f70d19b3731e90566 | |
| parent | 99182c6872f173e8e91426a0dba1468769818681 (diff) | |
| download | lua-a650378822f48e81978e626329bbd96373035eb2.tar.gz lua-a650378822f48e81978e626329bbd96373035eb2.tar.bz2 lua-a650378822f48e81978e626329bbd96373035eb2.zip | |
'cpcall' reimplemented as a predefined value in the registry
| -rw-r--r-- | lapi.c | 44 | ||||
| -rw-r--r-- | lstate.c | 33 | ||||
| -rw-r--r-- | lua.h | 5 |
3 files changed, 42 insertions, 40 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 2.89 2009/08/31 14:26:28 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.90 2009/09/17 18:04:21 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 | */ |
| @@ -854,40 +854,6 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, | |||
| 854 | } | 854 | } |
| 855 | 855 | ||
| 856 | 856 | ||
| 857 | /* | ||
| 858 | ** Execute a protected C call. | ||
| 859 | */ | ||
| 860 | struct CCallS { /* data to `f_Ccall' */ | ||
| 861 | lua_CFunction func; | ||
| 862 | void *ud; | ||
| 863 | }; | ||
| 864 | |||
| 865 | |||
| 866 | static void f_Ccall (lua_State *L, void *ud) { | ||
| 867 | struct CCallS *c = cast(struct CCallS *, ud); | ||
| 868 | Closure *cl; | ||
| 869 | cl = luaF_newCclosure(L, 0, getcurrenv(L)); | ||
| 870 | cl->c.f = c->func; | ||
| 871 | setclvalue(L, L->top, cl); /* push function */ | ||
| 872 | api_incr_top(L); | ||
| 873 | setpvalue(L->top, c->ud); /* push only argument */ | ||
| 874 | api_incr_top(L); | ||
| 875 | luaD_call(L, L->top - 2, 0, 0); | ||
| 876 | } | ||
| 877 | |||
| 878 | |||
| 879 | LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) { | ||
| 880 | struct CCallS c; | ||
| 881 | int status; | ||
| 882 | lua_lock(L); | ||
| 883 | c.func = func; | ||
| 884 | c.ud = ud; | ||
| 885 | status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0); | ||
| 886 | lua_unlock(L); | ||
| 887 | return status; | ||
| 888 | } | ||
| 889 | |||
| 890 | |||
| 891 | LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, | 857 | LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, |
| 892 | const char *chunkname) { | 858 | const char *chunkname) { |
| 893 | ZIO z; | 859 | ZIO z; |
| @@ -1113,3 +1079,11 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { | |||
| 1113 | return name; | 1079 | return name; |
| 1114 | } | 1080 | } |
| 1115 | 1081 | ||
| 1082 | |||
| 1083 | LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) { | ||
| 1084 | lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_CPCALL); | ||
| 1085 | lua_pushlightuserdata(L, &func); | ||
| 1086 | lua_pushlightuserdata(L, ud); | ||
| 1087 | return lua_pcall(L, 2, 0, 0); | ||
| 1088 | } | ||
| 1089 | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstate.c,v 2.57 2009/07/15 17:26:14 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 2.58 2009/09/17 18:04:21 roberto Exp roberto $ |
| 3 | ** Global State | 3 | ** Global State |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -91,18 +91,41 @@ static void freestack (lua_State *L) { | |||
| 91 | } | 91 | } |
| 92 | 92 | ||
| 93 | 93 | ||
| 94 | /* | ||
| 95 | ** Calls the function in variable pointed to by userdata in first argument | ||
| 96 | ** (Userdata cannot point directly to the function because pointer to | ||
| 97 | ** function is not compatible with void*.) | ||
| 98 | */ | ||
| 99 | static int cpcall (lua_State *L) { | ||
| 100 | lua_CFunction f = *(lua_CFunction *)lua_touserdata(L, 1); | ||
| 101 | lua_remove(L, 1); | ||
| 102 | return f(L); | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | /* | ||
| 107 | ** Create registry table and its predefined values | ||
| 108 | */ | ||
| 94 | static void init_registry (lua_State *L) { | 109 | static void init_registry (lua_State *L) { |
| 95 | Table *registry = luaH_new(L); | 110 | Closure *cp; |
| 96 | TValue mt; | 111 | TValue mt; |
| 112 | /* create registry */ | ||
| 113 | Table *registry = luaH_new(L); | ||
| 97 | sethvalue(L, registry(L), registry); | 114 | sethvalue(L, registry(L), registry); |
| 98 | luaH_resize(L, registry, LUA_RIDX_LAST, 0); | 115 | luaH_resize(L, registry, LUA_RIDX_LAST, 0); |
| 116 | /* registry[LUA_RIDX_MAINTHREAD] = L */ | ||
| 99 | setthvalue(L, &mt, L); | 117 | setthvalue(L, &mt, L); |
| 100 | setobj2t(L, luaH_setint(L, registry, LUA_RIDX_MAINTHREAD), &mt); | 118 | setobj2t(L, luaH_setint(L, registry, LUA_RIDX_MAINTHREAD), &mt); |
| 119 | /* registry[LUA_RIDX_CPCALL] = cpcall */ | ||
| 120 | cp = luaF_newCclosure(L, 0, hvalue(gt(L))); | ||
| 121 | cp->c.f = cpcall; | ||
| 122 | setclvalue(L, &mt, cp); | ||
| 123 | setobj2t(L, luaH_setint(L, registry, LUA_RIDX_CPCALL), &mt); | ||
| 101 | } | 124 | } |
| 102 | 125 | ||
| 103 | 126 | ||
| 104 | /* | 127 | /* |
| 105 | ** open parts that may cause memory-allocation errors | 128 | ** open parts of a state that may cause memory-allocation errors |
| 106 | */ | 129 | */ |
| 107 | static void f_luaopen (lua_State *L, void *ud) { | 130 | static void f_luaopen (lua_State *L, void *ud) { |
| 108 | global_State *g = G(L); | 131 | global_State *g = G(L); |
| @@ -118,6 +141,10 @@ static void f_luaopen (lua_State *L, void *ud) { | |||
| 118 | } | 141 | } |
| 119 | 142 | ||
| 120 | 143 | ||
| 144 | /* | ||
| 145 | ** preinitialize a state with consistent values without allocating | ||
| 146 | ** any memory (to avoid errors) | ||
| 147 | */ | ||
| 121 | static void preinit_state (lua_State *L, global_State *g) { | 148 | static void preinit_state (lua_State *L, global_State *g) { |
| 122 | G(L) = g; | 149 | G(L) = g; |
| 123 | L->stack = NULL; | 150 | L->stack = NULL; |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.h,v 1.242 2009/09/14 14:30:39 roberto Exp roberto $ | 2 | ** $Id: lua.h,v 1.243 2009/09/17 18:04:21 roberto Exp roberto $ |
| 3 | ** Lua - An Extensible Extension Language | 3 | ** Lua - An Extensible Extension Language |
| 4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) | 4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) |
| 5 | ** See Copyright Notice at the end of this file | 5 | ** 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); | |||
| 91 | 91 | ||
| 92 | /* predefined values in the registry */ | 92 | /* predefined values in the registry */ |
| 93 | #define LUA_RIDX_MAINTHREAD 1 | 93 | #define LUA_RIDX_MAINTHREAD 1 |
| 94 | #define LUA_RIDX_LAST LUA_RIDX_MAINTHREAD | 94 | #define LUA_RIDX_CPCALL 2 |
| 95 | #define LUA_RIDX_LAST LUA_RIDX_CPCALL | ||
| 95 | 96 | ||
| 96 | 97 | ||
| 97 | 98 | ||
