changeset 799:9c13a15a4002

Package.load() now returns false instead of null, and caches the result. This cleans up the luan.isLocked issues.
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 06 Sep 2016 18:03:19 -0600
parents 9eccfede1094
children e2a446c5c7c4
files src/luan/modules/IoLuan.java src/luan/modules/PackageLuan.java src/luan/modules/http/HttpServicer.java src/luan/modules/http/Http_test.luan src/luan/modules/http/Server.luan website/src/manual.html.luan
diffstat 6 files changed, 26 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
diff -r 9eccfede1094 -r 9c13a15a4002 src/luan/modules/IoLuan.java
--- a/src/luan/modules/IoLuan.java	Tue Sep 06 12:23:03 2016 -0600
+++ b/src/luan/modules/IoLuan.java	Tue Sep 06 18:03:19 2016 -0600
@@ -685,13 +685,7 @@
 		LuanFunction opener = (LuanFunction)schemes.get(luan,scheme);
 		if( opener == null )
 			throw new LuanException( "invalid scheme '"+scheme+"' in '"+name+"'" );
-		boolean isLocked = luan.isLocked;  // hack needed for luan scheme functions
-		luan.isLocked = false;
-		try {
-			return (LuanTable)Luan.first(opener.call(luan,new Object[]{location,options}));
-		} finally {
-			luan.isLocked = isLocked;
-		}
+		return (LuanTable)Luan.first(opener.call(luan,new Object[]{location,options}));
 	}
 
 	public static final class LuanSocket extends LuanIO {
diff -r 9eccfede1094 -r 9c13a15a4002 src/luan/modules/PackageLuan.java
--- a/src/luan/modules/PackageLuan.java	Tue Sep 06 12:23:03 2016 -0600
+++ b/src/luan/modules/PackageLuan.java	Tue Sep 06 18:03:19 2016 -0600
@@ -34,7 +34,7 @@
 
 	public static Object require(LuanState luan,String modName) throws LuanException {
 		Object mod = load(luan,modName);
-		if( mod==null )
+		if( mod.equals(Boolean.FALSE) )
 			throw new LuanException( "module '"+modName+"' not found" );
 		return mod;
 	}
@@ -47,17 +47,19 @@
 				mod = JavaLuan.load(luan,modName.substring(5));
 			} else {
 				String src = read(luan,modName);
-				if( src == null )
-					return null;
-				LuanFunction loader = Luan.load(src,modName);
-				mod = Luan.first(
-					loader.call(luan,new Object[]{modName})
-				);
-				if( mod == null ) {
-					mod = loaded.rawGet(modName);
-					if( mod != null )
-						return mod;
-					throw new LuanException( "module '"+modName+"' returned nil" );
+				if( src == null ) {
+					mod = Boolean.FALSE;
+				} else {
+					LuanFunction loader = Luan.load(src,modName);
+					mod = Luan.first(
+						loader.call(luan,new Object[]{modName})
+					);
+					if( mod == null ) {
+						mod = loaded.rawGet(modName);
+						if( mod != null )
+							return mod;
+						throw new LuanException( "module '"+modName+"' returned nil" );
+					}
 				}
 			}
 			loaded.rawPut(modName,mod);
@@ -87,31 +89,15 @@
 		}
 	}
 
