comparison core/src/luan/modules/RpcLuan.java @ 646:cdc70de628b5

simplify LuanException
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 29 Mar 2016 19:58:39 -0600
parents b4f3dbe1c6e3
children b620b8e1010f
comparison
equal deleted inserted replaced
645:859c0dedc8b6 646:cdc70de628b5
48 rtn[i] = readObj(in,luan); 48 rtn[i] = readObj(in,luan);
49 } 49 }
50 return rtn; 50 return rtn;
51 } else { 51 } else {
52 String msg = readString(in); 52 String msg = readString(in);
53 throw new LuanException(luan,msg); 53 throw new LuanException(msg);
54 } 54 }
55 } finally { 55 } finally {
56 out.close(); 56 out.close();
57 in.close(); 57 in.close();
58 } 58 }
73 for( int i=0; i<nArgs; i++ ) { 73 for( int i=0; i<nArgs; i++ ) {
74 args[i] = readObj(in,luan); 74 args[i] = readObj(in,luan);
75 } 75 }
76 LuanFunction fn = (LuanFunction)fns.rawGet(fnName); 76 LuanFunction fn = (LuanFunction)fns.rawGet(fnName);
77 if( fn == null ) 77 if( fn == null )
78 throw new LuanException(luan, "function not found: " + fnName ); 78 throw new LuanException( "function not found: " + fnName );
79 rtn = Luan.array(fn.call(luan,args)); 79 rtn = Luan.array(fn.call(luan,args));
80 } catch(LuanException e) { 80 } catch(LuanException e) {
81 writeBoolean(out,false); 81 writeBoolean(out,false);
82 writeString(out,e.getFullMessage()); 82 writeString(out,e.getFullMessage());
83 return; 83 return;
119 out.write(TABLE); 119 out.write(TABLE);
120 String s = pickle( luan, obj, Collections.newSetFromMap(new IdentityHashMap<LuanTable,Boolean>()) ); 120 String s = pickle( luan, obj, Collections.newSetFromMap(new IdentityHashMap<LuanTable,Boolean>()) );
121 writeString(out,s); 121 writeString(out,s);
122 } 122 }
123 else 123 else
124 throw new LuanException(luan, "invalid type: " + obj.getClass() ); 124 throw new LuanException( "invalid type: " + obj.getClass() );
125 } 125 }
126 126
127 static Object readObj(InputStream in,LuanState luan) throws IOException, LuanException { 127 static Object readObj(InputStream in,LuanState luan) throws IOException, LuanException {
128 int type = in.read(); 128 int type = in.read();
129 switch(type) { 129 switch(type) {
140 case TABLE: 140 case TABLE:
141 String s = readString(in); 141 String s = readString(in);
142 LuanFunction fn = BasicLuan.load(luan,s,"rpc-reader",null,true); 142 LuanFunction fn = BasicLuan.load(luan,s,"rpc-reader",null,true);
143 return fn.call(luan); 143 return fn.call(luan);
144 default: 144 default:
145 throw new LuanException(luan, "invalid type: " + type ); 145 throw new LuanException( "invalid type: " + type );
146 } 146 }
147 } 147 }
148 148
149 static Boolean readBoolean(InputStream in) throws IOException { 149 static Boolean readBoolean(InputStream in) throws IOException {
150 return Boolean.valueOf(readString(in)); 150 return Boolean.valueOf(readString(in));
206 if( obj instanceof String ) 206 if( obj instanceof String )
207 return "\"" + Luan.stringEncode((String)obj) + "\""; 207 return "\"" + Luan.stringEncode((String)obj) + "\"";
208 if( obj instanceof LuanTable ) { 208 if( obj instanceof LuanTable ) {
209 LuanTable tbl = (LuanTable)obj; 209 LuanTable tbl = (LuanTable)obj;
210 if( !set.add(tbl) ) { 210 if( !set.add(tbl) ) {
211 throw new LuanException(luan, "circular reference in table" ); 211 throw new LuanException( "circular reference in table" );
212 } 212 }
213 StringBuilder sb = new StringBuilder(); 213 StringBuilder sb = new StringBuilder();
214 sb.append( "{" ); 214 sb.append( "{" );
215 for( Map.Entry<Object,Object> entry : tbl.iterable(luan) ) { 215 for( Map.Entry<Object,Object> entry : tbl.iterable(luan) ) {
216 sb.append( "[" ); 216 sb.append( "[" );
220 sb.append( ", " ); 220 sb.append( ", " );
221 } 221 }
222 sb.append( "}" ); 222 sb.append( "}" );
223 return sb.toString(); 223 return sb.toString();
224 } 224 }
225 throw new LuanException(luan, "invalid type: " + obj.getClass() ); 225 throw new LuanException( "invalid type: " + obj.getClass() );
226 } 226 }
227 227
228 } 228 }