changeset 75:aa7538ae5fb6

can now load modules from classpath git-svn-id: https://luan-java.googlecode.com/svn/trunk@76 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 13 Feb 2013 09:19:15 +0000
parents f003338d503b
children 97b03fc807ad
files src/luan/LuanState.java src/luan/lib/BasicLib.java src/luan/lib/PackageLib.java src/luan/lib/Utils.java
diffstat 4 files changed, 126 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/LuanState.java	Wed Feb 13 06:27:56 2013 +0000
+++ b/src/luan/LuanState.java	Wed Feb 13 09:19:15 2013 +0000
@@ -28,6 +28,30 @@
 	final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
 
 
+	public Object get(String name) {
+		String[] a = name.split("\\.");
+		LuanTable t = global;
+		for( int i=0; i<a.length-1; i++ ) {
+			Object obj = t.get(a[i]);
+			if( !(obj instanceof LuanTable) )
+				return null;
+			t = (LuanTable)obj;
+		}
+		return t.get(a[a.length-1]);
+	}
+
+	public Object set(String name,Object value) {
+		String[] a = name.split("\\.");
+		LuanTable t = global;
+		for( int i=0; i<a.length-1; i++ ) {
+			Object obj = t.get(a[i]);
+			if( !(obj instanceof LuanTable) )
+				return null;
+			t = (LuanTable)obj;
+		}
+		return t.put(a[a.length-1],value);
+	}
+
 	public void load(LuanFunction loader,String modName) throws LuanException {
 		Object mod = Luan.first(call(loader,LuanElement.JAVA,"loader",modName));
 		if( mod == null )
--- a/src/luan/lib/BasicLib.java	Wed Feb 13 06:27:56 2013 +0000
+++ b/src/luan/lib/BasicLib.java	Wed Feb 13 09:19:15 2013 +0000
@@ -1,8 +1,6 @@
 package luan.lib;
 
 import java.io.File;
-import java.io.Reader;
-import java.io.FileReader;
 import java.io.InputStreamReader;
 import java.io.IOException;
 import java.lang.reflect.Method;
@@ -95,31 +93,10 @@
 		return LuanCompiler.compile(luan,new LuanSource(sourceName,text));
 	}
 
