Mercurial Hosting > luan
comparison core/src/luan/modules/PickleCon.java @ 202:75750ceb45ee
add LuanState.registry
git-svn-id: https://luan-java.googlecode.com/svn/trunk@203 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 04 Jul 2014 17:18:39 +0000 |
parents | fb3993431f76 |
children | ec016471c6eb |
comparison
equal
deleted
inserted
replaced
201:27abb3746917 | 202:75750ceb45ee |
---|---|
21 | 21 |
22 public final class PickleCon { | 22 public final class PickleCon { |
23 final LuanState luan; | 23 final LuanState luan; |
24 private final DataInputStream in; | 24 private final DataInputStream in; |
25 private final LuanFunction _read_binary; | 25 private final LuanFunction _read_binary; |
26 final LuanTable ioModule; | |
27 private final DataOutputStream out; | 26 private final DataOutputStream out; |
28 private final List<byte[]> binaries = new ArrayList<byte[]>(); | 27 private final List<byte[]> binaries = new ArrayList<byte[]>(); |
29 String src; | 28 String src; |
30 private final LuanTable env = new LuanTable(); | 29 final LuanTable env = new LuanTable(); |
31 | 30 |
32 PickleCon(LuanState luan,DataInputStream in,DataOutputStream out) { | 31 PickleCon(LuanState luan,DataInputStream in,DataOutputStream out) { |
33 this.in = in; | 32 this.in = in; |
34 this.luan = luan; | 33 this.luan = luan; |
35 try { | 34 try { |
37 PickleCon.class.getMethod( "_read_binary", Integer.TYPE ), this | 36 PickleCon.class.getMethod( "_read_binary", Integer.TYPE ), this |
38 ); | 37 ); |
39 } catch(NoSuchMethodException e) { | 38 } catch(NoSuchMethodException e) { |
40 throw new RuntimeException(e); | 39 throw new RuntimeException(e); |
41 } | 40 } |
42 this.ioModule = (LuanTable)luan.loaded().get("Io"); | |
43 env.put("Io",ioModule); | |
44 | 41 |
45 this.out = out; | 42 this.out = out; |
46 } | 43 } |
47 | 44 |
48 public byte[] _read_binary(int size) throws IOException, LuanException { | 45 public byte[] _read_binary(int size) throws IOException, LuanException { |
56 } | 53 } |
57 return a; | 54 return a; |
58 } | 55 } |
59 | 56 |
60 public Object read() throws IOException, LuanException { | 57 public Object read() throws IOException, LuanException { |
61 ioModule.put("_read_binary",_read_binary); | 58 env.put("_read_binary",_read_binary); |
62 try { | 59 try { |
63 src = in.readUTF(); | 60 src = in.readUTF(); |
64 LuanFunction fn = BasicLuan.load(luan,src,"pickle-reader",env,false); | 61 LuanFunction fn = BasicLuan.load(luan,src,"pickle-reader",env,false); |
65 return luan.call(fn); | 62 return luan.call(fn); |
66 } finally { | 63 } finally { |
67 ioModule.put("_binaries",null); | 64 env.put("_binaries",null); |
68 ioModule.put("_read_binary",null); | 65 env.put("_read_binary",null); |
69 } | 66 } |
70 } | 67 } |
71 | 68 |
72 public String pickle(Object obj) throws LuanException { | 69 public String pickle(Object obj) throws LuanException { |
73 if( obj == null ) | 70 if( obj == null ) |
81 if( obj instanceof LuanTable ) | 78 if( obj instanceof LuanTable ) |
82 return pickle( (LuanTable)obj, Collections.newSetFromMap(new IdentityHashMap<LuanTable,Boolean>()) ); | 79 return pickle( (LuanTable)obj, Collections.newSetFromMap(new IdentityHashMap<LuanTable,Boolean>()) ); |
83 if( obj instanceof byte[] ) { | 80 if( obj instanceof byte[] ) { |
84 byte[] a = (byte[])obj; | 81 byte[] a = (byte[])obj; |
85 binaries.add(a); | 82 binaries.add(a); |
86 return "Io._binaries[" + binaries.size() + "]"; | 83 return "_binaries[" + binaries.size() + "]"; |
87 } | 84 } |
88 throw luan.exception( "invalid type: " + obj.getClass() ); | 85 throw luan.exception( "invalid type: " + obj.getClass() ); |
89 } | 86 } |
90 | 87 |
91 private String pickle(Object obj,Set<LuanTable> set) throws LuanException { | 88 private String pickle(Object obj,Set<LuanTable> set) throws LuanException { |
110 } | 107 } |
111 | 108 |
112 public void write(Object... args) throws LuanException, IOException { | 109 public void write(Object... args) throws LuanException, IOException { |
113 StringBuilder sb = new StringBuilder(); | 110 StringBuilder sb = new StringBuilder(); |
114 if( !binaries.isEmpty() ) { | 111 if( !binaries.isEmpty() ) { |
115 sb.append( "Io._binaries = {}\n" ); | 112 sb.append( "_binaries = {}\n" ); |
116 for( byte[] a : binaries ) { | 113 for( byte[] a : binaries ) { |
117 sb.append( "Io._binaries[#Io._binaries+1] = Io._read_binary(" + a.length + ")\n" ); | 114 sb.append( "_binaries[#_binaries+1] = _read_binary(" + a.length + ")\n" ); |
118 } | 115 } |
119 } | 116 } |
120 for( Object obj : args ) { | 117 for( Object obj : args ) { |
121 sb.append( luan.toString(obj) ); | 118 sb.append( luan.toString(obj) ); |
122 } | 119 } |