Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
170:7c792a328a83 | 171:3dcb0f9bee82 |
---|---|
1 package luan.modules; | |
2 | |
3 import java.io.DataInputStream; | |
4 import java.io.DataOutputStream; | |
5 import java.io.IOException; | |
6 import luan.Luan; | |
7 import luan.LuanState; | |
8 import luan.LuanException; | |
9 import luan.LuanTable; | |
10 import luan.LuanJavaFunction; | |
11 import luan.LuanFunction; | |
12 | |
13 | |
14 public final class PickleClient { | |
15 | |
16 private final PickleCon con; | |
17 private final LuanFunction _reversed_pickle; | |
18 | |
19 PickleClient(LuanState luan,DataInputStream in,DataOutputStream out) { | |
20 this(new PickleCon(luan,in,out)); | |
21 } | |
22 | |
23 PickleClient(PickleCon con) { | |
24 this.con = con; | |
25 try { | |
26 this._reversed_pickle = new LuanJavaFunction( | |
27 PickleClient.class.getMethod( "_reversed_pickle" ), this | |
28 ); | |
29 } catch(NoSuchMethodException e) { | |
30 throw new RuntimeException(e); | |
31 } | |
32 } | |
33 | |
34 public Object _reversed_pickle() throws LuanException, IOException { | |
35 new PickleServer(con).run(); | |
36 return con.read(); | |
37 } | |
38 | |
39 public Object call(Object... args) throws LuanException, IOException { | |
40 con.write(args); | |
41 Object[] result; | |
42 con.ioModule.put("_reversed_pickle",_reversed_pickle); | |
43 try { | |
44 result = Luan.array(con.read()); | |
45 } finally { | |
46 con.ioModule.put("_reversed_pickle",null); | |
47 } | |
48 boolean ok = (boolean)result[0]; | |
49 if( ok ) { | |
50 Object[] rtn = new Object[result.length-1]; | |
51 System.arraycopy(result,1,rtn,0,rtn.length); | |
52 return rtn; | |
53 } else { | |
54 String msg = (String)result[1]; | |
55 String src = (String)result[2]; | |
56 throw con.luan.exception( | |
57 msg + "\n" | |
58 + "in:\n" | |
59 + "------------------\n" | |
60 + formatCode(src) + "\n" | |
61 + "------------------\n" | |
62 ); | |
63 } | |
64 } | |
65 | |
66 LuanTable table() { | |
67 LuanTable tbl = new LuanTable(); | |
68 try { | |
69 tbl.put( "pickle", new LuanJavaFunction( | |
70 PickleCon.class.getMethod( "pickle", Object.class ), con | |
71 ) ); | |
72 tbl.put( "call", new LuanJavaFunction( | |
73 PickleClient.class.getMethod( "call", new Object[0].getClass() ), this | |
74 ) ); | |
75 tbl.put( "close", new LuanJavaFunction( | |
76 PickleCon.class.getMethod( "close" ), con | |
77 ) ); | |
78 } catch(NoSuchMethodException e) { | |
79 throw new RuntimeException(e); | |
80 } | |
81 return tbl; | |
82 } | |
83 | |
84 | |
85 public static String formatCode(String s) { | |
86 StringBuilder buf = new StringBuilder(); | |
87 int line = 1; | |
88 int i = 0; | |
89 int i2 = 0; | |
90 while( i2 != -1 ) { | |
91 buf.append( line++ ); | |
92 buf.append( '\t' ); | |
93 i2 = s.indexOf('\n',i); | |
94 String lineStr = i2 == -1 ? s.substring(i) : s.substring(i,i2+1); | |
95 int j; | |
96 for( j=0; j<lineStr.length() && lineStr.charAt(j)=='\t'; j++ ) { | |
97 buf.append( " " ); | |
98 } | |
99 buf.append( lineStr.substring(j) ); | |
100 i = i2 + 1; | |
101 } | |
102 return buf.toString(); | |
103 } | |
104 | |
105 | |
106 } |