changeset 147:cc3a0578edac

fix Io.reverse_pickle git-svn-id: https://luan-java.googlecode.com/svn/trunk@148 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 15 Jun 2014 15:41:36 +0000
parents 0517a4a7fcc5
children dfd0f33b584e
files src/luan/Luan.java src/luan/LuanFunction.java src/luan/LuanTable.java src/luan/lib/IoLib.java src/luan/lib/OsLib.java src/luan/lib/PickleCon.java src/luan/lib/PickleServer.java src/luan/parser/Parser.java
diffstat 8 files changed, 43 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
diff -r 0517a4a7fcc5 -r cc3a0578edac src/luan/Luan.java
--- a/src/luan/Luan.java	Sun Jun 15 13:35:33 2014 +0000
+++ b/src/luan/Luan.java	Sun Jun 15 15:41:36 2014 +0000
@@ -135,6 +135,8 @@
 	public static String repr(Object obj) {
 		if( obj == null )
 			return "nil";
+		if( obj instanceof Boolean )
+			return Luan.toString((Boolean)obj);
 		if( obj instanceof Number )
 			return Luan.toString((Number)obj);
 		if( obj instanceof String )
diff -r 0517a4a7fcc5 -r cc3a0578edac src/luan/LuanFunction.java
--- a/src/luan/LuanFunction.java	Sun Jun 15 13:35:33 2014 +0000
+++ b/src/luan/LuanFunction.java	Sun Jun 15 15:41:36 2014 +0000
@@ -1,7 +1,7 @@
 package luan;
 
 
-public abstract class LuanFunction {
+public abstract class LuanFunction implements LuanRepr {
 
 	public abstract Object call(LuanState luan,Object[] args) throws LuanException;
 
@@ -11,4 +11,8 @@
 		return "function: " + Integer.toHexString(hashCode());
 	}
 
+	@Override public String repr() {
+		return "<function>";
+	}
+
 }
diff -r 0517a4a7fcc5 -r cc3a0578edac src/luan/LuanTable.java
--- a/src/luan/LuanTable.java	Sun Jun 15 13:35:33 2014 +0000
+++ b/src/luan/LuanTable.java	Sun Jun 15 15:41:36 2014 +0000
@@ -120,7 +120,7 @@
 		return "table: " + Integer.toHexString(hashCode());
 	}
 
-	public String repr() {
+	@Override public String repr() {
 		return repr( Collections.newSetFromMap(new IdentityHashMap<LuanTable,Boolean>()) );
 	}
 
@@ -181,7 +181,7 @@
 		} else {
 			String s = Luan.repr(obj);
 			if( s == null )
-				s = "\"<couldn't repr: " + Luan.stringEncode(Luan.toString(obj)) + ">\"";
+				s = "<couldn't repr: " + Luan.stringEncode(Luan.toString(obj)) + ">";
 			return s;
 		}
 	}
