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 /lstate.c | |
parent | 99182c6872f173e8e91426a0dba1468769818681 (diff) | |
download | lua-a650378822f48e81978e626329bbd96373035eb2.tar.gz lua-a650378822f48e81978e626329bbd96373035eb2.tar.bz2 lua-a650378822f48e81978e626329bbd96373035eb2.zip |
'cpcall' reimplemented as a predefined value in the registry
Diffstat (limited to 'lstate.c')
-rw-r--r-- | lstate.c | 33 |
1 files changed, 30 insertions, 3 deletions
@@ -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; |