aboutsummaryrefslogtreecommitdiff
path: root/loadlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-03-17 18:37:37 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-03-17 18:37:37 -0300
commit1514e49d4321efc30d597e05a072266c7c4d697d (patch)
treeaffcab00868f3b34091b85519a0ee4da8728b032 /loadlib.c
parent22ef84b6c8d980eb869f414610f8ff5f25568ca7 (diff)
downloadlua-1514e49d4321efc30d597e05a072266c7c4d697d.tar.gz
lua-1514e49d4321efc30d597e05a072266c7c4d697d.tar.bz2
lua-1514e49d4321efc30d597e05a072266c7c4d697d.zip
avoid using function environments in C libraries (as it probably will
be deprecated)
Diffstat (limited to 'loadlib.c')
-rw-r--r--loadlib.c21
1 files changed, 10 insertions, 11 deletions
diff --git a/loadlib.c b/loadlib.c
index eba25e30..44112e31 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.79 2010/01/13 16:09:05 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.80 2010/01/13 16:30:27 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**
@@ -353,9 +353,7 @@ static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
353 lua_CFunction f = ll_sym(L, *reg, sym); 353 lua_CFunction f = ll_sym(L, *reg, sym);
354 if (f == NULL) 354 if (f == NULL)
355 return ERRFUNC; /* unable to find function */ 355 return ERRFUNC; /* unable to find function */
356 lua_pushcfunction(L, f); /* else create new function... */ 356 lua_pushcfunction(L, f); /* else create new function */
357 lua_pushglobaltable(L); /* ... and set the standard global table... */
358 lua_setfenv(L, -2); /* ... as its environment */
359 return 0; /* no errors */ 357 return 0; /* no errors */
360 } 358 }
361} 359}
@@ -435,7 +433,7 @@ static int ll_searchpath (lua_State *L) {
435static const char *findfile (lua_State *L, const char *name, 433static const char *findfile (lua_State *L, const char *name,
436 const char *pname) { 434 const char *pname) {
437 const char *path; 435 const char *path;
438 lua_getfield(L, LUA_ENVIRONINDEX, pname); 436 lua_getfield(L, lua_upvalueindex(1), pname);
439 path = lua_tostring(L, -1); 437 path = lua_tostring(L, -1);
440 if (path == NULL) 438 if (path == NULL)
441 luaL_error(L, LUA_QL("package.%s") " must be a string", pname); 439 luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
@@ -509,7 +507,7 @@ static int loader_Croot (lua_State *L) {
509 507
510static int loader_preload (lua_State *L) { 508static int loader_preload (lua_State *L) {
511 const char *name = luaL_checkstring(L, 1); 509 const char *name = luaL_checkstring(L, 1);
512 lua_getfield(L, LUA_ENVIRONINDEX, "preload"); 510 lua_getfield(L, lua_upvalueindex(1), "preload");
513 if (!lua_istable(L, -1)) 511 if (!lua_istable(L, -1))
514 luaL_error(L, LUA_QL("package.preload") " must be a table"); 512 luaL_error(L, LUA_QL("package.preload") " must be a table");
515 lua_getfield(L, -1, name); 513 lua_getfield(L, -1, name);
@@ -535,7 +533,7 @@ static int ll_require (lua_State *L) {
535 return 1; /* package is already loaded */ 533 return 1; /* package is already loaded */
536 } 534 }
537 /* else must load it; iterate over available loaders */ 535 /* else must load it; iterate over available loaders */
538 lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); 536 lua_getfield(L, lua_upvalueindex(1), "loaders");
539 if (!lua_istable(L, -1)) 537 if (!lua_istable(L, -1))
540 luaL_error(L, LUA_QL("package.loaders") " must be a table"); 538 luaL_error(L, LUA_QL("package.loaders") " must be a table");
541 lua_pushliteral(L, ""); /* error message accumulator */ 539 lua_pushliteral(L, ""); /* error message accumulator */
@@ -709,12 +707,12 @@ LUAMOD_API int luaopen_package (lua_State *L) {
709 lua_setfield(L, -2, "__gc"); 707 lua_setfield(L, -2, "__gc");
710 /* create `package' table */ 708 /* create `package' table */
711 luaL_register(L, LUA_LOADLIBNAME, pk_funcs); 709 luaL_register(L, LUA_LOADLIBNAME, pk_funcs);
712 lua_copy(L, -1, LUA_ENVIRONINDEX);
713 /* create `loaders' table */ 710 /* create `loaders' table */
714 lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0); 711 lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);
715 /* fill it with pre-defined loaders */ 712 /* fill it with pre-defined loaders */
716 for (i=0; loaders[i] != NULL; i++) { 713 for (i=0; loaders[i] != NULL; i++) {
717 lua_pushcfunction(L, loaders[i]); 714 lua_pushvalue(L, -2); /* set 'package' as upvalue for all loaders */
715 lua_pushcclosure(L, loaders[i], 1);
718 lua_rawseti(L, -2, i+1); 716 lua_rawseti(L, -2, i+1);
719 } 717 }
720 lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */ 718 lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */
@@ -731,8 +729,9 @@ LUAMOD_API int luaopen_package (lua_State *L) {
731 lua_newtable(L); 729 lua_newtable(L);
732 lua_setfield(L, -2, "preload"); 730 lua_setfield(L, -2, "preload");
733 lua_pushglobaltable(L); 731 lua_pushglobaltable(L);
734 luaL_register(L, NULL, ll_funcs); /* open lib into global table */ 732 lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
735 lua_pop(L, 1); 733 luaL_openlib(L, NULL, ll_funcs, 1); /* open lib into global table */
734 lua_pop(L, 1); /* pop global table */
736 return 1; /* return 'package' table */ 735 return 1; /* return 'package' table */
737} 736}
738 737