-	private static boolean wouldLoad(LuanState luan,String modName) throws LuanException {
+	public static void enableLoad(LuanState luan,String... mods) throws LuanException {
+		if( !luan.isLocked )
+			return;
 		LuanTable loaded = loaded(luan);
-		if( loaded.rawGet(modName) != null )
-			return false;
-		if( modName.startsWith("java:") )
-			return true;
-		LuanTable t = IoLuan.uri(luan,modName,null);
-		if( t == null )
-			return false;
-		IoLuan.LuanIn in = (IoLuan.LuanIn)t.rawGet("java");
-		try {
-			return in.exists();
-		} catch(IOException e) {
-			throw new LuanException(e);
-		}
-	}
-
-	public static void enableLoad(LuanState luan,String... mods) throws LuanException {
-		if( luan.isLocked ) {
-			for( String mod : mods ) {
-				if( wouldLoad(luan,mod) ) {
-					luan.isLocked = false;
-					luan.deepenClone(luan,new LuanCloner(LuanCloner.Type.COMPLETE));
-					return;
-				}
+		for( String mod : mods ) {
+			if( loaded.rawGet(mod) == null ) {
+				luan.isLocked = false;
+				luan.deepenClone(luan,new LuanCloner(LuanCloner.Type.COMPLETE));
+				break;
 			}
 		}
 	}
diff -r 9eccfede1094 -r 9c13a15a4002 src/luan/modules/http/HttpServicer.java
--- a/src/luan/modules/http/HttpServicer.java	Tue Sep 06 12:23:03 2016 -0600
+++ b/src/luan/modules/http/HttpServicer.java	Tue Sep 06 18:03:19 2016 -0600
@@ -49,7 +49,7 @@
 			LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http.luan");
 			LuanTable per_session_pages = (LuanTable)module.rawGet("per_session_pages");
 			Object mod = PackageLuan.load(luan,modName);
-			if( mod==null )
+			if( mod.equals(Boolean.FALSE) )
 				return false;
 			if( !(mod instanceof LuanFunction) )
 				throw new LuanException( "module '"+modName+"' must return a function" );
diff -r 9eccfede1094 -r 9c13a15a4002 src/luan/modules/http/Http_test.luan
--- a/src/luan/modules/http/Http_test.luan	Tue Sep 06 12:23:03 2016 -0600
+++ b/src/luan/modules/http/Http_test.luan	Tue Sep 06 18:03:19 2016 -0600
@@ -23,7 +23,7 @@
 	try {
 		function()
 			local mod = Package.load("site:"..path..".luan")
-			if mod ~= nil then
+			if mod and true then
 				mod()
 			else
 				local not_found = Package.load("site:/not_found.luan")
diff -r 9eccfede1094 -r 9c13a15a4002 src/luan/modules/http/Server.luan
--- a/src/luan/modules/http/Server.luan	Tue Sep 06 12:23:03 2016 -0600
+++ b/src/luan/modules/http/Server.luan	Tue Sep 06 18:03:19 2016 -0600
@@ -94,7 +94,6 @@
 	M.handlers.addHandler(NotFound.new(M.luan_handler))
 	M.server = Server.new(M.port)
 	M.server.setHandler(hc)
---	Package.load("site:/init.luan")
 end
 
 function M.start()
diff -r 9eccfede1094 -r 9c13a15a4002 website/src/manual.html.luan
--- a/website/src/manual.html.luan	Tue Sep 06 12:23:03 2016 -0600
+++ b/website/src/manual.html.luan	Tue Sep 06 18:03:19 2016 -0600
@@ -2268,7 +2268,7 @@
 To load a new value, <code>Package.load</code> first checks if <code>mod_uri</code> starts with "<b>java:</b>".  If yes, then this is a Java class which is loaded by special Java code.
 
 <p>
-If <code>mod_uri</code> is not a Java class, then <code>Package.load</code> tries to read the text of the file referred to by <code>mod_uri</code>.  If the file doesn't exist, then <code>Package.load</code> returns <b>nil</b>.  If the file exists, then its content is compiled into a chunk calling <a href="#Luan.load"><code>Luan.load</code></a> and passing in an empty table as the <code>env</code> value.  This chunk is run passing in <code>mod_uri</code> as an argument.  If the chunk returns a value other than <b>nil</b>, then that value is the value of this module.  Otherwise the <code>env</code> that was passed in is the value of this module.
+Otherwise <code>Package.load</code> tries to read the text of the file referred to by <code>mod_uri</code>.  If the file doesn't exist, then <code>Package.load</code> returns <b>false</b>.  If the file exists, then its content is compiled into a chunk by calling <a href="#Luan.load"><code>Luan.load</code></a>.  This chunk is run passing in <code>mod_uri</code> as an argument.  The value returned by the chunk must not be <b>nil</b> and is loaded.
 
 <p>
 If a new value for the module successful loaded, then it is stored in <code>Package.loaded[mod_uri]</code>.  The value is returned.