diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-03-13 12:50:03 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-03-13 12:50:03 -0300 |
commit | 9428ec42d0c17e5ebb887c01bab419a42a1072db (patch) | |
tree | 4fedbbc76f0692bdb0c9947a2fad766d49087ad0 /lbaselib.c | |
parent | 6ffcf2136788b032bdc0f8520b1bbc2a4d2ea76e (diff) | |
download | lua-9428ec42d0c17e5ebb887c01bab419a42a1072db.tar.gz lua-9428ec42d0c17e5ebb887c01bab419a42a1072db.tar.bz2 lua-9428ec42d0c17e5ebb887c01bab419a42a1072db.zip |
new optional argument to 'load', to control allowed modes (binary or
textual chunks)
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 56 |
1 files changed, 41 insertions, 15 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.210 2009/02/07 12:23:15 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.211 2009/03/10 17:14:37 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 | */ |
@@ -9,6 +9,7 @@ | |||
9 | #include <ctype.h> | 9 | #include <ctype.h> |
10 | #include <stdio.h> | 10 | #include <stdio.h> |
11 | #include <stdlib.h> | 11 | #include <stdlib.h> |
12 | #include <string.h> | ||
12 | 13 | ||
13 | #define lbaselib_c | 14 | #define lbaselib_c |
14 | #define LUA_LIB | 15 | #define LUA_LIB |
@@ -273,14 +274,6 @@ static int load_aux (lua_State *L, int status) { | |||
273 | } | 274 | } |
274 | 275 | ||
275 | 276 | ||
276 | static int luaB_loadstring (lua_State *L) { | ||
277 | size_t l; | ||
278 | const char *s = luaL_checklstring(L, 1, &l); | ||
279 | const char *chunkname = luaL_optstring(L, 2, s); | ||
280 | return load_aux(L, luaL_loadbuffer(L, s, l, chunkname)); | ||
281 | } | ||
282 | |||
283 | |||
284 | static int luaB_loadfile (lua_State *L) { | 277 | static int luaB_loadfile (lua_State *L) { |
285 | const char *fname = luaL_optstring(L, 1, NULL); | 278 | const char *fname = luaL_optstring(L, 1, NULL); |
286 | return load_aux(L, luaL_loadfile(L, fname)); | 279 | return load_aux(L, luaL_loadfile(L, fname)); |
@@ -293,8 +286,20 @@ static int luaB_loadfile (lua_State *L) { | |||
293 | ** stack top. Instead, it keeps its resulting string in a | 286 | ** stack top. Instead, it keeps its resulting string in a |
294 | ** reserved slot inside the stack. | 287 | ** reserved slot inside the stack. |
295 | */ | 288 | */ |
289 | |||
290 | |||
291 | static const char *checkrights (lua_State *L, const char *mode, const char *s) { | ||
292 | if (strchr(mode, 'b') == NULL && *s == LUA_SIGNATURE[0]) | ||
293 | return lua_pushstring(L, "attempt to load a binary chunk"); | ||
294 | if (strchr(mode, 't') == NULL && *s != LUA_SIGNATURE[0]) | ||
295 | return lua_pushstring(L, "attempt to load a text chunk"); | ||
296 | return NULL; /* chunk in allowed format */ | ||
297 | } | ||
298 | |||
299 | |||
296 | static const char *generic_reader (lua_State *L, void *ud, size_t *size) { | 300 | static const char *generic_reader (lua_State *L, void *ud, size_t *size) { |
297 | (void)ud; /* to avoid warnings */ | 301 | const char *s; |
302 | const char **mode = (const char **)ud; | ||
298 | luaL_checkstack(L, 2, "too many nested functions"); | 303 | luaL_checkstack(L, 2, "too many nested functions"); |
299 | lua_pushvalue(L, 1); /* get function */ | 304 | lua_pushvalue(L, 1); /* get function */ |
300 | lua_call(L, 0, 1); /* call it */ | 305 | lua_call(L, 0, 1); /* call it */ |
@@ -302,7 +307,12 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) { | |||
302 | *size = 0; | 307 | *size = 0; |
303 | return NULL; | 308 | return NULL; |
304 | } | 309 | } |
305 | else if (lua_isstring(L, -1)) { | 310 | else if ((s = lua_tostring(L, -1)) != NULL) { |
311 | if (*mode != NULL) { /* first time? */ | ||
312 | s = checkrights(L, *mode, s); /* check whether chunk format is allowed */ | ||
313 | *mode = NULL; /* to avoid further checks */ | ||
314 | if (s) luaL_error(L, s); | ||
315 | } | ||
306 | lua_replace(L, 3); /* save string in a reserved stack slot */ | 316 | lua_replace(L, 3); /* save string in a reserved stack slot */ |
307 | return lua_tolstring(L, 3, size); | 317 | return lua_tolstring(L, 3, size); |
308 | } | 318 | } |
@@ -315,14 +325,30 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) { | |||
315 | 325 | ||
316 | static int luaB_load (lua_State *L) { | 326 | static int luaB_load (lua_State *L) { |
317 | int status; | 327 | int status; |
318 | const char *cname = luaL_optstring(L, 2, "=(load)"); | 328 | const char *s = lua_tostring(L, 1); |
319 | luaL_checktype(L, 1, LUA_TFUNCTION); | 329 | const char *mode = luaL_optstring(L, 3, "bt"); |
320 | lua_settop(L, 3); /* function, eventual name, plus one reserved slot */ | 330 | if (s != NULL) { /* loading a string? */ |
321 | status = lua_load(L, generic_reader, NULL, cname); | 331 | const char *chunkname = luaL_optstring(L, 2, s); |
332 | status = (checkrights(L, mode, s) != NULL) | ||
333 | || luaL_loadbuffer(L, s, lua_objlen(L, 1), chunkname); | ||
334 | } | ||
335 | else { /* loading from a reader function */ | ||
336 | const char *chunkname = luaL_optstring(L, 2, "=(load)"); | ||
337 | luaL_checktype(L, 1, LUA_TFUNCTION); | ||
338 | lua_settop(L, 3); /* function, eventual name, plus one reserved slot */ | ||
339 | status = lua_load(L, generic_reader, &mode, chunkname); | ||
340 | } | ||
322 | return load_aux(L, status); | 341 | return load_aux(L, status); |
323 | } | 342 | } |
324 | 343 | ||
325 | 344 | ||
345 | static int luaB_loadstring (lua_State *L) { | ||
346 | lua_settop(L, 2); | ||
347 | lua_pushliteral(L, "tb"); | ||
348 | return luaB_load(L); /* dostring(s, n) == load(s, n, "tb") */ | ||
349 | } | ||
350 | |||
351 | |||
326 | static int dofilecont (lua_State *L) { | 352 | static int dofilecont (lua_State *L) { |
327 | return lua_gettop(L) - 1; | 353 | return lua_gettop(L) - 1; |
328 | } | 354 | } |