changeset 202:75750ceb45ee

add LuanState.registry git-svn-id: https://luan-java.googlecode.com/svn/trunk@203 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 04 Jul 2014 17:18:39 +0000
parents 27abb3746917
children 99eef1d0e706
files core/src/luan/LuanState.java core/src/luan/LuanTable.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 core/src/luan/modules/PickleClient.java core/src/luan/modules/PickleCon.java core/src/luan/modules/PickleServer.java core/src/luan/modules/StringLuan.java web/src/luan/modules/web/HttpLuan.java
diffstat 11 files changed, 99 insertions(+), 115 deletions(-) [+]
line wrap: on
line diff
diff -r 27abb3746917 -r 75750ceb45ee core/src/luan/LuanState.java
--- a/core/src/luan/LuanState.java	Thu Jul 03 22:42:44 2014 +0000
+++ b/core/src/luan/LuanState.java	Fri Jul 04 17:18:39 2014 +0000
@@ -17,72 +17,39 @@
 
 	final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
 
+	private LuanTable registry;
 	private LuanTable global;
-	private LuanTable loaded;
-	private LuanTable preload;
-	private LuanTable searchers;
-	public final Set<String> blocked;
 
 	protected LuanState() {
+		registry = new LuanTable();
 		global = new LuanTable();
 		global.put("_G",global);
-		loaded = new LuanTable();
-		preload = new LuanTable();
-		searchers = new LuanTable();
-		blocked = new HashSet<String>();
 	}
 
-	protected LuanState(LuanState luan) {
-		blocked = new HashSet<String>(luan.blocked);
-	}
+	protected LuanState(LuanState luan) {}
 
 	@Override public void deepenClone(LuanState clone,DeepCloner cloner) {
+		clone.registry = cloner.deepClone(registry);
 		clone.global = cloner.deepClone(global);
-		clone.loaded = cloner.deepClone(loaded);
-		clone.preload = cloner.deepClone(preload);
-		clone.searchers = cloner.deepClone(searchers);
 	}
 
 	public abstract LuanTable currentEnvironment();
 
