changeset 146:0517a4a7fcc5

add Io.reverse_pickle git-svn-id: https://luan-java.googlecode.com/svn/trunk@147 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 15 Jun 2014 13:35:33 +0000
parents 90f38a5d0e0a
children cc3a0578edac
files src/luan/lib/OsLib.java src/luan/lib/PickleClient.java src/luan/lib/PickleCon.java src/luan/lib/PickleServer.java
diffstat 4 files changed, 107 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/lib/OsLib.java	Sun Jun 15 10:02:16 2014 +0000
+++ b/src/luan/lib/OsLib.java	Sun Jun 15 13:35:33 2014 +0000
@@ -45,6 +45,17 @@
 			return new IoLib.LuanFile(file).table();
 		}
 
+		public LuanTable list_children() {
+			File[] files = file.listFiles();
+			if( files==null )
+				return null;
+			LuanTable list = new LuanTable();
+			for( File f : files ) {
+				list.add(new LuanFile(f).table());
+			}
+			return list;
+		}
+
 		LuanTable table() {
 			LuanTable tbl = new LuanTable();
 			try {
@@ -69,12 +80,18 @@
 				tbl.put( "mkdirs", new LuanJavaFunction(
 					File.class.getMethod( "mkdirs" ), file
 				) );
+				tbl.put( "last_modified", new LuanJavaFunction(
+					File.class.getMethod( "lastModified" ), file
+				) );
 				tbl.put( "child", new LuanJavaFunction(
 					LuanFile.class.getMethod( "child", String.class ), this
 				) );
 				tbl.put( "io_file", new LuanJavaFunction(
 					LuanFile.class.getMethod( "io_file" ), this
 				) );
+				tbl.put( "list_children", new LuanJavaFunction(
+					LuanFile.class.getMethod( "list_children" ), this
+				) );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
 			}
--- a/src/luan/lib/PickleClient.java	Sun Jun 15 10:02:16 2014 +0000
+++ b/src/luan/lib/PickleClient.java	Sun Jun 15 13:35:33 2014 +0000
@@ -8,19 +8,43 @@
 import luan.LuanException;
 import luan.LuanTable;
 import luan.LuanJavaFunction;
+import luan.LuanFunction;
 
 
 public final class PickleClient {
 
 	private final PickleCon con;
+	private final LuanFunction _reversed_pickle;
 
 	PickleClient(LuanState luan,DataInputStream in,DataOutputStream out) {
-		con = new PickleCon(luan,in,out);
+		this(new PickleCon(luan,in,out));
+	}
+
+	PickleClient(PickleCon con) {
+		this.con = con;
+		try {
+			this._reversed_pickle = new LuanJavaFunction(
+				PickleClient.class.getMethod( "_reversed_pickle" ), this
+			);
+		} catch(NoSuchMethodException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	public Object _reversed_pickle() throws LuanException, IOException {
+		new PickleServer(con).run();
+		return con.read();
 	}
 
 	public Object call(Object... args) throws LuanException, IOException {
 		con.write(args);
-		Object[] result = Luan.array(con.read());
+		Object[] result;
+		con.ioModule.put("_reversed_pickle",_reversed_pickle);
+		try {
+			result = Luan.array(con.read());
+		} finally {
+			con.ioModule.put("_reversed_pickle",null);
+		}
 		boolean ok = (boolean)result[0];
 		if( ok ) {
 			Object[] rtn = new Object[result.length-1];
--- a/src/luan/lib/PickleCon.java	Sun Jun 15 10:02:16 2014 +0000
+++ b/src/luan/lib/PickleCon.java	Sun Jun 15 13:35:33 2014 +0000
@@ -23,7 +23,7 @@
 	final LuanState luan;
 	private final DataInputStream in;
 	private final LuanFunction _read_binary;
-	private final LuanTable ioModule;
+	final LuanTable ioModule;
 	private final DataOutputStream out;
 	private final List<byte[]> binaries = new ArrayList<byte[]>();
 	String src;
@@ -57,11 +57,14 @@
 
 	public Object read() throws IOException, LuanException {
 		ioModule.put("_read_binary",_read_binary);
-		src = in.readUTF();
-		LuanFunction fn = BasicLib.load(luan,src,"pickle-reader",true,false);
-		Object rtn = luan.call(fn);
-		ioModule.put("_binaries",null);
-		return rtn;
+		try {
+			src = in.readUTF();
+			LuanFunction fn = BasicLib.load(luan,src,"pickle-reader",true,false);
+			return luan.call(fn);
+		} finally {
+			ioModule.put("_binaries",null);
+			ioModule.put("_read_binary",null);
+		}
 	}
 
 	public String pickle(Object obj) throws LuanException {
--- a/src/luan/lib/PickleServer.java	Sun Jun 15 10:02:16 2014 +0000
+++ b/src/luan/lib/PickleServer.java	Sun Jun 15 13:35:33 2014 +0000
@@ -8,15 +8,23 @@
 import java.util.ArrayList;
 import luan.Luan;
 import luan.LuanState;
+import luan.LuanTable;
+import luan.LuanFunction;
+import luan.LuanJavaFunction;
 import luan.LuanException;
 
 
-final class PickleServer {
+public final class PickleServer {
 
 	private final PickleCon con;
+	private boolean isRunning;
 
 	PickleServer(LuanState luan,DataInputStream in,DataOutputStream out) {
-		con = new PickleCon(luan,in,out);
+		this(new PickleCon(luan,in,out));
+	}
+
+	PickleServer(PickleCon con) {
+		this.con = con;
 	}
 
 	void next() throws IOException {
@@ -44,20 +52,54 @@
 	}
 
 	public void run() {
+		LuanTable ioModule = con.ioModule;
+		Object old_reverse_pickle = ioModule.get("reverse_pickle");
+		Object old_close_pickle = ioModule.get("unreverse_pickle");
 		try {
-			while( true ) {
-				next();
+			try {
+				ioModule.put("reverse_pickle", new LuanJavaFunction(
+					PickleServer.class.getMethod( "reverse_pickle" ), this
+				) );
+				ioModule.put("unreverse_pickle", new LuanJavaFunction(
+					PickleServer.class.getMethod( "unreverse_pickle" ), this
+				) );
+			} catch(NoSuchMethodException e) {
+				throw new RuntimeException(e);
 			}
-		} catch(EOFException e) {
-			// done
-		} catch(IOException e) {
-			e.printStackTrace();
-		}
-		try {
-			con.close();
-		} catch(IOException e) {
-			throw new RuntimeException(e);
+			isRunning = true;
+			try {
+				while( isRunning ) {
+					next();
+				}
+			} catch(EOFException e) {
+				// done
+			} catch(IOException e) {
+				e.printStackTrace();
+			}
+			if( isRunning ) {
+				try {
+					con.close();
+				} catch(IOException e) {
+					throw new RuntimeException(e);
+				}
+			}
+		} finally {
+			ioModule.put("reverse_pickle",old_reverse_pickle);
+			ioModule.put("unreverse_pickle",old_close_pickle);
 		}
 	}
 
+	public LuanTable reverse_pickle() throws IOException {
+		try {
+			con.write( "return Io._reversed_pickle()\n" );
+		} catch(LuanException e) {
+			throw new RuntimeException(e);
+		}
+		return new PickleClient(con).table();
+	}
+
+	public void unreverse_pickle() {
+		isRunning = false;
+	}
+
 }