From 07e210e6555b9930edc2f2fb21220f6f6769f110 Mon Sep 17 00:00:00 2001
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Mon, 24 Feb 2003 13:50:41 -0300
Subject: `require' returns value returned by package

---
 lbaselib.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

(limited to 'lbaselib.c')

diff --git a/lbaselib.c b/lbaselib.c
index 7c18548d..dac9fb23 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lbaselib.c,v 1.120 2003/02/18 16:02:13 roberto Exp roberto $
+** $Id: lbaselib.c,v 1.121 2003/02/18 16:13:15 roberto Exp roberto $
 ** Basic library
 ** See Copyright Notice in lua.h
 */
@@ -454,8 +454,8 @@ static int luaB_require (lua_State *L) {
   path = getpath(L);
   lua_pushvalue(L, 1);  /* check package's name in book-keeping table */
   lua_rawget(L, 2);
-  if (!lua_isnil(L, -1))  /* is it there? */
-    return 0;  /* package is already loaded */
+  if (lua_toboolean(L, -1))  /* is it there? */
+    return 1;  /* package is already loaded; return its result */
   else {  /* must load it */
     while (status == LUA_ERRFILE) {
       lua_settop(L, 3);  /* reset stack position */
@@ -470,12 +470,17 @@ static int luaB_require (lua_State *L) {
       lua_insert(L, -2);  /* put it below function */
       lua_pushvalue(L, 1);
       lua_setglobal(L, "_REQUIREDNAME");  /* set new name */
-      lua_call(L, 0, 0);  /* run loaded module */
+      lua_call(L, 0, 1);  /* run loaded module */
+      lua_insert(L, -2);  /* put result below previous name */
       lua_setglobal(L, "_REQUIREDNAME");  /* reset to previous name */
+      if (lua_isnil(L, -1)) {  /* no/nil return? */
+        lua_pushboolean(L, 1);
+        lua_replace(L, -2);  /* replace to true */
+      }
       lua_pushvalue(L, 1);
-      lua_pushboolean(L, 1);
+      lua_pushvalue(L, -2);
       lua_rawset(L, 2);  /* mark it as loaded */
-      return 0;
+      return 1;  /* return value */
     }
     case LUA_ERRFILE: {  /* file not found */
       return luaL_error(L, "could not load package `%s' from path `%s'",
-- 
cgit v1.2.3-55-g6feb