changeset 299:a74559240b4f

simplify PackageLuan and remove IO loading param git-svn-id: https://luan-java.googlecode.com/svn/trunk@300 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 16 Dec 2014 06:24:49 +0000
parents 2ce23c7e2342
children 9fb523472035
files core/src/luan/LuanState.java core/src/luan/modules/BasicLuan.java core/src/luan/modules/IoLuan.java core/src/luan/modules/JavaLuan.java core/src/luan/modules/PackageLuan.java web/src/luan/modules/web/Web_server.luan
diffstat 6 files changed, 53 insertions(+), 105 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/LuanState.java	Tue Dec 16 03:51:56 2014 +0000
+++ b/core/src/luan/LuanState.java	Tue Dec 16 06:24:49 2014 +0000
@@ -44,15 +44,6 @@
 		return registry;
 	}
 
-	public final LuanTable registryTable(Object key) {
-		LuanTable tbl = (LuanTable)registry.get(key);
-		if( tbl == null ) {
-			tbl = new LuanTableImpl();
-			registry.put(key,tbl);
-		}
-		return tbl;
-	}
-
 	public final LuanTable global() {
 		return global;
 	}
--- a/core/src/luan/modules/BasicLuan.java	Tue Dec 16 03:51:56 2014 +0000
+++ b/core/src/luan/modules/BasicLuan.java	Tue Dec 16 06:24:49 2014 +0000
@@ -35,10 +35,10 @@
 	public static LuanFunction load_file(LuanState luan,String fileName,LuanTable env) throws LuanException {
 		if( fileName == null )
 			fileName = "stdin:";
-		LuanFunction fn = PackageLuan.loader(luan,fileName,false,env);
-		if( fn == null )
+		String src = PackageLuan.read(luan,fileName);
+		if( src == null )
 			throw luan.exception( "file '"+fileName+"' not found" );
-		return fn;
+		return load(luan,src,fileName,env,false);
 	}
 
 	public static Object do_file(LuanState luan,String fileName) throws LuanException {
--- a/core/src/luan/modules/IoLuan.java	Tue Dec 16 03:51:56 2014 +0000
+++ b/core/src/luan/modules/IoLuan.java	Tue Dec 16 06:24:49 2014 +0000
@@ -188,17 +188,6 @@
 			return blocks(inputStream(),n);
 		}
 
-		public LuanFunction loader(LuanState luan,String name,LuanTable env) throws LuanException {
-			try {
-				String src = read_text();
-				return BasicLuan.load(luan,src,name,env,false);
-			} catch(FileNotFoundException e) {
-				return null;
-			} catch(IOException e) {
-				throw luan.exception(e);
-			}
-		}
-
 		public boolean exists() throws IOException {
 			try {
 				inputStream().close();
@@ -226,9 +215,6 @@
 				tbl.put( "read_blocks", new LuanJavaFunction(
 					LuanIn.class.getMethod( "read_blocks", Integer.class ), this
 				) );
-				tbl.put( "loader", new LuanJavaFunction(
-					LuanIn.class.getMethod( "loader", LuanState.class, String.class, LuanTable.class ), this
-				) );
 				tbl.put( "exists", new LuanJavaFunction(
 					LuanIn.class.getMethod( "exists" ), this
 				) );
@@ -429,20 +415,15 @@
 		}
 	}
 
