aboutsummaryrefslogtreecommitdiff
path: root/loadlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-03-01 14:01:53 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-03-01 14:01:53 -0300
commite049abb69a45e06ca5769a3956a0ba36a959d29e (patch)
tree6071757e921764d1823135fa8292151d0c9beaf4 /loadlib.c
parent98816d0ce58db9e817129b076723187d690501df (diff)
downloadlua-e049abb69a45e06ca5769a3956a0ba36a959d29e.tar.gz
lua-e049abb69a45e06ca5769a3956a0ba36a959d29e.tar.bz2
lua-e049abb69a45e06ca5769a3956a0ba36a959d29e.zip
loaders receive an extra argument returned by the searcher
(typically the file name)
Diffstat (limited to 'loadlib.c')
-rw-r--r--loadlib.c55
1 files changed, 32 insertions, 23 deletions
diff --git a/loadlib.c b/loadlib.c
index 2e5e31a6..e90b5355 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.95 2011/01/07 18:54:49 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.96 2011/02/07 19:15:24 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**
@@ -364,9 +364,15 @@ static const char *findfile (lua_State *L, const char *name,
364} 364}
365 365
366 366
367static void loaderror (lua_State *L, const char *filename) { 367static int checkload (lua_State *L, int stat, const char *filename) {
368 luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", 368 if (stat) { /* module loaded successfully? */
369 lua_tostring(L, 1), filename, lua_tostring(L, -1)); 369 lua_pushstring(L, filename); /* will be 2nd argument to module */
370 return 2; /* return open function and file name */
371 }
372 else
373 return luaL_error(L, "error loading module " LUA_QS
374 " from file " LUA_QS ":\n\t%s",
375 lua_tostring(L, 1), filename, lua_tostring(L, -1));
370} 376}
371 377
372 378
@@ -374,10 +380,8 @@ static int loader_Lua (lua_State *L) {
374 const char *filename; 380 const char *filename;
375 const char *name = luaL_checkstring(L, 1); 381 const char *name = luaL_checkstring(L, 1);
376 filename = findfile(L, name, "path"); 382 filename = findfile(L, name, "path");
377 if (filename == NULL) return 1; /* library not found in this path */ 383 if (filename == NULL) return 1; /* module not found in this path */
378 if (luaL_loadfile(L, filename) != LUA_OK) 384 return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
379 loaderror(L, filename);
380 return 1; /* library loaded successfully */
381} 385}
382 386
383 387
@@ -402,10 +406,8 @@ static int loadfunc (lua_State *L, const char *filename, const char *modname) {
402static int loader_C (lua_State *L) { 406static int loader_C (lua_State *L) {
403 const char *name = luaL_checkstring(L, 1); 407 const char *name = luaL_checkstring(L, 1);
404 const char *filename = findfile(L, name, "cpath"); 408 const char *filename = findfile(L, name, "cpath");
405 if (filename == NULL) return 1; /* library not found in this path */ 409 if (filename == NULL) return 1; /* module not found in this path */
406 if (loadfunc(L, filename, name) != 0) 410 return checkload(L, (loadfunc(L, filename, name) == 0), filename);
407 loaderror(L, filename);
408 return 1; /* library loaded successfully */
409} 411}
410 412
411 413
@@ -419,12 +421,16 @@ static int loader_Croot (lua_State *L) {
419 filename = findfile(L, lua_tostring(L, -1), "cpath"); 421 filename = findfile(L, lua_tostring(L, -1), "cpath");
420 if (filename == NULL) return 1; /* root not found */ 422 if (filename == NULL) return 1; /* root not found */
421 if ((stat = loadfunc(L, filename, name)) != 0) { 423 if ((stat = loadfunc(L, filename, name)) != 0) {
422 if (stat != ERRFUNC) loaderror(L, filename); /* real error */ 424 if (stat != ERRFUNC)
423 lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, 425 return checkload(L, 0, filename); /* real error */
424 name, filename); 426 else { /* open function not found */
425 return 1; /* function not found */ 427 lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
428 name, filename);
429 return 1;
430 }
426 } 431 }
427 return 1; 432 lua_pushstring(L, filename); /* will be 2nd argument to module */
433 return 2;
428} 434}
429 435
430 436
@@ -457,16 +463,19 @@ static int ll_require (lua_State *L) {
457 luaL_error(L, "module " LUA_QS " not found:%s", 463 luaL_error(L, "module " LUA_QS " not found:%s",
458 name, lua_tostring(L, -2)); 464 name, lua_tostring(L, -2));
459 lua_pushstring(L, name); 465 lua_pushstring(L, name);
460 lua_call(L, 1, 1); /* call it */ 466 lua_call(L, 1, 2); /* call it */
461 if (lua_isfunction(L, -1)) /* did it find module? */ 467 if (lua_isfunction(L, -2)) /* did it find module? */
462 break; /* module loaded successfully */ 468 break; /* module loaded successfully */
463 else if (lua_isstring(L, -1)) /* loader returned error message? */ 469 else if (lua_isstring(L, -2)) { /* loader returned error message? */
464 lua_concat(L, 2); /* accumulate it */ 470 lua_pop(L, 1); /* remove extra return */
471 lua_concat(L, 2); /* accumulate error message */
472 }
465 else 473 else
466 lua_pop(L, 1); 474 lua_pop(L, 2); /* remove both returns */
467 } 475 }
468 lua_pushstring(L, name); /* pass name as argument to module */ 476 lua_pushstring(L, name); /* pass name as argument to module */
469 lua_call(L, 1, 1); /* run loaded module */ 477 lua_insert(L, -2); /* name is 1st argument (before search data) */
478 lua_call(L, 2, 1); /* run loaded module */
470 if (!lua_isnil(L, -1)) /* non-nil return? */ 479 if (!lua_isnil(L, -1)) /* non-nil return? */
471 lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ 480 lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
472 lua_getfield(L, 2, name); 481 lua_getfield(L, 2, name);