-	public final LuanTable global() {
-		return global;
-	}
-
-	public final LuanTable loaded() {
-		return loaded;
-	}
-
-	public final LuanTable preload() {
-		return preload;
-	}
-
-	public final LuanTable searchers() {
-		return searchers;
+	public final LuanTable registry() {
+		return registry;
 	}
 
-	public final Object get(String name) {
-		String[] a = name.split("\\.");
-		LuanTable t = loaded;
-		for( int i=0; i<a.length-1; i++ ) {
-			Object obj = t.get(a[i]);
-			if( !(obj instanceof LuanTable) )
-				return null;
-			t = (LuanTable)obj;
+	public final LuanTable registryTable(Object key) {
+		LuanTable tbl = (LuanTable)registry.get(key);
+		if( tbl == null ) {
+			tbl = new LuanTable();
+			registry.put(key,tbl);
 		}
-		return t.get(a[a.length-1]);
+		return tbl;
 	}
 
-	public final Object set(String name,Object value) {
-		String[] a = name.split("\\.");
-		LuanTable t = loaded;
-		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 final LuanTable global() {
+		return global;
 	}
 
 	public static LuanState newStandard() {
diff -r 27abb3746917 -r 75750ceb45ee core/src/luan/LuanTable.java
--- a/core/src/luan/LuanTable.java	Thu Jul 03 22:42:44 2014 +0000
+++ b/core/src/luan/LuanTable.java	Fri Jul 04 17:18:39 2014 +0000
@@ -385,4 +385,8 @@
 			throw new UnsupportedOperationException();
 		}
 	}
+
+	public boolean isEmpty() {
+		return (list==null || list.isEmpty()) && (map==null || map.isEmpty());
+	}
 }
diff -r 27abb3746917 -r 75750ceb45ee core/src/luan/modules/BasicLuan.java
--- a/core/src/luan/modules/BasicLuan.java	Thu Jul 03 22:42:44 2014 +0000
+++ b/core/src/luan/modules/BasicLuan.java	Fri Jul 04 17:18:39 2014 +0000
@@ -1,6 +1,5 @@
 package luan.modules;
 
-import java.io.File;
 import java.io.InputStreamReader;
 import java.io.IOException;
 import java.lang.reflect.Method;
diff -r 27abb3746917 -r 75750ceb45ee core/src/luan/modules/IoLuan.java
--- a/core/src/luan/modules/IoLuan.java	Thu Jul 03 22:42:44 2014 +0000
+++ b/core/src/luan/modules/IoLuan.java	Fri Jul 04 17:18:39 2014 +0000
@@ -5,8 +5,6 @@
 import java.io.PrintStream;
 import java.io.Reader;
 import java.io.Writer;
-import java.io.FileReader;
-import java.io.FileWriter;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.BufferedInputStream;
@@ -313,7 +311,7 @@
 	public static final class LuanUrl extends LuanIn {
 		private final URL url;
 
-		public LuanUrl(String s) throws MalformedURLException {
+		private LuanUrl(String s) throws MalformedURLException {
 			this.url = new URL(s);
 		}
 
@@ -329,11 +327,11 @@
 	public static final class LuanFile extends LuanIO {
 		private final File file;
 
-		public LuanFile(String name) {
+		private LuanFile(String name) {
 			this(new File(name));
 		}
 
-		public LuanFile(File file) {
+		private LuanFile(File file) {
 			this.file = file;
 		}
 
@@ -429,11 +427,11 @@
 	public static final class LuanSocket extends LuanIO {
 		private final Socket socket;
 
-		public LuanSocket(String host,int port) throws IOException {
+		private LuanSocket(String host,int port) throws IOException {
 			this(new Socket(host,port));
 		}
 
-		public LuanSocket(Socket socket) throws IOException {
+		private LuanSocket(Socket socket) throws IOException {
 			this.socket = socket;
 		}
 
diff -r 27abb3746917 -r 75750ceb45ee core/src/luan/modules/JavaLuan.java
--- a/core/src/luan/modules/JavaLuan.java	Thu Jul 03 22:42:44 2014 +0000
+++ b/core/src/luan/modules/JavaLuan.java	Fri Jul 04 17:18:39 2014 +0000
@@ -32,22 +32,25 @@
 	public static final LuanFunction LOADER = new LuanFunction() {
 		@Override public Object call(LuanState luan,Object[] args) {
 			LuanTable module = new LuanTable();
-			MyMetatableGetter mmg = new MyMetatableGetter();
-			mmg.init();
-			module.put( MetatableGetter.KEY, mmg );
+			module.put( MetatableGetter.KEY, new MyMetatableGetter() );
 			try {
 				module.put( "class", new LuanJavaFunction(JavaLuan.class.getMethod("getClass",LuanState.class,String.class),null) );
 				add( module, "proxy", LuanState.class, Static.class, LuanTable.class, Object.class );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
 			}
-			luan.searchers().add(javaSearcher);
 			return module;
 		}
 	};
 
+	private static boolean isLoaded(LuanState luan) {
+		return PackageLuan.loaded(luan).get("Java") != null;
+	}
+
 	public static final LuanFunction javaSearcher = new LuanFunction() {
 		@Override public Object call(LuanState luan,Object[] args) throws LuanException {
+			if( !isLoaded(luan) )
+				return LuanFunction.NOTHING;
 			String modName = (String)args[0];
 			final Static s = JavaLuan.getClass(luan,modName);
 			if( s==null )
@@ -72,9 +75,7 @@
 	public static class MyMetatableGetter implements MetatableGetter {
 		private LuanTable metatable;
 
-		private MyMetatableGetter() {}
-
-		private void init() {
+		private MyMetatableGetter() {
 			this.metatable = new LuanTable();
 			try {
 				metatable.put( "__index", new LuanJavaFunction(
@@ -86,8 +87,10 @@
 			add( metatable, "__newindex", LuanState.class, Object.class, Object.class, Object.class );
 		}
 
+		private MyMetatableGetter(MyMetatableGetter mmg) {}
+
 		@Override public MetatableGetter shallowClone() {
-			return new MyMetatableGetter();
+			return new MyMetatableGetter(this);
 		}
 
 		@Override public void deepenClone(MetatableGetter c,DeepCloner cloner) {
diff -r 27abb3746917 -r 75750ceb45ee core/src/luan/modules/PackageLuan.java
--- a/core/src/luan/modules/PackageLuan.java	Thu Jul 03 22:42:44 2014 +0000
+++ b/core/src/luan/modules/PackageLuan.java	Fri Jul 04 17:18:39 2014 +0000
@@ -21,10 +21,10 @@
 	public static final LuanFunction LOADER = new LuanFunction() {
 		@Override public Object call(LuanState luan,Object[] args) {
 			LuanTable module = new LuanTable();
-			module.put("loaded",luan.loaded());
-			module.put("preload",luan.preload());
-			module.put("path","?.luan;java:luan/modules/?.luan");
-			module.put("jpath",jpath);
+			module.put( "loaded", loaded(luan) );
+			module.put( "preload", new LuanTable() );
+			module.put( "path", "?.luan;java:luan/modules/?.luan" );
+			module.put( "jpath", jpath );
 			try {
 				module.put("require",requireFn);
 				add( module, "block_lib", LuanState.class, String.class );
@@ -35,11 +35,7 @@
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
 			}
-			LuanTable searchers = luan.searchers();
-			searchers.add(preloadSearcher);
-			searchers.add(fileSearcher);
-			searchers.add(javaSearcher);
-			module.put("searchers",searchers);
+			module.put( "searchers", searchers(luan) );
 			return module;
 		}
 	};
@@ -57,6 +53,33 @@
 		t.put( method, new LuanJavaFunction(PackageLuan.class.getMethod(method,parameterTypes),null) );
 	}
 
+	public static LuanTable loaded(LuanState luan) {
+		return luan.registryTable("Package.loaded");
+	}
+
+	private static LuanTable blocked(LuanState luan) {
+		return luan.registryTable("Package.blocked");
+	}
+
+	private static Object pkg(LuanState luan,String key) {
+		LuanTable t = (LuanTable)loaded(luan).get("Package");
+		return t==null ? null : t.get(key);
+	}
+
+	private static LuanTable searchers(LuanState luan) {
+		String key = "Package.searchers";
+		LuanTable tbl = (LuanTable)luan.registry().get(key);
+		if( tbl == null ) {
+			tbl = new LuanTable();
+			tbl.add(preloadSearcher);
+			tbl.add(fileSearcher);
+			tbl.add(javaSearcher);
+			tbl.add(JavaLuan.javaSearcher);
+			luan.registry().put(key,tbl);
+		}
+		return tbl;
+	}
+
 	public static Object require(LuanState luan,String modName) throws LuanException {
 		Object mod = load(luan,modName);
 		if( mod==null )
@@ -65,7 +88,7 @@
 	}
 
 	public static Object load(LuanState luan,String modName) throws LuanException {
-		LuanTable loaded = luan.loaded();
+		LuanTable loaded = loaded(luan);
 		Object mod = loaded.get(modName);
 		if( mod == null ) {
 			Object[] a = search(luan,modName);
@@ -94,14 +117,7 @@
 	}
 
 	public static Object[] search(LuanState luan,String modName) throws LuanException {
-		List<Object> list = null;
-		LuanTable searchers = (LuanTable)luan.get("Package.searchers");
-		if( searchers == null ) {
-			list = Collections.<Object>singletonList(javaSearcher);
-		} else {
-			list = searchers.asList();
-		}
-		for( Object s : list ) {
+		for( Object s : searchers(luan).asList() ) {
 			LuanFunction searcher = (LuanFunction)s;
 			Object[] a = Luan.array(luan.call(searcher,"<searcher>",new Object[]{modName}));
 			if( a.length >= 1 && a[0] instanceof LuanFunction )
@@ -113,7 +129,8 @@
 	public static final LuanFunction preloadSearcher = new LuanFunction() {
 		@Override public Object call(LuanState luan,Object[] args) {
 			String modName = (String)args[0];
-			return luan.preload().get(modName);
+			LuanTable preload = (LuanTable)pkg(luan,"preload");
+			return preload==null ? LuanFunction.NOTHING : preload.get(modName);
 		}
 	};
 
@@ -137,7 +154,7 @@
 	public static final LuanFunction fileSearcher = new LuanFunction() {
 		@Override public Object[] call(LuanState luan,Object[] args) {
 			String modName = (String)args[0];
-			String path = (String)luan.get("Package.path");
+			String path = (String)pkg(luan,"path");
 			if( path==null )
 				return LuanFunction.NOTHING;
 			String file = search_path(modName,path);
@@ -168,7 +185,7 @@
 		{
 			String modName = (String)args[0];
 			modName = modName.replace('/','.');
-			String path = (String)luan.get("Package.jpath");
+			String path = (String)pkg(luan,"jpath");
 			if( path==null )
 				path = jpath;
 			for( String s : path.split(";") ) {
@@ -187,13 +204,13 @@
 
 
 	public static void block_lib(LuanState luan,String path) {
-		luan.blocked.add(path);
+		blocked(luan).put(path,true);
 	}
 
 	public static LuanFunction load_lib(LuanState luan,String path)
 		throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, LuanException
 	{
-		if( luan.blocked.contains(path) )
+		if( blocked(luan).get(path) != null )
 			throw luan.exception(path+" is blocked");
 		int i = path.lastIndexOf('.');
 		String clsPath = path.substring(0,i);
diff -r 27abb3746917 -r 75750ceb45ee core/src/luan/modules/PickleClient.java
--- a/core/src/luan/modules/PickleClient.java	Thu Jul 03 22:42:44 2014 +0000
+++ b/core/src/luan/modules/PickleClient.java	Fri Jul 04 17:18:39 2014 +0000
@@ -39,11 +39,11 @@
 	public Object call(Object... args) throws LuanException, IOException {
 		con.write(args);
 		Object[] result;
-		con.ioModule.put("_reversed_pickle",_reversed_pickle);
+		con.env.put("_reversed_pickle",_reversed_pickle);
 		try {
 			result = Luan.array(con.read());
 		} finally {
-			con.ioModule.put("_reversed_pickle",null);
+			con.env.put("_reversed_pickle",null);
 		}
 		boolean ok = (boolean)result[0];
 		if( ok ) {
diff -r 27abb3746917 -r 75750ceb45ee core/src/luan/modules/PickleCon.java
--- a/core/src/luan/modules/PickleCon.java	Thu Jul 03 22:42:44 2014 +0000
+++ b/core/src/luan/modules/PickleCon.java	Fri Jul 04 17:18:39 2014 +0000
@@ -23,11 +23,10 @@
 	final LuanState luan;
 	private final DataInputStream in;
 	private final LuanFunction _read_binary;
-	final LuanTable ioModule;
 	private final DataOutputStream out;
 	private final List<byte[]> binaries = new ArrayList<byte[]>();
 	String src;
-	private final LuanTable env = new LuanTable();
+	final LuanTable env = new LuanTable();
 
 	PickleCon(LuanState luan,DataInputStream in,DataOutputStream out) {
 		this.in = in;
@@ -39,8 +38,6 @@
 		} catch(NoSuchMethodException e) {
 			throw new RuntimeException(e);
 		}
-		this.ioModule = (LuanTable)luan.loaded().get("Io");
-		env.put("Io",ioModule);
 
 		this.out = out;
 	}
@@ -58,14 +55,14 @@
 	}
 
 	public Object read() throws IOException, LuanException {
-		ioModule.put("_read_binary",_read_binary);
+		env.put("_read_binary",_read_binary);
 		try {
 			src = in.readUTF();
 			LuanFunction fn = BasicLuan.load(luan,src,"pickle-reader",env,false);
 			return luan.call(fn);
 		} finally {
-			ioModule.put("_binaries",null);
-			ioModule.put("_read_binary",null);
+			env.put("_binaries",null);
+			env.put("_read_binary",null);
 		}
 	}
 
@@ -83,7 +80,7 @@
 		if( obj instanceof byte[] ) {
 			byte[] a = (byte[])obj;
 			binaries.add(a);
-			return "Io._binaries[" + binaries.size() + "]";
+			return "_binaries[" + binaries.size() + "]";
 		}
 		throw luan.exception( "invalid type: " + obj.getClass() );
 	}
@@ -112,9 +109,9 @@
 	public void write(Object... args) throws LuanException, IOException {
 		StringBuilder sb = new StringBuilder();
 		if( !binaries.isEmpty() ) {
-			sb.append( "Io._binaries = {}\n" );
+			sb.append( "_binaries = {}\n" );
 			for( byte[] a : binaries ) {
-				sb.append( "Io._binaries[#Io._binaries+1] = Io._read_binary(" + a.length + ")\n" );
+				sb.append( "_binaries[#_binaries+1] = _read_binary(" + a.length + ")\n" );
 			}
 		}
 		for( Object obj : args ) {
diff -r 27abb3746917 -r 75750ceb45ee core/src/luan/modules/PickleServer.java
--- a/core/src/luan/modules/PickleServer.java	Thu Jul 03 22:42:44 2014 +0000
+++ b/core/src/luan/modules/PickleServer.java	Fri Jul 04 17:18:39 2014 +0000
@@ -53,15 +53,16 @@
 	}
 
 	public void run() {
-		LuanTable ioModule = con.ioModule;
-		Object old_reverse_pickle = ioModule.get("reverse_pickle");
-		Object old_unreverse_pickle = ioModule.get("_unreverse_pickle");
+		LuanTable io = (LuanTable)PackageLuan.loaded(con.luan).get("Io");
+		LuanTable env = con.env;
+		Object old_reverse_pickle = io.get("reverse_pickle");
+		Object old_unreverse_pickle = env.get("_unreverse_pickle");
 		try {
 			try {
-				ioModule.put("reverse_pickle", new LuanJavaFunction(
+				io.put("reverse_pickle", new LuanJavaFunction(
 					PickleServer.class.getMethod( "reverse_pickle", LuanFunction.class ), this
 				) );
-				ioModule.put("_unreverse_pickle", new LuanJavaFunction(
+				env.put("_unreverse_pickle", new LuanJavaFunction(
 					PickleServer.class.getMethod( "_unreverse_pickle" ), this
 				) );
 			} catch(NoSuchMethodException e) {
@@ -85,14 +86,14 @@
 				}
 			}
 		} finally {
-			ioModule.put("reverse_pickle",old_reverse_pickle);
-			ioModule.put("_unreverse_pickle",old_unreverse_pickle);
+			io.put("reverse_pickle",old_reverse_pickle);
+			env.put("_unreverse_pickle",old_unreverse_pickle);
 		}
 	}
 
 	public void reverse_pickle(LuanFunction fn) throws IOException, LuanException {
 		try {
-			con.write( "return Io._reversed_pickle()\n" );
+			con.write( "return _reversed_pickle()\n" );
 		} catch(LuanException e) {
 			throw new RuntimeException(e);
 		}
@@ -101,7 +102,7 @@
 			con.luan.call(fn,new Object[]{pc.table()});
 		} finally {
 			try {
-				pc.call( "Io._unreverse_pickle()\n" );
+				pc.call( "_unreverse_pickle()\n" );
 			} catch(LuanException e) {
 				throw new RuntimeException(e);
 			}
diff -r 27abb3746917 -r 75750ceb45ee core/src/luan/modules/StringLuan.java
--- a/core/src/luan/modules/StringLuan.java	Thu Jul 03 22:42:44 2014 +0000
+++ b/core/src/luan/modules/StringLuan.java	Fri Jul 04 17:18:39 2014 +0000
@@ -18,9 +18,7 @@
 	public static final LuanFunction LOADER = new LuanFunction() {
 		@Override public Object call(LuanState luan,Object[] args) {
 			LuanTable module = new LuanTable();
-			MyMetatableGetter mmg = new MyMetatableGetter();
-			mmg.init(module);
-			module.put( MetatableGetter.KEY, mmg );
+			module.put( MetatableGetter.KEY, new MyMetatableGetter(module) );
 			try {
 				add( module, "to_binary", String.class );
 				add( module, "to_integers", String.class );
@@ -51,9 +49,7 @@
 		private LuanTable module;
 		private LuanTable metatable;
 
-		private MyMetatableGetter() {}
-
-		private void init(LuanTable module) {
+		private MyMetatableGetter(LuanTable module) {
 			this.module = module;
 			this.metatable = new LuanTable();
 			try {
@@ -65,8 +61,10 @@
 			}
 		}
 
+		private MyMetatableGetter(MyMetatableGetter mmg) {}
+
 		@Override public MetatableGetter shallowClone() {
-			return new MyMetatableGetter();
+			return new MyMetatableGetter(this);
 		}
 
 		@Override public void deepenClone(MetatableGetter c,DeepCloner cloner) {
diff -r 27abb3746917 -r 75750ceb45ee web/src/luan/modules/web/HttpLuan.java
--- a/web/src/luan/modules/web/HttpLuan.java	Thu Jul 03 22:42:44 2014 +0000
+++ b/web/src/luan/modules/web/HttpLuan.java	Fri Jul 04 17:18:39 2014 +0000
@@ -79,7 +79,7 @@
 			}
 		}
 
-		LuanTable module = (LuanTable)luan.loaded().get("web/Http");
+		LuanTable module = (LuanTable)PackageLuan.loaded(luan).get("web/Http");
 		if( module == null )
 			throw luan.exception( "module 'web/Http' not defined" );
 		HttpLuan lib = new HttpLuan(request,response);