diff options
-rw-r--r-- | lbaselib.c | 37 |
1 files changed, 16 insertions, 21 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.155 2004/08/30 15:28:32 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.156 2004/08/30 18:35:14 roberto Exp roberto $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -273,25 +273,25 @@ static int luaB_loadfile (lua_State *L) { | |||
273 | } | 273 | } |
274 | 274 | ||
275 | 275 | ||
276 | struct Aux_load { | 276 | /* |
277 | int func; | 277 | ** Reader for generic `load' function: `lua_load' uses the |
278 | int res; | 278 | ** stack for internal stuff, so the reader cannot change the |
279 | }; | 279 | ** stack top. Instead, it keeps its resulting string in a |
280 | 280 | ** reserved slot inside the stack. | |
281 | 281 | */ | |
282 | static const char *generic_reader (lua_State *L, void *ud, size_t *size) { | 282 | static const char *generic_reader (lua_State *L, void *ud, size_t *size) { |
283 | struct Aux_load *al = (struct Aux_load *)ud; | 283 | luaL_checkstack(L, 2, "too many nested functions"); |
284 | luaL_unref(L, al->res, LUA_REGISTRYINDEX); | 284 | lua_pushvalue(L, 1); /* get function */ |
285 | lua_getref(L, al->func); | 285 | lua_call(L, 0, 1); /* call it */ |
286 | lua_call(L, 0, 1); | ||
287 | if (lua_isnil(L, -1)) { | 286 | if (lua_isnil(L, -1)) { |
288 | *size = 0; | 287 | *size = 0; |
289 | return NULL; | 288 | return NULL; |
290 | } | 289 | } |
291 | else if (lua_isstring(L, -1)) { | 290 | else if (lua_isstring(L, -1)) { |
292 | const char *res = lua_tostring(L, -1); | 291 | const char *res; |
293 | *size = lua_strlen(L, -1); | 292 | lua_replace(L, 3); /* save string in a reserved stack slot */ |
294 | al->res = luaL_ref(L, LUA_REGISTRYINDEX); | 293 | res = lua_tostring(L, 3); |
294 | *size = lua_strlen(L, 3); | ||
295 | return res; | 295 | return res; |
296 | } | 296 | } |
297 | else luaL_error(L, "reader function must return a string"); | 297 | else luaL_error(L, "reader function must return a string"); |
@@ -300,16 +300,11 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) { | |||
300 | 300 | ||
301 | 301 | ||
302 | static int luaB_load (lua_State *L) { | 302 | static int luaB_load (lua_State *L) { |
303 | struct Aux_load al; | ||
304 | int status; | 303 | int status; |
305 | const char *cname = luaL_optstring(L, 2, "=(load)"); | 304 | const char *cname = luaL_optstring(L, 2, "=(load)"); |
306 | luaL_checktype(L, 1, LUA_TFUNCTION); | 305 | luaL_checktype(L, 1, LUA_TFUNCTION); |
307 | lua_settop(L, 1); | 306 | lua_settop(L, 3); /* function, eventual name, plus one reserved slot */ |
308 | al.func = luaL_ref(L, LUA_REGISTRYINDEX); | 307 | status = lua_load(L, generic_reader, NULL, cname); |
309 | al.res = LUA_REFNIL; | ||
310 | status = lua_load(L, generic_reader, &al, cname); | ||
311 | luaL_unref(L, al.func, LUA_REGISTRYINDEX); | ||
312 | luaL_unref(L, al.res, LUA_REGISTRYINDEX); | ||
313 | return load_aux(L, status); | 308 | return load_aux(L, status); |
314 | } | 309 | } |
315 | 310 | ||