Mercurial Hosting > luan
diff core/src/luan/modules/PickleClient.java @ 171:3dcb0f9bee82
add core component
git-svn-id: https://luan-java.googlecode.com/svn/trunk@172 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sun, 22 Jun 2014 05:41:22 +0000 |
parents | src/luan/modules/PickleClient.java@4c0131c2b650 |
children | 75750ceb45ee |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/src/luan/modules/PickleClient.java Sun Jun 22 05:41:22 2014 +0000 @@ -0,0 +1,106 @@ +package luan.modules; + +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; +import luan.LuanFunction; + + +public final class PickleClient { + + private final PickleCon con; + private final LuanFunction _reversed_pickle; + + PickleClient(LuanState luan,DataInputStream in,DataOutputStream 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; + 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]; + System.arraycopy(result,1,rtn,0,rtn.length); + return rtn; + } else { + String msg = (String)result[1]; + String src = (String)result[2]; + throw con.luan.exception( + msg + "\n" + + "in:\n" + + "------------------\n" + + formatCode(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; + } + + + public static String formatCode(String s) { + StringBuilder buf = new StringBuilder(); + int line = 1; + int i = 0; + int i2 = 0; + while( i2 != -1 ) { + buf.append( line++ ); + buf.append( '\t' ); + i2 = s.indexOf('\n',i); + String lineStr = i2 == -1 ? s.substring(i) : s.substring(i,i2+1); + int j; + for( j=0; j<lineStr.length() && lineStr.charAt(j)=='\t'; j++ ) { + buf.append( " " ); + } + buf.append( lineStr.substring(j) ); + i = i2 + 1; + } + return buf.toString(); + } + + +}