| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 class _MessageTraverserVisitedMap { |
| 6 |
| 7 operator[](var object) => null; |
| 8 void operator[]=(var object, var info) { } |
| 9 |
| 10 void reset() { } |
| 11 void cleanup() { } |
| 12 |
| 13 } |
| 14 |
| 15 /** Abstract visitor for dart objects that can be sent as isolate messages. */ |
| 16 class _MessageTraverser { |
| 17 |
| 18 _MessageTraverserVisitedMap _visited; |
| 19 _MessageTraverser() : _visited = new _MessageTraverserVisitedMap(); |
| 20 |
| 21 /** Visitor's entry point. */ |
| 22 traverse(var x) { |
| 23 if (isPrimitive(x)) return visitPrimitive(x); |
| 24 _visited.reset(); |
| 25 var result; |
| 26 try { |
| 27 result = _dispatch(x); |
| 28 } finally { |
| 29 _visited.cleanup(); |
| 30 } |
| 31 return result; |
| 32 } |
| 33 |
| 34 _dispatch(var x) { |
| 35 if (isPrimitive(x)) return visitPrimitive(x); |
| 36 if (x is List) return visitList(x); |
| 37 if (x is Map) return visitMap(x); |
| 38 if (x is SendPort) return visitSendPort(x); |
| 39 if (x is SendPortSync) return visitSendPortSync(x); |
| 40 if (x is Function) return visitFunction(x); |
| 41 |
| 42 // TODO(floitsch): make this a real exception. (which one)? |
| 43 throw "Message serialization: Illegal value $x passed"; |
| 44 } |
| 45 |
| 46 abstract visitPrimitive(x); |
| 47 abstract visitList(List x); |
| 48 abstract visitMap(Map x); |
| 49 abstract visitSendPort(SendPort x); |
| 50 abstract visitSendPortSync(SendPortSync x); |
| 51 |
| 52 visitFunction(Function func) { |
| 53 throw "Serialization of functions is not allowed."; |
| 54 } |
| 55 |
| 56 static bool isPrimitive(x) { |
| 57 return (x === null) || (x is String) || (x is num) || (x is bool); |
| 58 } |
| 59 } |
| 60 |
| 61 |
| 62 /** A visitor that recursively copies a message. */ |
| 63 class _Copier extends _MessageTraverser { |
| 64 |
| 65 visitPrimitive(x) => x; |
| 66 |
| 67 List visitList(List list) { |
| 68 List copy = _visited[list]; |
| 69 if (copy !== null) return copy; |
| 70 |
| 71 int len = list.length; |
| 72 |
| 73 // TODO(floitsch): we loose the generic type of the List. |
| 74 copy = new List(len); |
| 75 _visited[list] = copy; |
| 76 for (int i = 0; i < len; i++) { |
| 77 copy[i] = _dispatch(list[i]); |
| 78 } |
| 79 return copy; |
| 80 } |
| 81 |
| 82 Map visitMap(Map map) { |
| 83 Map copy = _visited[map]; |
| 84 if (copy !== null) return copy; |
| 85 |
| 86 // TODO(floitsch): we loose the generic type of the map. |
| 87 copy = new Map(); |
| 88 _visited[map] = copy; |
| 89 map.forEach((key, val) { |
| 90 copy[_dispatch(key)] = _dispatch(val); |
| 91 }); |
| 92 return copy; |
| 93 } |
| 94 |
| 95 } |
| 96 |
| 97 /** Visitor that serializes a message as a JSON array. */ |
| 98 class _Serializer extends _MessageTraverser { |
| 99 int _nextFreeRefId = 0; |
| 100 |
| 101 visitPrimitive(x) => x; |
| 102 |
| 103 visitList(List list) { |
| 104 int copyId = _visited[list]; |
| 105 if (copyId !== null) return ['ref', copyId]; |
| 106 |
| 107 int id = _nextFreeRefId++; |
| 108 _visited[list] = id; |
| 109 var jsArray = _serializeList(list); |
| 110 // TODO(floitsch): we are losing the generic type. |
| 111 return ['list', id, jsArray]; |
| 112 } |
| 113 |
| 114 visitMap(Map map) { |
| 115 int copyId = _visited[map]; |
| 116 if (copyId !== null) return ['ref', copyId]; |
| 117 |
| 118 int id = _nextFreeRefId++; |
| 119 _visited[map] = id; |
| 120 var keys = _serializeList(map.getKeys()); |
| 121 var values = _serializeList(map.getValues()); |
| 122 // TODO(floitsch): we are losing the generic type. |
| 123 return ['map', id, keys, values]; |
| 124 } |
| 125 |
| 126 _serializeList(List list) { |
| 127 int len = list.length; |
| 128 var result = new List(len); |
| 129 for (int i = 0; i < len; i++) { |
| 130 result[i] = _dispatch(list[i]); |
| 131 } |
| 132 return result; |
| 133 } |
| 134 } |
| 135 |
| 136 /** Deserializes arrays created with [_Serializer]. */ |
| 137 class _Deserializer { |
| 138 Map<int, Dynamic> _deserialized; |
| 139 |
| 140 _Deserializer(); |
| 141 |
| 142 static bool isPrimitive(x) { |
| 143 return (x === null) || (x is String) || (x is num) || (x is bool); |
| 144 } |
| 145 |
| 146 deserialize(x) { |
| 147 if (isPrimitive(x)) return x; |
| 148 // TODO(floitsch): this should be new HashMap<int, var|Dynamic>() |
| 149 _deserialized = new HashMap(); |
| 150 return _deserializeHelper(x); |
| 151 } |
| 152 |
| 153 _deserializeHelper(x) { |
| 154 if (isPrimitive(x)) return x; |
| 155 assert(x is List); |
| 156 switch (x[0]) { |
| 157 case 'ref': return _deserializeRef(x); |
| 158 case 'list': return _deserializeList(x); |
| 159 case 'map': return _deserializeMap(x); |
| 160 case 'sendport': return deserializeSendPort(x); |
| 161 // TODO(floitsch): Use real exception (which one?). |
| 162 default: throw "Unexpected serialized object"; |
| 163 } |
| 164 } |
| 165 |
| 166 _deserializeRef(List x) { |
| 167 int id = x[1]; |
| 168 var result = _deserialized[id]; |
| 169 assert(result !== null); |
| 170 return result; |
| 171 } |
| 172 |
| 173 List _deserializeList(List x) { |
| 174 int id = x[1]; |
| 175 // We rely on the fact that Dart-lists are directly mapped to Js-arrays. |
| 176 List dartList = x[2]; |
| 177 _deserialized[id] = dartList; |
| 178 int len = dartList.length; |
| 179 for (int i = 0; i < len; i++) { |
| 180 dartList[i] = _deserializeHelper(dartList[i]); |
| 181 } |
| 182 return dartList; |
| 183 } |
| 184 |
| 185 Map _deserializeMap(List x) { |
| 186 Map result = new Map(); |
| 187 int id = x[1]; |
| 188 _deserialized[id] = result; |
| 189 List keys = x[2]; |
| 190 List values = x[3]; |
| 191 int len = keys.length; |
| 192 assert(len == values.length); |
| 193 for (int i = 0; i < len; i++) { |
| 194 var key = _deserializeHelper(keys[i]); |
| 195 var value = _deserializeHelper(values[i]); |
| 196 result[key] = value; |
| 197 } |
| 198 return result; |
| 199 } |
| 200 |
| 201 abstract deserializeSendPort(List x); |
| 202 |
| 203 } |
| OLD | NEW |