aboutsummaryrefslogtreecommitdiff
path: root/lbaselib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lbaselib.c')
-rw-r--r--lbaselib.c30
1 files changed, 14 insertions, 16 deletions
diff --git a/lbaselib.c b/lbaselib.c
index 5260fc35..4536d617 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.273 2011/11/30 13:03:24 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.274 2012/04/27 14:13:19 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*/
@@ -242,10 +242,16 @@ static int luaB_ipairs (lua_State *L) {
242} 242}
243 243
244 244
245static int load_aux (lua_State *L, int status) { 245static int load_aux (lua_State *L, int status, int envidx) {
246 if (status == LUA_OK) 246 if (status == LUA_OK) {
247 if (envidx != 0) { /* 'env' parameter? */
248 lua_pushvalue(L, envidx); /* environment for loaded function */
249 if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */
250 lua_pop(L, 1); /* remove 'env' if not used by previous call */
251 }
247 return 1; 252 return 1;
248 else { 253 }
254 else { /* error (message is on top of the stack) */
249 lua_pushnil(L); 255 lua_pushnil(L);
250 lua_insert(L, -2); /* put before error message */ 256 lua_insert(L, -2); /* put before error message */
251 return 2; /* return nil plus error message */ 257 return 2; /* return nil plus error message */
@@ -256,13 +262,9 @@ static int load_aux (lua_State *L, int status) {
256static int luaB_loadfile (lua_State *L) { 262static int luaB_loadfile (lua_State *L) {
257 const char *fname = luaL_optstring(L, 1, NULL); 263 const char *fname = luaL_optstring(L, 1, NULL);
258 const char *mode = luaL_optstring(L, 2, NULL); 264 const char *mode = luaL_optstring(L, 2, NULL);
259 int env = !lua_isnone(L, 3); /* 'env' parameter? */ 265 int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */
260 int status = luaL_loadfilex(L, fname, mode); 266 int status = luaL_loadfilex(L, fname, mode);
261 if (status == LUA_OK && env) { /* 'env' parameter? */ 267 return load_aux(L, status, env);
262 lua_pushvalue(L, 3);
263 lua_setupvalue(L, -2, 1); /* set it as 1st upvalue of loaded chunk */
264 }
265 return load_aux(L, status);
266} 268}
267 269
268 270
@@ -307,9 +309,9 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
307static int luaB_load (lua_State *L) { 309static int luaB_load (lua_State *L) {
308 int status; 310 int status;
309 size_t l; 311 size_t l;
310 int top = lua_gettop(L);
311 const char *s = lua_tolstring(L, 1, &l); 312 const char *s = lua_tolstring(L, 1, &l);
312 const char *mode = luaL_optstring(L, 3, "bt"); 313 const char *mode = luaL_optstring(L, 3, "bt");
314 int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */
313 if (s != NULL) { /* loading a string? */ 315 if (s != NULL) { /* loading a string? */
314 const char *chunkname = luaL_optstring(L, 2, s); 316 const char *chunkname = luaL_optstring(L, 2, s);
315 status = luaL_loadbufferx(L, s, l, chunkname, mode); 317 status = luaL_loadbufferx(L, s, l, chunkname, mode);
@@ -320,11 +322,7 @@ static int luaB_load (lua_State *L) {
320 lua_settop(L, RESERVEDSLOT); /* create reserved slot */ 322 lua_settop(L, RESERVEDSLOT); /* create reserved slot */
321 status = lua_load(L, generic_reader, NULL, chunkname, mode); 323 status = lua_load(L, generic_reader, NULL, chunkname, mode);
322 } 324 }
323 if (status == LUA_OK && top >= 4) { /* is there an 'env' argument */ 325 return load_aux(L, status, env);
324 lua_pushvalue(L, 4); /* environment for loaded function */
325 lua_setupvalue(L, -2, 1); /* set it as 1st upvalue */
326 }
327 return load_aux(L, status);
328} 326}
329 327
330/* }====================================================== */ 328/* }====================================================== */