diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-12-13 14:38:00 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-12-13 14:38:00 -0200 |
commit | b83b6ba015d2fef9a73aa1b8e55c2d2233bd2812 (patch) | |
tree | e29e93816938f6f1a07ca9fe07d544985a4aa617 | |
parent | af119c8b55f15910df17e02633d8a5ca656b209a (diff) | |
download | lua-b83b6ba015d2fef9a73aa1b8e55c2d2233bd2812.tar.gz lua-b83b6ba015d2fef9a73aa1b8e55c2d2233bd2812.tar.bz2 lua-b83b6ba015d2fef9a73aa1b8e55c2d2233bd2812.zip |
'loadin' -> 'load'
-rw-r--r-- | lbaselib.c | 73 |
1 files changed, 29 insertions, 44 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.253 2010/12/07 11:40:42 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.254 2010/12/08 12:58: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 | */ |
@@ -253,6 +253,11 @@ static int luaB_loadfile (lua_State *L) { | |||
253 | ** ======================================================= | 253 | ** ======================================================= |
254 | */ | 254 | */ |
255 | 255 | ||
256 | /* | ||
257 | ** check whether a chunk (prefix in 's') satisfies given 'mode' | ||
258 | ** ('t' for text, 'b' for binary). Returns error message (also | ||
259 | ** pushed on the stack) in case of errors. | ||
260 | */ | ||
256 | static const char *checkrights (lua_State *L, const char *mode, const char *s) { | 261 | static const char *checkrights (lua_State *L, const char *mode, const char *s) { |
257 | if (strchr(mode, 'b') == NULL && *s == LUA_SIGNATURE[0]) | 262 | if (strchr(mode, 'b') == NULL && *s == LUA_SIGNATURE[0]) |
258 | return lua_pushstring(L, "attempt to load a binary chunk"); | 263 | return lua_pushstring(L, "attempt to load a binary chunk"); |
@@ -263,10 +268,11 @@ static const char *checkrights (lua_State *L, const char *mode, const char *s) { | |||
263 | 268 | ||
264 | 269 | ||
265 | /* | 270 | /* |
266 | ** reserves a slot, above all arguments, to hold a copy of the returned | 271 | ** reserved slot, above all arguments, to hold a copy of the returned |
267 | ** string to avoid it being collected while parsed | 272 | ** string to avoid it being collected while parsed. 'load' has four |
273 | ** optional arguments (chunk, source name, mode, and environment). | ||
268 | */ | 274 | */ |
269 | #define RESERVEDSLOT 4 | 275 | #define RESERVEDSLOT 5 |
270 | 276 | ||
271 | 277 | ||
272 | /* | 278 | /* |
@@ -275,25 +281,20 @@ static const char *checkrights (lua_State *L, const char *mode, const char *s) { | |||
275 | ** stack top. Instead, it keeps its resulting string in a | 281 | ** stack top. Instead, it keeps its resulting string in a |
276 | ** reserved slot inside the stack. | 282 | ** reserved slot inside the stack. |
277 | */ | 283 | */ |
278 | typedef struct { /* reader state */ | ||
279 | int f; /* position of reader function on stack */ | ||
280 | const char *mode; /* allowed modes (binary/text) */ | ||
281 | } Readstat; | ||
282 | |||
283 | static const char *generic_reader (lua_State *L, void *ud, size_t *size) { | 284 | static const char *generic_reader (lua_State *L, void *ud, size_t *size) { |
284 | const char *s; | 285 | const char *s; |
285 | Readstat *stat = (Readstat *)ud; | 286 | const char **mode = (const char **)ud; |
286 | luaL_checkstack(L, 2, "too many nested functions"); | 287 | luaL_checkstack(L, 2, "too many nested functions"); |
287 | lua_pushvalue(L, stat->f); /* get function */ | 288 | lua_pushvalue(L, 1); /* get function */ |
288 | lua_call(L, 0, 1); /* call it */ | 289 | lua_call(L, 0, 1); /* call it */ |
289 | if (lua_isnil(L, -1)) { | 290 | if (lua_isnil(L, -1)) { |
290 | *size = 0; | 291 | *size = 0; |
291 | return NULL; | 292 | return NULL; |
292 | } | 293 | } |
293 | else if ((s = lua_tostring(L, -1)) != NULL) { | 294 | else if ((s = lua_tostring(L, -1)) != NULL) { |
294 | if (stat->mode != NULL) { /* first time? */ | 295 | if (*mode != NULL) { /* first time? */ |
295 | s = checkrights(L, stat->mode, s); /* check mode */ | 296 | s = checkrights(L, *mode, s); /* check mode */ |
296 | stat->mode = NULL; /* to avoid further checks */ | 297 | *mode = NULL; /* to avoid further checks */ |
297 | if (s) luaL_error(L, s); | 298 | if (s) luaL_error(L, s); |
298 | } | 299 | } |
299 | lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ | 300 | lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ |
@@ -306,43 +307,28 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) { | |||
306 | } | 307 | } |
307 | 308 | ||
308 | 309 | ||
309 | static int luaB_load_aux (lua_State *L, int farg) { | 310 | static int luaB_load (lua_State *L) { |
310 | int status; | 311 | int status; |
311 | Readstat stat; | ||
312 | size_t l; | 312 | size_t l; |
313 | const char *s = lua_tolstring(L, farg, &l); | 313 | int top = lua_gettop(L); |
314 | stat.mode = luaL_optstring(L, farg + 2, "bt"); | 314 | const char *s = lua_tolstring(L, 1, &l); |
315 | const char *mode = luaL_optstring(L, 3, "bt"); | ||
315 | if (s != NULL) { /* loading a string? */ | 316 | if (s != NULL) { /* loading a string? */ |
316 | const char *chunkname = luaL_optstring(L, farg + 1, s); | 317 | const char *chunkname = luaL_optstring(L, 2, s); |
317 | status = (checkrights(L, stat.mode, s) != NULL) | 318 | status = (checkrights(L, mode, s) != NULL) |
318 | || luaL_loadbuffer(L, s, l, chunkname); | 319 | || luaL_loadbuffer(L, s, l, chunkname); |
319 | } | 320 | } |
320 | else { /* loading from a reader function */ | 321 | else { /* loading from a reader function */ |
321 | const char *chunkname = luaL_optstring(L, farg + 1, "=(load)"); | 322 | const char *chunkname = luaL_optstring(L, 2, "=(load)"); |
322 | luaL_checktype(L, farg, LUA_TFUNCTION); | 323 | luaL_checktype(L, 1, LUA_TFUNCTION); |
323 | stat.f = farg; | ||
324 | lua_settop(L, RESERVEDSLOT); /* create reserved slot */ | 324 | lua_settop(L, RESERVEDSLOT); /* create reserved slot */ |
325 | status = lua_load(L, generic_reader, &stat, chunkname); | 325 | status = lua_load(L, generic_reader, &mode, chunkname); |
326 | } | 326 | } |
327 | return load_aux(L, status); | 327 | if (status == LUA_OK && top >= 4) { /* is there an 'env' argument */ |
328 | } | 328 | lua_pushvalue(L, 4); /* environment for loaded function */ |
329 | 329 | lua_setupvalue(L, -2, 1); /* set it as 1st upvalue */ | |
330 | |||
331 | static int luaB_load (lua_State *L) { | ||
332 | return luaB_load_aux(L, 1); | ||
333 | } | ||
334 | |||
335 | |||
336 | static int luaB_loadin (lua_State *L) { | ||
337 | int n; | ||
338 | luaL_checkany(L, 1); | ||
339 | n = luaB_load_aux(L, 2); | ||
340 | if (n == 1) { /* success? */ | ||
341 | lua_pushvalue(L, 1); /* environment for loaded function */ | ||
342 | if (lua_setupvalue(L, -2, 1) == NULL) | ||
343 | luaL_error(L, "loaded chunk does not have an upvalue"); | ||
344 | } | 330 | } |
345 | return n; | 331 | return load_aux(L, status); |
346 | } | 332 | } |
347 | 333 | ||
348 | 334 | ||
@@ -447,7 +433,7 @@ static int luaB_newproxy (lua_State *L) { | |||
447 | else if (lua_isboolean(L, 1)) { | 433 | else if (lua_isboolean(L, 1)) { |
448 | lua_createtable(L, 0, 1); /* create a new metatable `m' ... */ | 434 | lua_createtable(L, 0, 1); /* create a new metatable `m' ... */ |
449 | lua_pushboolean(L, 1); | 435 | lua_pushboolean(L, 1); |
450 | lua_setfield(L, -2, "__gc"); /* ... m.__gc = false (HACK!!)... */ | 436 | lua_setfield(L, -2, "__gc"); /* ... m.__gc = true (HACK!!)... */ |
451 | lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */ | 437 | lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */ |
452 | lua_pushboolean(L, 1); | 438 | lua_pushboolean(L, 1); |
453 | lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */ | 439 | lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */ |
@@ -477,7 +463,6 @@ static const luaL_Reg base_funcs[] = { | |||
477 | {"ipairs", luaB_ipairs}, | 463 | {"ipairs", luaB_ipairs}, |
478 | {"loadfile", luaB_loadfile}, | 464 | {"loadfile", luaB_loadfile}, |
479 | {"load", luaB_load}, | 465 | {"load", luaB_load}, |
480 | {"loadin", luaB_loadin}, | ||
481 | {"loadstring", luaB_loadstring}, | 466 | {"loadstring", luaB_loadstring}, |
482 | {"next", luaB_next}, | 467 | {"next", luaB_next}, |
483 | {"pairs", luaB_pairs}, | 468 | {"pairs", luaB_pairs}, |