diff src/luan/modules/PackageLuan.java @ 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 ea1768c00d03
line wrap: on
line diff
--- 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;
 			}
 		}
 	}