-	public static String readAll(Reader in)
-		throws IOException
-	{
-		char[] a = new char[8192];
-		StringBuilder buf = new StringBuilder();
-		int n;
-		while( (n=in.read(a)) != -1 ) {
-			buf.append(a,0,n);
-		}
-		return buf.toString();
-	}
-
-	public static String read(File file)
-		throws IOException
-	{
-		Reader in = new FileReader(file);
-		String s = readAll(in);
-		in.close();
-		return s;
-	}
-
 
 	public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException {
 		try {
-			String src = fileName==null ? readAll(new InputStreamReader(System.in)) : read(new File(fileName));
+			String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : Utils.read(new File(fileName));
 			return load(luan,src,fileName);
 		} catch(IOException e) {
 			throw new LuanException(luan,LuanElement.JAVA,e);
--- a/src/luan/lib/PackageLib.java	Wed Feb 13 06:27:56 2013 +0000
+++ b/src/luan/lib/PackageLib.java	Wed Feb 13 09:19:15 2013 +0000
@@ -1,8 +1,9 @@
 package luan.lib;
 
 import java.io.File;
-import java.util.List;
-import java.util.ArrayList;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Arrays;
 import luan.Luan;
 import luan.LuanState;
 import luan.LuanTable;
@@ -20,20 +21,17 @@
 		public Object[] call(LuanState luan,Object[] args) throws LuanException {
 			LuanTable global = luan.global;
 			LuanTable module = new LuanTable();
-			List<Object> searchers = new ArrayList<Object>();
 			module.put("loaded",luan.loaded);
 			module.put("preload",luan.preload);
-			module.put("path","?.lua");
+//			module.put("path","?.lua");
 			try {
 				add( global, "require", LuanState.class, String.class );
 				add( module, "module", LuanState.class, String.class );
 				add( module, "search_path", String.class, String.class );
-				searchers.add( new LuanJavaFunction(PackageLib.class.getMethod("preloadSearcher",LuanState.class,String.class),null) );
-				searchers.add( new LuanJavaFunction(PackageLib.class.getMethod("fileSearcher",LuanState.class,String.class),null) );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
 			}
-			module.put("searchers",new LuanTable(searchers));
+			module.put("searchers",new LuanTable(Arrays.<Object>asList(preloadSearcher,fileSearcher,javaFileSearcher)));
 			return new Object[]{module};
 		}
 	};
@@ -49,10 +47,9 @@
 	}
 
 	public static Object module(LuanState luan,String modName) throws LuanException {
-		LuanTable module = (LuanTable)luan.global.get(NAME);
 		Object mod = luan.loaded.get(modName);
 		if( mod == null ) {
-			LuanTable searchers = (LuanTable)module.get("searchers");
+			LuanTable searchers = (LuanTable)luan.get("package.searchers");
 			for( Object s : searchers.asList() ) {
 				LuanFunction searcher = (LuanFunction)s;
 				Object[] a = luan.call(searcher,LuanElement.JAVA,"searcher",modName);
@@ -89,15 +86,56 @@
 		}
 	};
 
-	public static Object[] fileSearcher(LuanState luan,String modName) {
-		LuanTable module = (LuanTable)luan.global.get(NAME);
-		String path = (String)module.get("path");
-		String file = search_path(modName,path);
-		return file==null ? LuanFunction.EMPTY_RTN : new Object[]{fileLoader,file};
-	}
+	public static final LuanFunction fileSearcher = new LuanFunction() {
+		public Object[] call(LuanState luan,Object[] args) throws LuanException {
+			String modName = (String)args[0];
+			String path = (String)luan.get("package.path");
+			if( path==null )
+				return LuanFunction.EMPTY_RTN;
+			String file = search_path(modName,path);
+			return file==null ? LuanFunction.EMPTY_RTN : new Object[]{fileLoader,file};
+		}
+	};
+
+	public static final LuanFunction preloadSearcher = new LuanFunction() {
+		public Object[] call(LuanState luan,Object[] args) throws LuanException {
+			String modName = (String)args[0];
+			return new Object[]{luan.preload.get(modName)};
+		}
+	};
+
+
+
 
-	public static Object preloadSearcher(LuanState luan,String modName) {
-		return luan.preload.get(modName);
-	}
-		
+	private static final LuanFunction javaFileLoader = new LuanFunction() {
+		public Object[] call(LuanState luan,Object[] args) throws LuanException {
+			try {
+				String modName = (String)args[0];
+				URL url = (URL)args[1];
+				String src = Utils.read(url);
+				LuanFunction fn = BasicLib.load(luan,src,url.toString());
+				return luan.call(fn,LuanElement.JAVA,modName,args);
+			} catch(IOException e) {
+				throw new LuanException(luan,LuanElement.JAVA,e);
+			}
+		}
+	};
+
+	public static final LuanFunction javaFileSearcher = new LuanFunction() {
+		public Object[] call(LuanState luan,Object[] args) throws LuanException {
+			String modName = (String)args[0];
+			String path = (String)luan.get("package.jpath");
+			if( path==null )
+				return LuanFunction.EMPTY_RTN;
+			for( String s : path.split(";") ) {
+				String file = s.replaceAll("\\?",modName);
+				URL url = ClassLoader.getSystemResource(file);
+				if( url != null ) {
+					return new Object[]{javaFileLoader,url};
+				}
+			}
+			return LuanFunction.EMPTY_RTN;
+		}
+	};
+
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/lib/Utils.java	Wed Feb 13 09:19:15 2013 +0000
@@ -0,0 +1,44 @@
+package luan.lib;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.IOException;
+import java.net.URL;
+
+
+public final class Utils {
+	private Utils() {}  // never
+
+	public static String readAll(Reader in)
+		throws IOException
+	{
+		char[] a = new char[8192];
+		StringBuilder buf = new StringBuilder();
+		int n;
+		while( (n=in.read(a)) != -1 ) {
+			buf.append(a,0,n);
+		}
+		return buf.toString();
+	}
+
+	public static String read(File file)
+		throws IOException
+	{
+		Reader in = new FileReader(file);
+		String s = readAll(in);
+		in.close();
+		return s;
+	}
+
+	public static String read(URL url)
+		throws IOException
+	{
+		Reader in = new InputStreamReader(url.openStream());
+		String s = readAll(in);
+		in.close();
+		return s;
+	}
+
+}