diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-06-28 14:14:12 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-06-28 14:14:12 -0300 |
commit | 49a01c0991462ad5c6143b0d4ba37ec35c8bca2b (patch) | |
tree | 4372cb9a371defce68b21db9de98f1ff4020959d /loadlib.c | |
parent | 17526ed2fe81c52dd6cf22d90fea7fea62d07229 (diff) | |
download | lua-49a01c0991462ad5c6143b0d4ba37ec35c8bca2b.tar.gz lua-49a01c0991462ad5c6143b0d4ba37ec35c8bca2b.tar.bz2 lua-49a01c0991462ad5c6143b0d4ba37ec35c8bca2b.zip |
'package.loaders' -> 'package.searchers'
Diffstat (limited to 'loadlib.c')
-rw-r--r-- | loadlib.c | 52 |
1 files changed, 28 insertions, 24 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: loadlib.c,v 1.97 2011/03/01 17:01:53 roberto Exp roberto $ | 2 | ** $Id: loadlib.c,v 1.98 2011/04/08 19:17:36 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 | ** |
@@ -376,7 +376,7 @@ static int checkload (lua_State *L, int stat, const char *filename) { | |||
376 | } | 376 | } |
377 | 377 | ||
378 | 378 | ||
379 | static int loader_Lua (lua_State *L) { | 379 | static int searcher_Lua (lua_State *L) { |
380 | const char *filename; | 380 | const char *filename; |
381 | const char *name = luaL_checkstring(L, 1); | 381 | const char *name = luaL_checkstring(L, 1); |
382 | filename = findfile(L, name, "path"); | 382 | filename = findfile(L, name, "path"); |
@@ -403,7 +403,7 @@ static int loadfunc (lua_State *L, const char *filename, const char *modname) { | |||
403 | } | 403 | } |
404 | 404 | ||
405 | 405 | ||
406 | static int loader_C (lua_State *L) { | 406 | static int searcher_C (lua_State *L) { |
407 | const char *name = luaL_checkstring(L, 1); | 407 | const char *name = luaL_checkstring(L, 1); |
408 | const char *filename = findfile(L, name, "cpath"); | 408 | const char *filename = findfile(L, name, "cpath"); |
409 | if (filename == NULL) return 1; /* module not found in this path */ | 409 | if (filename == NULL) return 1; /* module not found in this path */ |
@@ -411,7 +411,7 @@ static int loader_C (lua_State *L) { | |||
411 | } | 411 | } |
412 | 412 | ||
413 | 413 | ||
414 | static int loader_Croot (lua_State *L) { | 414 | static int searcher_Croot (lua_State *L) { |
415 | const char *filename; | 415 | const char *filename; |
416 | const char *name = luaL_checkstring(L, 1); | 416 | const char *name = luaL_checkstring(L, 1); |
417 | const char *p = strchr(name, '.'); | 417 | const char *p = strchr(name, '.'); |
@@ -434,7 +434,7 @@ static int loader_Croot (lua_State *L) { | |||
434 | } | 434 | } |
435 | 435 | ||
436 | 436 | ||
437 | static int loader_preload (lua_State *L) { | 437 | static int searcher_preload (lua_State *L) { |
438 | const char *name = luaL_checkstring(L, 1); | 438 | const char *name = luaL_checkstring(L, 1); |
439 | lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); | 439 | lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); |
440 | lua_getfield(L, -1, name); | 440 | lua_getfield(L, -1, name); |
@@ -452,30 +452,30 @@ static int ll_require (lua_State *L) { | |||
452 | lua_getfield(L, 2, name); | 452 | lua_getfield(L, 2, name); |
453 | if (lua_toboolean(L, -1)) /* is it there? */ | 453 | if (lua_toboolean(L, -1)) /* is it there? */ |
454 | return 1; /* package is already loaded */ | 454 | return 1; /* package is already loaded */ |
455 | /* else must load it; iterate over available loaders */ | 455 | /* else must load it; iterate over available seachers to find a loader */ |
456 | lua_getfield(L, lua_upvalueindex(1), "loaders"); | 456 | lua_getfield(L, lua_upvalueindex(1), "searchers"); |
457 | if (!lua_istable(L, -1)) | 457 | if (!lua_istable(L, -1)) |
458 | luaL_error(L, LUA_QL("package.loaders") " must be a table"); | 458 | luaL_error(L, LUA_QL("package.searchers") " must be a table"); |
459 | lua_pushliteral(L, ""); /* error message accumulator */ | 459 | lua_pushliteral(L, ""); /* error message accumulator */ |
460 | for (i=1; ; i++) { | 460 | for (i=1; ; i++) { |
461 | lua_rawgeti(L, -2, i); /* get a loader */ | 461 | lua_rawgeti(L, -2, i); /* get a seacher */ |
462 | if (lua_isnil(L, -1)) | 462 | if (lua_isnil(L, -1)) /* no more searchers? */ |
463 | luaL_error(L, "module " LUA_QS " not found:%s", | 463 | luaL_error(L, "module " LUA_QS " not found:%s", |
464 | name, lua_tostring(L, -2)); | 464 | name, lua_tostring(L, -2)); |
465 | lua_pushstring(L, name); | 465 | lua_pushstring(L, name); |
466 | lua_call(L, 1, 2); /* call it */ | 466 | lua_call(L, 1, 2); /* call it */ |
467 | if (lua_isfunction(L, -2)) /* did it find module? */ | 467 | if (lua_isfunction(L, -2)) /* did it find a loader? */ |
468 | break; /* module loaded successfully */ | 468 | break; /* module loader found */ |
469 | else if (lua_isstring(L, -2)) { /* loader returned error message? */ | 469 | else if (lua_isstring(L, -2)) { /* searcher returned error message? */ |
470 | lua_pop(L, 1); /* remove extra return */ | 470 | lua_pop(L, 1); /* remove extra return */ |
471 | lua_concat(L, 2); /* accumulate error message */ | 471 | lua_concat(L, 2); /* accumulate error message */ |
472 | } | 472 | } |
473 | else | 473 | else |
474 | lua_pop(L, 2); /* remove both returns */ | 474 | lua_pop(L, 2); /* remove both returns */ |
475 | } | 475 | } |
476 | lua_pushstring(L, name); /* pass name as argument to module */ | 476 | lua_pushstring(L, name); /* pass name as argument to module loader */ |
477 | lua_insert(L, -2); /* name is 1st argument (before search data) */ | 477 | lua_insert(L, -2); /* name is 1st argument (before search data) */ |
478 | lua_call(L, 2, 1); /* run loaded module */ | 478 | lua_call(L, 2, 1); /* run loader to load module */ |
479 | if (!lua_isnil(L, -1)) /* non-nil return? */ | 479 | if (!lua_isnil(L, -1)) /* non-nil return? */ |
480 | lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ | 480 | lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ |
481 | lua_getfield(L, 2, name); | 481 | lua_getfield(L, 2, name); |
@@ -622,8 +622,8 @@ static const luaL_Reg ll_funcs[] = { | |||
622 | }; | 622 | }; |
623 | 623 | ||
624 | 624 | ||
625 | static const lua_CFunction loaders[] = | 625 | static const lua_CFunction searchers[] = |
626 | {loader_preload, loader_Lua, loader_C, loader_Croot, NULL}; | 626 | {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL}; |
627 | 627 | ||
628 | 628 | ||
629 | LUAMOD_API int luaopen_package (lua_State *L) { | 629 | LUAMOD_API int luaopen_package (lua_State *L) { |
@@ -634,15 +634,19 @@ LUAMOD_API int luaopen_package (lua_State *L) { | |||
634 | lua_setfield(L, -2, "__gc"); | 634 | lua_setfield(L, -2, "__gc"); |
635 | /* create `package' table */ | 635 | /* create `package' table */ |
636 | luaL_newlib(L, pk_funcs); | 636 | luaL_newlib(L, pk_funcs); |
637 | /* create `loaders' table */ | 637 | /* create 'searchers' table */ |
638 | lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0); | 638 | lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); |
639 | /* fill it with pre-defined loaders */ | 639 | /* fill it with pre-defined searchers */ |
640 | for (i=0; loaders[i] != NULL; i++) { | 640 | for (i=0; searchers[i] != NULL; i++) { |
641 | lua_pushvalue(L, -2); /* set 'package' as upvalue for all loaders */ | 641 | lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ |
642 | lua_pushcclosure(L, loaders[i], 1); | 642 | lua_pushcclosure(L, searchers[i], 1); |
643 | lua_rawseti(L, -2, i+1); | 643 | lua_rawseti(L, -2, i+1); |
644 | } | 644 | } |
645 | lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */ | 645 | #if defined(LUA_COMPAT_LOADERS) |
646 | lua_pushvalue(L, -1); /* make a copy of 'searchers' table */ | ||
647 | lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */ | ||
648 | #endif | ||
649 | lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */ | ||
646 | /* set field 'path' */ | 650 | /* set field 'path' */ |
647 | setpath(L, "path", LUA_PATHVERSION, LUA_PATH, LUA_PATH_DEFAULT); | 651 | setpath(L, "path", LUA_PATHVERSION, LUA_PATH, LUA_PATH_DEFAULT); |
648 | /* set field 'cpath' */ | 652 | /* set field 'cpath' */ |