diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-02-24 13:50:41 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-02-24 13:50:41 -0300 |
commit | 07e210e6555b9930edc2f2fb21220f6f6769f110 (patch) | |
tree | 0acd48637a772ddda5a5c89b51dc26e415ba9dd6 | |
parent | feb724c12232528045fcc0672407a643f8588685 (diff) | |
download | lua-07e210e6555b9930edc2f2fb21220f6f6769f110.tar.gz lua-07e210e6555b9930edc2f2fb21220f6f6769f110.tar.bz2 lua-07e210e6555b9930edc2f2fb21220f6f6769f110.zip |
`require' returns value returned by package
-rw-r--r-- | lbaselib.c | 17 |
1 files changed, 11 insertions, 6 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.120 2003/02/18 16:02:13 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.121 2003/02/18 16:13:15 roberto Exp roberto $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -454,8 +454,8 @@ static int luaB_require (lua_State *L) { | |||
454 | path = getpath(L); | 454 | path = getpath(L); |
455 | lua_pushvalue(L, 1); /* check package's name in book-keeping table */ | 455 | lua_pushvalue(L, 1); /* check package's name in book-keeping table */ |
456 | lua_rawget(L, 2); | 456 | lua_rawget(L, 2); |
457 | if (!lua_isnil(L, -1)) /* is it there? */ | 457 | if (lua_toboolean(L, -1)) /* is it there? */ |
458 | return 0; /* package is already loaded */ | 458 | return 1; /* package is already loaded; return its result */ |
459 | else { /* must load it */ | 459 | else { /* must load it */ |
460 | while (status == LUA_ERRFILE) { | 460 | while (status == LUA_ERRFILE) { |
461 | lua_settop(L, 3); /* reset stack position */ | 461 | lua_settop(L, 3); /* reset stack position */ |
@@ -470,12 +470,17 @@ static int luaB_require (lua_State *L) { | |||
470 | lua_insert(L, -2); /* put it below function */ | 470 | lua_insert(L, -2); /* put it below function */ |
471 | lua_pushvalue(L, 1); | 471 | lua_pushvalue(L, 1); |
472 | lua_setglobal(L, "_REQUIREDNAME"); /* set new name */ | 472 | lua_setglobal(L, "_REQUIREDNAME"); /* set new name */ |
473 | lua_call(L, 0, 0); /* run loaded module */ | 473 | lua_call(L, 0, 1); /* run loaded module */ |
474 | lua_insert(L, -2); /* put result below previous name */ | ||
474 | lua_setglobal(L, "_REQUIREDNAME"); /* reset to previous name */ | 475 | lua_setglobal(L, "_REQUIREDNAME"); /* reset to previous name */ |
476 | if (lua_isnil(L, -1)) { /* no/nil return? */ | ||
477 | lua_pushboolean(L, 1); | ||
478 | lua_replace(L, -2); /* replace to true */ | ||
479 | } | ||
475 | lua_pushvalue(L, 1); | 480 | lua_pushvalue(L, 1); |
476 | lua_pushboolean(L, 1); | 481 | lua_pushvalue(L, -2); |
477 | lua_rawset(L, 2); /* mark it as loaded */ | 482 | lua_rawset(L, 2); /* mark it as loaded */ |
478 | return 0; | 483 | return 1; /* return value */ |
479 | } | 484 | } |
480 | case LUA_ERRFILE: { /* file not found */ | 485 | case LUA_ERRFILE: { /* file not found */ |
481 | return luaL_error(L, "could not load package `%s' from path `%s'", | 486 | return luaL_error(L, "could not load package `%s' from path `%s'", |