diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-11-09 17:11:20 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-11-09 17:11:20 -0200 |
| commit | eef22fa4ce2dbdef5fbc49c89e8dfba1ad48d776 (patch) | |
| tree | cd39d884221e6b73b71a4af6559a65227250ef9e | |
| parent | eda87f115632c9c3e234d68b422c12fc7800ab17 (diff) | |
| download | lua-eef22fa4ce2dbdef5fbc49c89e8dfba1ad48d776.tar.gz lua-eef22fa4ce2dbdef5fbc49c89e8dfba1ad48d776.tar.bz2 lua-eef22fa4ce2dbdef5fbc49c89e8dfba1ad48d776.zip | |
create error messages with a single concat, to avoid problems with
creating strings piecemeal
| -rw-r--r-- | loadlib.c | 53 |
1 files changed, 33 insertions, 20 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: loadlib.c,v 1.101 2011/11/06 13:59:12 roberto Exp roberto $ | 2 | ** $Id: loadlib.c,v 1.102 2011/11/09 15:18:04 roberto Exp roberto $ |
| 3 | ** Dynamic library loader for Lua | 3 | ** Dynamic library loader for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | ** | 5 | ** |
| @@ -344,19 +344,21 @@ static const char *searchpath (lua_State *L, const char *name, | |||
| 344 | const char *path, | 344 | const char *path, |
| 345 | const char *sep, | 345 | const char *sep, |
| 346 | const char *dirsep) { | 346 | const char *dirsep) { |
| 347 | int nerr = 0; /* number of entries in possible error message */ | ||
| 347 | if (*sep != '\0') /* non-empty separator? */ | 348 | if (*sep != '\0') /* non-empty separator? */ |
| 348 | name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ | 349 | name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ |
| 349 | lua_pushliteral(L, ""); /* error accumulator */ | ||
| 350 | while ((path = pushnexttemplate(L, path)) != NULL) { | 350 | while ((path = pushnexttemplate(L, path)) != NULL) { |
| 351 | const char *filename = luaL_gsub(L, lua_tostring(L, -1), | 351 | const char *filename = luaL_gsub(L, lua_tostring(L, -1), |
| 352 | LUA_PATH_MARK, name); | 352 | LUA_PATH_MARK, name); |
| 353 | lua_remove(L, -2); /* remove path template */ | 353 | lua_remove(L, -2); /* remove path template */ |
| 354 | if (readable(filename)) /* does file exist and is readable? */ | 354 | if (readable(filename)) /* does file exist and is readable? */ |
| 355 | return filename; /* return that file name */ | 355 | return filename; /* return that file name */ |
| 356 | luaL_checkstack(L, 1, "too many templates in path"); | ||
| 356 | lua_pushfstring(L, "\n\tno file " LUA_QS, filename); | 357 | lua_pushfstring(L, "\n\tno file " LUA_QS, filename); |
| 357 | lua_remove(L, -2); /* remove file name */ | 358 | lua_remove(L, -2); /* remove file name */ |
| 358 | lua_concat(L, 2); /* add entry to possible error message */ | 359 | nerr++; |
| 359 | } | 360 | } |
| 361 | lua_concat(L, nerr); /* create error message */ | ||
| 360 | return NULL; /* not found */ | 362 | return NULL; /* not found */ |
| 361 | } | 363 | } |
| 362 | 364 | ||
| @@ -467,35 +469,46 @@ static int searcher_preload (lua_State *L) { | |||
| 467 | } | 469 | } |
| 468 | 470 | ||
| 469 | 471 | ||
| 470 | static int ll_require (lua_State *L) { | 472 | static void findloader (lua_State *L, const char *name) { |
| 471 | const char *name = luaL_checkstring(L, 1); | ||
| 472 | int i; | 473 | int i; |
| 473 | lua_settop(L, 1); /* _LOADED table will be at index 2 */ | 474 | int nerr = 0; /* number of error messages on the stack */ |
| 474 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | 475 | lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */ |
| 475 | lua_getfield(L, 2, name); | 476 | if (!lua_istable(L, 3)) |
| 476 | if (lua_toboolean(L, -1)) /* is it there? */ | ||
| 477 | return 1; /* package is already loaded */ | ||
| 478 | /* else must load it; iterate over available seachers to find a loader */ | ||
| 479 | lua_getfield(L, lua_upvalueindex(1), "searchers"); | ||
| 480 | if (!lua_istable(L, -1)) | ||
| 481 | luaL_error(L, LUA_QL("package.searchers") " must be a table"); | 477 | luaL_error(L, LUA_QL("package.searchers") " must be a table"); |
| 482 | lua_pushliteral(L, ""); /* error message accumulator */ | 478 | /* iterate over available seachers to find a loader */ |
| 483 | for (i=1; ; i++) { | 479 | for (i = 1; ; i++) { |
| 484 | lua_rawgeti(L, -2, i); /* get a seacher */ | 480 | lua_rawgeti(L, 3, i); /* get a seacher */ |
| 485 | if (lua_isnil(L, -1)) /* no more searchers? */ | 481 | if (lua_isnil(L, -1)) { /* no more searchers? */ |
| 482 | lua_pop(L, 1); /* remove nil */ | ||
| 483 | lua_concat(L, nerr); /* concatenate all messages */ | ||
| 486 | luaL_error(L, "module " LUA_QS " not found:%s", | 484 | luaL_error(L, "module " LUA_QS " not found:%s", |
| 487 | name, lua_tostring(L, -2)); | 485 | name, lua_tostring(L, -1)); |
| 486 | } | ||
| 488 | lua_pushstring(L, name); | 487 | lua_pushstring(L, name); |
| 489 | lua_call(L, 1, 2); /* call it */ | 488 | lua_call(L, 1, 2); /* call it */ |
| 490 | if (lua_isfunction(L, -2)) /* did it find a loader? */ | 489 | if (lua_isfunction(L, -2)) /* did it find a loader? */ |
| 491 | break; /* module loader found */ | 490 | return; /* module loader found */ |
| 492 | else if (lua_isstring(L, -2)) { /* searcher returned error message? */ | 491 | else if (lua_isstring(L, -2)) { /* searcher returned error message? */ |
| 493 | lua_pop(L, 1); /* remove extra return */ | 492 | lua_pop(L, 1); /* remove extra return */ |
| 494 | lua_concat(L, 2); /* accumulate error message */ | 493 | nerr++; /* accumulate error message */ |
| 494 | luaL_checkstack(L, 1, "too many searchers"); | ||
| 495 | } | 495 | } |
| 496 | else | 496 | else |
| 497 | lua_pop(L, 2); /* remove both returns */ | 497 | lua_pop(L, 2); /* remove both returns */ |
| 498 | } | 498 | } |
| 499 | } | ||
| 500 | |||
| 501 | |||
| 502 | static int ll_require (lua_State *L) { | ||
| 503 | const char *name = luaL_checkstring(L, 1); | ||
| 504 | lua_settop(L, 1); /* _LOADED table will be at index 2 */ | ||
| 505 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | ||
| 506 | lua_getfield(L, 2, name); /* _LOADED[name] */ | ||
| 507 | if (lua_toboolean(L, -1)) /* is it there? */ | ||
| 508 | return 1; /* package is already loaded */ | ||
| 509 | /* else must load package */ | ||
| 510 | lua_pop(L, 1); /* remove 'getfield' result */ | ||
| 511 | findloader(L, name); | ||
| 499 | lua_pushstring(L, name); /* pass name as argument to module loader */ | 512 | lua_pushstring(L, name); /* pass name as argument to module loader */ |
| 500 | lua_insert(L, -2); /* name is 1st argument (before search data) */ | 513 | lua_insert(L, -2); /* name is 1st argument (before search data) */ |
| 501 | lua_call(L, 2, 1); /* run loader to load module */ | 514 | lua_call(L, 2, 1); /* run loader to load module */ |
