diff options
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 35 |
1 files changed, 4 insertions, 31 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.269 2011/11/14 17:10:24 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.270 2011/11/23 17:29:04 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,6 @@ static int luaB_loadfile (lua_State *L) { | |||
273 | */ | 273 | */ |
274 | 274 | ||
275 | 275 | ||
276 | typedef struct { | ||
277 | const char *mode; | ||
278 | } loaddata; | ||
279 | |||
280 | |||
281 | /* | ||
282 | ** check whether a chunk (prefix in 's') satisfies given 'mode' | ||
283 | ** ('t' for text, 'b' for binary). Returns error message (also | ||
284 | ** pushed on the stack) in case of errors. | ||
285 | */ | ||
286 | static const char *checkrights (lua_State *L, const char *mode, const char *s) { | ||
287 | const char *x = (*s == LUA_SIGNATURE[0]) ? "binary" : "text"; | ||
288 | if (strchr(mode, x[0]) == NULL) | ||
289 | return lua_pushfstring(L, | ||
290 | "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode); | ||
291 | else return NULL; | ||
292 | } | ||
293 | |||
294 | |||
295 | /* | 276 | /* |
296 | ** reserved slot, above all arguments, to hold a copy of the returned | 277 | ** reserved slot, above all arguments, to hold a copy of the returned |
297 | ** string to avoid it being collected while parsed. 'load' has four | 278 | ** string to avoid it being collected while parsed. 'load' has four |
@@ -308,7 +289,7 @@ static const char *checkrights (lua_State *L, const char *mode, const char *s) { | |||
308 | */ | 289 | */ |
309 | static const char *generic_reader (lua_State *L, void *ud, size_t *size) { | 290 | static const char *generic_reader (lua_State *L, void *ud, size_t *size) { |
310 | const char *s; | 291 | const char *s; |
311 | loaddata *ld = (loaddata *)ud; | 292 | (void)(ud); /* not used */ |
312 | luaL_checkstack(L, 2, "too many nested functions"); | 293 | luaL_checkstack(L, 2, "too many nested functions"); |
313 | lua_pushvalue(L, 1); /* get function */ | 294 | lua_pushvalue(L, 1); /* get function */ |
314 | lua_call(L, 0, 1); /* call it */ | 295 | lua_call(L, 0, 1); /* call it */ |
@@ -317,11 +298,6 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) { | |||
317 | return NULL; | 298 | return NULL; |
318 | } | 299 | } |
319 | else if ((s = lua_tostring(L, -1)) != NULL) { | 300 | else if ((s = lua_tostring(L, -1)) != NULL) { |
320 | if (ld->mode != NULL) { /* first time? */ | ||
321 | s = checkrights(L, ld->mode, s); /* check mode */ | ||
322 | ld->mode = NULL; /* to avoid further checks */ | ||
323 | if (s) luaL_error(L, s); | ||
324 | } | ||
325 | lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ | 301 | lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ |
326 | return lua_tolstring(L, RESERVEDSLOT, size); | 302 | return lua_tolstring(L, RESERVEDSLOT, size); |
327 | } | 303 | } |
@@ -340,16 +316,13 @@ static int luaB_load (lua_State *L) { | |||
340 | const char *mode = luaL_optstring(L, 3, "bt"); | 316 | const char *mode = luaL_optstring(L, 3, "bt"); |
341 | if (s != NULL) { /* loading a string? */ | 317 | if (s != NULL) { /* loading a string? */ |
342 | const char *chunkname = luaL_optstring(L, 2, s); | 318 | const char *chunkname = luaL_optstring(L, 2, s); |
343 | status = (checkrights(L, mode, s) != NULL) | 319 | status = luaL_loadbufferx(L, s, l, chunkname, mode); |
344 | || luaL_loadbuffer(L, s, l, chunkname); | ||
345 | } | 320 | } |
346 | else { /* loading from a reader function */ | 321 | else { /* loading from a reader function */ |
347 | const char *chunkname = luaL_optstring(L, 2, "=(load)"); | 322 | const char *chunkname = luaL_optstring(L, 2, "=(load)"); |
348 | loaddata ld; | ||
349 | ld.mode = mode; | ||
350 | luaL_checktype(L, 1, LUA_TFUNCTION); | 323 | luaL_checktype(L, 1, LUA_TFUNCTION); |
351 | lua_settop(L, RESERVEDSLOT); /* create reserved slot */ | 324 | lua_settop(L, RESERVEDSLOT); /* create reserved slot */ |
352 | status = lua_load(L, generic_reader, &ld, chunkname); | 325 | status = lua_load(L, generic_reader, NULL, chunkname, mode); |
353 | } | 326 | } |
354 | if (status == LUA_OK && top >= 4) { /* is there an 'env' argument */ | 327 | if (status == LUA_OK && top >= 4) { /* is there an 'env' argument */ |
355 | lua_pushvalue(L, 4); /* environment for loaded function */ | 328 | lua_pushvalue(L, 4); /* environment for loaded function */ |