diff src/luan/lib/PickleClient.java @ 129:486a0641bca4

add pickle client/server; fix parser bugs; git-svn-id: https://luan-java.googlecode.com/svn/trunk@130 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 09 Jun 2014 09:16:16 +0000
parents
children 0594c132888b
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/lib/PickleClient.java	Mon Jun 09 09:16:16 2014 +0000
@@ -0,0 +1,60 @@
+package luan.lib;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import luan.Luan;
+import luan.LuanState;
+import luan.LuanException;
+import luan.LuanTable;
+import luan.LuanJavaFunction;
+
+
+public final class PickleClient {
+
+	private final PickleCon con;
+
+	PickleClient(LuanState luan,DataInputStream in,DataOutputStream out) {
+		con = new PickleCon(luan,in,out);
+	}
+
+	public Object call(Object... args) throws LuanException, IOException {
+		con.write(args);
+		Object[] result = Luan.array(con.read());
+		boolean ok = (boolean)result[0];
+		if( ok ) {
+			Object[] rtn = new Object[result.length-1];
+			System.arraycopy(result,1,rtn,0,rtn.length);
+			return rtn;
+		} else {
+			String msg = (String)result[1];
+			String src = (String)result[2];
+			throw con.luan.JAVA.exception(
+				msg + "\n"
+				+ "in:\n"
+				+ "------------------\n"
+				+ src + "\n"
+				+ "------------------\n"
+			);
+		}
+	}
+
+	LuanTable table() {
+		LuanTable tbl = new LuanTable();
+		try {
+			tbl.put( "pickle", new LuanJavaFunction(
+				PickleCon.class.getMethod( "pickle", Object.class ), con
+			) );
+			tbl.put( "call", new LuanJavaFunction(
+				PickleClient.class.getMethod( "call", new Object[0].getClass() ), this
+			) );
+			tbl.put( "close", new LuanJavaFunction(
+				PickleCon.class.getMethod( "close" ), con
+			) );
+		} catch(NoSuchMethodException e) {
+			throw new RuntimeException(e);
+		}
+		return tbl;
+	}
+
+}