diff -r 0517a4a7fcc5 -r cc3a0578edac src/luan/lib/IoLib.java
--- a/src/luan/lib/IoLib.java	Sun Jun 15 13:35:33 2014 +0000
+++ b/src/luan/lib/IoLib.java	Sun Jun 15 15:41:36 2014 +0000
@@ -212,7 +212,7 @@
 
 	public static abstract class LuanIn {
 		abstract InputStream inputStream() throws IOException;
-		public abstract String name();
+		public abstract String to_string();
 
 		public String read_text() throws IOException {
 			Reader in = new InputStreamReader(inputStream());
@@ -240,8 +240,8 @@
 		LuanTable table() {
 			LuanTable tbl = new LuanTable();
 			try {
-				tbl.put( "name", new LuanJavaFunction(
-					LuanIn.class.getMethod( "name" ), this
+				tbl.put( "to_string", new LuanJavaFunction(
+					LuanIn.class.getMethod( "to_string" ), this
 				) );
 				tbl.put( "read_text", new LuanJavaFunction(
 					LuanIn.class.getMethod( "read_text" ), this
@@ -321,7 +321,7 @@
 			return url.openStream();
 		}
 
-		@Override public String name() {
+		@Override public String to_string() {
 			return url.toString();
 		}
 	}
@@ -345,7 +345,7 @@
 			return new FileOutputStream(file);
 		}
 
-		@Override public String name() {
+		@Override public String to_string() {
 			return file.toString();
 		}
 
@@ -402,7 +402,7 @@
 			return socket.getOutputStream();
 		}
 
-		@Override public String name() {
+		@Override public String to_string() {
 			return socket.toString();
 		}
 
diff -r 0517a4a7fcc5 -r cc3a0578edac src/luan/lib/OsLib.java
--- a/src/luan/lib/OsLib.java	Sun Jun 15 13:35:33 2014 +0000
+++ b/src/luan/lib/OsLib.java	Sun Jun 15 15:41:36 2014 +0000
@@ -45,7 +45,7 @@
 			return new IoLib.LuanFile(file).table();
 		}
 
-		public LuanTable list_children() {
+		public LuanTable children() {
 			File[] files = file.listFiles();
 			if( files==null )
 				return null;
@@ -59,8 +59,11 @@
 		LuanTable table() {
 			LuanTable tbl = new LuanTable();
 			try {
+				tbl.put( "to_string", new LuanJavaFunction(
+					File.class.getMethod( "toString" ), file
+				) );
 				tbl.put( "name", new LuanJavaFunction(
-					File.class.getMethod( "toString" ), file
+					File.class.getMethod( "getName" ), file
 				) );
 				tbl.put( "exists", new LuanJavaFunction(
 					File.class.getMethod( "exists" ), file
@@ -89,8 +92,8 @@
 				tbl.put( "io_file", new LuanJavaFunction(
 					LuanFile.class.getMethod( "io_file" ), this
 				) );
-				tbl.put( "list_children", new LuanJavaFunction(
-					LuanFile.class.getMethod( "list_children" ), this
+				tbl.put( "children", new LuanJavaFunction(
+					LuanFile.class.getMethod( "children" ), this
 				) );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
diff -r 0517a4a7fcc5 -r cc3a0578edac src/luan/lib/PickleCon.java
--- a/src/luan/lib/PickleCon.java	Sun Jun 15 13:35:33 2014 +0000
+++ b/src/luan/lib/PickleCon.java	Sun Jun 15 15:41:36 2014 +0000
@@ -70,6 +70,8 @@
 	public String pickle(Object obj) throws LuanException {
 		if( obj == null )
 			return "nil";
+		if( obj instanceof Boolean )
+			return Luan.toString((Boolean)obj);
 		if( obj instanceof Number )
 			return Luan.toString((Number)obj);
 		if( obj instanceof String )
diff -r 0517a4a7fcc5 -r cc3a0578edac src/luan/lib/PickleServer.java
--- a/src/luan/lib/PickleServer.java	Sun Jun 15 13:35:33 2014 +0000
+++ b/src/luan/lib/PickleServer.java	Sun Jun 15 15:41:36 2014 +0000
@@ -38,6 +38,7 @@
 					list.add( con.pickle(obj) );
 				}
 			} catch(LuanException e) {
+//				System.out.println(e);
 //e.printStackTrace();
 				list.add( "return false, " );
 				list.add( con.pickle(e.getMessage()) );
@@ -54,14 +55,14 @@
 	public void run() {
 		LuanTable ioModule = con.ioModule;
 		Object old_reverse_pickle = ioModule.get("reverse_pickle");
-		Object old_close_pickle = ioModule.get("unreverse_pickle");
+		Object old_unreverse_pickle = ioModule.get("_unreverse_pickle");
 		try {
 			try {
 				ioModule.put("reverse_pickle", new LuanJavaFunction(
-					PickleServer.class.getMethod( "reverse_pickle" ), this
+					PickleServer.class.getMethod( "reverse_pickle", LuanFunction.class ), this
 				) );
-				ioModule.put("unreverse_pickle", new LuanJavaFunction(
-					PickleServer.class.getMethod( "unreverse_pickle" ), this
+				ioModule.put("_unreverse_pickle", new LuanJavaFunction(
+					PickleServer.class.getMethod( "_unreverse_pickle" ), this
 				) );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
@@ -85,20 +86,29 @@
 			}
 		} finally {
 			ioModule.put("reverse_pickle",old_reverse_pickle);
-			ioModule.put("unreverse_pickle",old_close_pickle);
+			ioModule.put("_unreverse_pickle",old_unreverse_pickle);
 		}
 	}
 
-	public LuanTable reverse_pickle() throws IOException {
+	public void reverse_pickle(LuanFunction fn) throws IOException, LuanException {
 		try {
 			con.write( "return Io._reversed_pickle()\n" );
 		} catch(LuanException e) {
 			throw new RuntimeException(e);
 		}
-		return new PickleClient(con).table();
+		PickleClient pc = new PickleClient(con);
+		try {
+			con.luan.call(fn,new Object[]{pc.table()});
+		} finally {
+			try {
+				pc.call( "Io._unreverse_pickle()\n" );
+			} catch(LuanException e) {
+				throw new RuntimeException(e);
+			}
+		}
 	}
 
-	public void unreverse_pickle() {
+	public void _unreverse_pickle() {
 		isRunning = false;
 	}
 
diff -r 0517a4a7fcc5 -r cc3a0578edac src/luan/parser/Parser.java
--- a/src/luan/parser/Parser.java	Sun Jun 15 13:35:33 2014 +0000
+++ b/src/luan/parser/Parser.java	Sun Jun 15 15:41:36 2014 +0000
@@ -28,12 +28,12 @@
 	}
 
 	public int begin() {
+		frame++;
 		if( frame == stack.length ) {
 			int[] a = new int[2*frame];
 			System.arraycopy(stack,0,a,0,frame);
 			stack = a;
 		}
-		frame++;
 		stack[frame] = stack[frame-1];
 		return i();
 	}