-	public static LuanTable file(LuanState luan,String name,Boolean loading) throws LuanException {
-		if( Boolean.TRUE.equals(loading) )
-			name += ".luan";
+	public static LuanTable file(LuanState luan,String name) throws LuanException {
 		File file = new File(name);
 		return new LuanFile(file).table();
 	}
 
-	public static LuanTable classpath(LuanState luan,String name,Boolean loading) throws LuanException {
+	public static LuanTable classpath(LuanState luan,String name) throws LuanException {
 		if( name.contains("//") )
 			return null;
 		String path = name;
-		boolean isLoading = Boolean.TRUE.equals(loading);
-		if( isLoading )
-			path += ".luan";
 		check(luan,"classpath",path);
 		URL url;
 		if( !path.contains("#") ) {
@@ -470,38 +451,20 @@
 		return null;
 	}
 
-	private static LuanTable url(String url,Boolean loading) throws IOException {
-		if( Boolean.TRUE.equals(loading) )
-			url += ".luan";
+	private static LuanTable url(String url) throws IOException {
 		return new LuanUrl(new URL(url)).table();
 	}
 
-	public static LuanTable http(String path,Boolean loading) throws IOException {
-		return url("http:"+path,loading);
-	}
-
-	public static LuanTable https(String path,Boolean loading) throws IOException {
-		return url("https:"+path,loading);
+	public static LuanTable http(String path) throws IOException {
+		return url("http:"+path);
 	}
 
-	public static LuanTable java(LuanState luan,String path,Boolean loading) throws LuanException {
-		if( !Boolean.TRUE.equals(loading) )
-			return null;
-		final LuanFunction fn = JavaLuan.javaLoader(luan,path);
-		if( fn==null )
-			return null;
-		LuanFunction loader = new LuanFunction() {
-			@Override public Object call(LuanState luan,Object[] args) {
-				return fn;
-			}
-		};
-		LuanTable tbl = Luan.newTable();
-		tbl.put( "loader", loader );
-		return tbl;
+	public static LuanTable https(String path) throws IOException {
+		return url("https:"+path);
 	}
 
-	public static LuanTable luan(LuanState luan,String path,Boolean loading) throws LuanException {
-		return classpath( luan, "luan/modules/" + path, loading );
+	public static LuanTable luan(LuanState luan,String path) throws LuanException {
+		return classpath( luan, "luan/modules/" + path );
 	}
 
 	public static LuanTable stdin(LuanState luan) throws LuanException {
@@ -512,13 +475,12 @@
 	public static LuanTable newSchemes() {
 		LuanTable schemes = Luan.newTable();
 		try {
-			add( schemes, "file", LuanState.class, String.class, Boolean.class );
-			add( schemes, "classpath", LuanState.class, String.class, Boolean.class );
+			add( schemes, "file", LuanState.class, String.class );
+			add( schemes, "classpath", LuanState.class, String.class );
 			add( schemes, "socket", LuanState.class, String.class );
-			add( schemes, "http", String.class, Boolean.class );
-			add( schemes, "https", String.class, Boolean.class );
-			add( schemes, "java", LuanState.class, String.class, Boolean.class );
-			add( schemes, "luan", LuanState.class, String.class, Boolean.class );
+			add( schemes, "http", String.class );
+			add( schemes, "https", String.class );
+			add( schemes, "luan", LuanState.class, String.class );
 			add( schemes, "stdin", LuanState.class );
 		} catch(NoSuchMethodException e) {
 			throw new RuntimeException(e);
@@ -536,7 +498,7 @@
 		return t;
 	}
 
-	public static LuanTable Uri(LuanState luan,String name,Boolean loading) throws LuanException {
+	public static LuanTable Uri(LuanState luan,String name) throws LuanException {
 		int i = name.indexOf(':');
 		if( i == -1 )
 			throw luan.exception( "invalid Io name '"+name+"', missing scheme" );
@@ -546,7 +508,7 @@
 		LuanFunction opener = (LuanFunction)schemes.get(scheme);
 		if( opener == null )
 			throw luan.exception( "invalid scheme '"+scheme+"' in '"+name+"'" );
-		return (LuanTable)Luan.first(luan.call(opener,"<open \""+name+"\">",new Object[]{location,loading}));
+		return (LuanTable)Luan.first(luan.call(opener,"<open \""+name+"\">",new Object[]{location}));
 	}
 
 	public static final class LuanSocket extends LuanIO {
--- a/core/src/luan/modules/JavaLuan.java	Tue Dec 16 03:51:56 2014 +0000
+++ b/core/src/luan/modules/JavaLuan.java	Tue Dec 16 06:24:49 2014 +0000
@@ -32,20 +32,6 @@
 		return true;
 	}
 
-	static LuanFunction javaLoader(LuanState luan,String modName) throws LuanException {
-		if( !isLoaded(luan) )
-			return null;
-		final Static s = JavaLuan.getClass(luan,modName);
-		if( s==null )
-			return null;
-		LuanFunction loader = new LuanFunction() {
-			@Override public Object call(LuanState luan,Object[] args) {
-				return s;
-			}
-		};
-		return loader;
-	}
-
 	public static Object __index(LuanState luan,Object obj,Object key) throws LuanException {
 		if( obj instanceof Static ) {
 			if( key instanceof String ) {
@@ -304,7 +290,22 @@
 		}
 	}
 
-	public static Static getClass(LuanState luan,String name) throws LuanException {
+	public static Static load(LuanState luan,String name) throws LuanException {
+		@SuppressWarnings("unchecked")
+		Map<String,Static> loaded = (Map<String,Static>)luan.registry().get("Java.loaded");
+		if( loaded == null ) {
+			loaded = new HashMap<String,Static>();
+			luan.registry().put("Java.loaded",loaded);
+		}
+		Static s = loaded.get(name);
+		if( s == null ) {
+			s = getClassStatic(luan,name);
+			loaded.put(name,s);
+		}
+		return s;
+	}
+
+	private static Static getClassStatic(LuanState luan,String name) throws LuanException {
 		Class cls;
 		try {
 			cls = Class.forName(name);
--- a/core/src/luan/modules/PackageLuan.java	Tue Dec 16 03:51:56 2014 +0000
+++ b/core/src/luan/modules/PackageLuan.java	Tue Dec 16 06:24:49 2014 +0000
@@ -25,12 +25,12 @@
 	}
 
 	public static LuanTable loaded(LuanState luan) {
-		return luan.registryTable("Package.loaded");
-	}
-
-	private static Object pkg(LuanState luan,String key) {
-		LuanTable t = (LuanTable)loaded(luan).get("Package");
-		return t==null ? null : t.get(key);
+		LuanTable tbl = (LuanTable)luan.registry().get("Package.loaded");
+		if( tbl == null ) {
+			tbl = Luan.newTable();
+			luan.registry().put("Package.loaded",tbl);
+		}
+		return tbl;
 	}
 
 	public static Object require(LuanState luan,String modName) throws LuanException {
@@ -41,15 +41,16 @@
 	}
 
 	public static Object load(LuanState luan,String modName) throws LuanException {
+		if( modName.startsWith("java:") )
+			return JavaLuan.load(luan,modName.substring(5));
 		LuanTable loaded = loaded(luan);
 		Object mod = loaded.get(modName);
 		if( mod == null ) {
-			Object[] a = search(luan,modName);
-			if( a == null )
+			String src = read(luan,modName+".luan");
+			if( src == null )
 				return null;
-			LuanFunction loader = (LuanFunction)a[0];
-			a[0] = modName;
-			mod = Luan.first(luan.call(loader,"<require \""+modName+"\">",a));
+			LuanFunction loader = BasicLuan.load(luan,src,modName,null,false);
+			mod = Luan.first(luan.call(loader,"<require \""+modName+"\">",new Object[]{modName}));
 			if( mod != null ) {
 				loaded.put(modName,mod);
 			} else {
@@ -63,19 +64,12 @@
 		return mod;
 	}
 
-	static LuanFunction loader(LuanState luan,String name,boolean loading,LuanTable env) throws LuanException {
-		LuanTable t = IoLuan.Uri(luan,name,loading);
+	static String read(LuanState luan,String uri) throws LuanException {
+		LuanTable t = IoLuan.Uri(luan,uri);
 		if( t == null )
 			return null;
-		LuanFunction loader = (LuanFunction)t.get("loader");
-		if( loader == null )
-			return null;
-		return (LuanFunction)Luan.first(luan.call(loader,new Object[]{name,env}));
-	}
-
-	public static Object[] search(LuanState luan,String modName) throws LuanException {
-		LuanFunction fn = loader(luan,modName,true,null);
-		return fn==null ? null : new Object[]{fn,modName};
+		LuanFunction reader = (LuanFunction)t.get("read_text");
+		return (String)Luan.first(luan.call(reader));
 	}
 
 }
--- a/web/src/luan/modules/web/Web_server.luan	Tue Dec 16 03:51:56 2014 +0000
+++ b/web/src/luan/modules/web/Web_server.luan	Tue Dec 16 06:24:49 2014 +0000
@@ -73,8 +73,8 @@
 function init(dir)
 	dir = dir.gsub("/$","")  -- remove trailing '/' if any
 	Http.dir = dir
-	function Io.schemes.site(path,loading)
-		return Io.Uri( dir..path, loading )
+	function Io.schemes.site(path)
+		return Io.Uri( dir..path )
 	end
 	authentication_handler.setPassword(private_password)
 	local base = dir