aboutsummaryrefslogtreecommitdiff
path: root/loadlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-01-04 14:36:39 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-01-04 14:36:39 -0200
commit35e729fa6d96f39015c991e4036fbdb0a9c8977b (patch)
treef7630db72fbd4d60951b7006332e2af20996616b /loadlib.c
parent0dc09cb42e558aabf9ffca397b5588e6ee2fecfa (diff)
downloadlua-35e729fa6d96f39015c991e4036fbdb0a9c8977b.tar.gz
lua-35e729fa6d96f39015c991e4036fbdb0a9c8977b.tar.bz2
lua-35e729fa6d96f39015c991e4036fbdb0a9c8977b.zip
comments + small details
Diffstat (limited to 'loadlib.c')
-rw-r--r--loadlib.c30
1 files changed, 14 insertions, 16 deletions
diff --git a/loadlib.c b/loadlib.c
index b9794043..3ca27ce4 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.70 2009/12/17 13:06:47 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.71 2009/12/22 15:32:50 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**
@@ -79,6 +79,9 @@
79#define setprogdir(L) ((void)0) 79#define setprogdir(L) ((void)0)
80 80
81 81
82/*
83** system-dependent functions
84*/
82static void ll_unloadlib (void *lib); 85static void ll_unloadlib (void *lib);
83static void *ll_load (lua_State *L, const char *path, int seeglb); 86static void *ll_load (lua_State *L, const char *path, int seeglb);
84static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); 87static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);
@@ -304,7 +307,7 @@ static void **ll_register (lua_State *L, const char *path) {
304 if (!lua_isnil(L, -1)) /* is there an entry? */ 307 if (!lua_isnil(L, -1)) /* is there an entry? */
305 plib = (void **)lua_touserdata(L, -1); 308 plib = (void **)lua_touserdata(L, -1);
306 else { /* no entry yet; create one */ 309 else { /* no entry yet; create one */
307 lua_pop(L, 1); 310 lua_pop(L, 1); /* remove result from gettable */
308 plib = (void **)lua_newuserdata(L, sizeof(const void *)); 311 plib = (void **)lua_newuserdata(L, sizeof(const void *));
309 *plib = NULL; 312 *plib = NULL;
310 luaL_getmetatable(L, "_LOADLIB"); 313 luaL_getmetatable(L, "_LOADLIB");
@@ -332,18 +335,17 @@ static int gctm (lua_State *L) {
332static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { 335static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
333 void **reg = ll_register(L, path); 336 void **reg = ll_register(L, path);
334 if (*reg == NULL) *reg = ll_load(L, path, *sym == '*'); 337 if (*reg == NULL) *reg = ll_load(L, path, *sym == '*');
335 if (*reg == NULL) 338 if (*reg == NULL) return ERRLIB; /* unable to load library */
336 return ERRLIB; /* unable to load library */ 339 if (*sym == '*') { /* loading only library (no function)? */
337 else if (*sym == '*') { /* loading only library (no function)? */
338 lua_pushboolean(L, 1); /* return 'true' */ 340 lua_pushboolean(L, 1); /* return 'true' */
339 return 0; 341 return 0; /* no errors */
340 } 342 }
341 else { 343 else {
342 lua_CFunction f = ll_sym(L, *reg, sym); 344 lua_CFunction f = ll_sym(L, *reg, sym);
343 if (f == NULL) 345 if (f == NULL)
344 return ERRFUNC; /* unable to find function */ 346 return ERRFUNC; /* unable to find function */
345 lua_pushcfunction(L, f); 347 lua_pushcfunction(L, f); /* else return function */
346 return 0; /* return function */ 348 return 0; /* no errors */
347 } 349 }
348} 350}
349 351
@@ -447,31 +449,28 @@ static int loader_Lua (lua_State *L) {
447} 449}
448 450
449 451
450static const char *mkfuncname (lua_State *L, const char *modname) { 452static int loadfunc(lua_State *L, const char *filename, const char *modname) {
451 const char *funcname; 453 const char *funcname;
452 const char *mark = strchr(modname, *LUA_IGMARK); 454 const char *mark = strchr(modname, *LUA_IGMARK);
453 if (mark) modname = mark + 1; 455 if (mark) modname = mark + 1;
454 funcname = luaL_gsub(L, modname, ".", LUA_OFSEP); 456 funcname = luaL_gsub(L, modname, ".", LUA_OFSEP);
455 funcname = lua_pushfstring(L, POF"%s", funcname); 457 funcname = lua_pushfstring(L, POF"%s", funcname);
456 lua_remove(L, -2); /* remove 'gsub' result */ 458 lua_remove(L, -2); /* remove 'gsub' result */
457 return funcname; 459 return ll_loadfunc(L, filename, funcname);
458} 460}
459 461
460 462
461static int loader_C (lua_State *L) { 463static int loader_C (lua_State *L) {
462 const char *funcname;
463 const char *name = luaL_checkstring(L, 1); 464 const char *name = luaL_checkstring(L, 1);
464 const char *filename = findfile(L, name, "cpath"); 465 const char *filename = findfile(L, name, "cpath");
465 if (filename == NULL) return 1; /* library not found in this path */ 466 if (filename == NULL) return 1; /* library not found in this path */
466 funcname = mkfuncname(L, name); 467 if (loadfunc(L, filename, name) != 0)
467 if (ll_loadfunc(L, filename, funcname) != 0)
468 loaderror(L, filename); 468 loaderror(L, filename);
469 return 1; /* library loaded successfully */ 469 return 1; /* library loaded successfully */
470} 470}
471 471
472 472
473static int loader_Croot (lua_State *L) { 473static int loader_Croot (lua_State *L) {
474 const char *funcname;
475 const char *filename; 474 const char *filename;
476 const char *name = luaL_checkstring(L, 1); 475 const char *name = luaL_checkstring(L, 1);
477 const char *p = strchr(name, '.'); 476 const char *p = strchr(name, '.');
@@ -480,8 +479,7 @@ static int loader_Croot (lua_State *L) {
480 lua_pushlstring(L, name, p - name); 479 lua_pushlstring(L, name, p - name);
481 filename = findfile(L, lua_tostring(L, -1), "cpath"); 480 filename = findfile(L, lua_tostring(L, -1), "cpath");
482 if (filename == NULL) return 1; /* root not found */ 481 if (filename == NULL) return 1; /* root not found */
483 funcname = mkfuncname(L, name); 482 if ((stat = loadfunc(L, filename, name)) != 0) {
484 if ((stat = ll_loadfunc(L, filename, funcname)) != 0) {
485 if (stat != ERRFUNC) loaderror(L, filename); /* real error */ 483 if (stat != ERRFUNC) loaderror(L, filename); /* real error */
486 lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, 484 lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
487 name, filename); 485 name, filename);