diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-08-27 18:02:08 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-08-27 18:02:08 -0300 |
commit | beb896b08223d7ae11868ee1ea7a4858626b5ee7 (patch) | |
tree | ee5d204c8548015483e3c709de67727f24f2dd66 | |
parent | 8332d5c8a5059b85da1adaa3f0197d0f57afae81 (diff) | |
download | lua-beb896b08223d7ae11868ee1ea7a4858626b5ee7.tar.gz lua-beb896b08223d7ae11868ee1ea7a4858626b5ee7.tar.bz2 lua-beb896b08223d7ae11868ee1ea7a4858626b5ee7.zip |
new function (generic) `load'
-rw-r--r-- | lbaselib.c | 44 |
1 files changed, 43 insertions, 1 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.131 2003/05/16 18:59:08 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.132 2003/08/25 19:49:47 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 | */ |
@@ -272,6 +272,47 @@ static int luaB_loadfile (lua_State *L) { | |||
272 | } | 272 | } |
273 | 273 | ||
274 | 274 | ||
275 | struct Aux_load { | ||
276 | int func; | ||
277 | int res; | ||
278 | }; | ||
279 | |||
280 | |||
281 | static const char *generic_reader (lua_State *L, void *ud, size_t *size) { | ||
282 | struct Aux_load *al = (struct Aux_load *)ud; | ||
283 | luaL_unref(L, al->res, LUA_REGISTRYINDEX); | ||
284 | lua_getref(L, al->func); | ||
285 | lua_call(L, 0, 1); | ||
286 | if (lua_isnil(L, -1)) { | ||
287 | *size = 0; | ||
288 | return NULL; | ||
289 | } | ||
290 | else if (lua_isstring(L, -1)) { | ||
291 | const char *res = lua_tostring(L, -1); | ||
292 | *size = lua_strlen(L, -1); | ||
293 | al->res = luaL_ref(L, LUA_REGISTRYINDEX); | ||
294 | return res; | ||
295 | } | ||
296 | else luaL_error(L, "reader function must return a string"); | ||
297 | return NULL; /* to avoid warnings */ | ||
298 | } | ||
299 | |||
300 | |||
301 | static int luaB_load (lua_State *L) { | ||
302 | struct Aux_load al; | ||
303 | int status; | ||
304 | const char *cname = luaL_optstring(L, 2, "= generic load"); | ||
305 | luaL_checktype(L, 1, LUA_TFUNCTION); | ||
306 | lua_settop(L, 1); | ||
307 | al.func = luaL_ref(L, LUA_REGISTRYINDEX); | ||
308 | al.res = LUA_REFNIL; | ||
309 | status = lua_load(L, generic_reader, &al, cname); | ||
310 | luaL_unref(L, al.func, LUA_REGISTRYINDEX); | ||
311 | luaL_unref(L, al.res, LUA_REGISTRYINDEX); | ||
312 | return load_aux(L, status); | ||
313 | } | ||
314 | |||
315 | |||
275 | static int luaB_dofile (lua_State *L) { | 316 | static int luaB_dofile (lua_State *L) { |
276 | const char *fname = luaL_optstring(L, 1, NULL); | 317 | const char *fname = luaL_optstring(L, 1, NULL); |
277 | int status = luaL_loadfile(L, fname); | 318 | int status = luaL_loadfile(L, fname); |
@@ -536,6 +577,7 @@ static const luaL_reg base_funcs[] = { | |||
536 | {"loadfile", luaB_loadfile}, | 577 | {"loadfile", luaB_loadfile}, |
537 | {"dofile", luaB_dofile}, | 578 | {"dofile", luaB_dofile}, |
538 | {"loadstring", luaB_loadstring}, | 579 | {"loadstring", luaB_loadstring}, |
580 | {"load", luaB_load}, | ||
539 | {"require", luaB_require}, | 581 | {"require", luaB_require}, |
540 | {NULL, NULL} | 582 | {NULL, NULL} |
541 | }; | 583 | }; |