Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Abstract visitor for dart objects that can be passed as messages between any | 6 * Abstract visitor for dart objects that can be passed as messages between any |
| 7 * isolates. | 7 * isolates. |
| 8 */ | 8 */ |
| 9 class MessageTraverser { | 9 class MessageTraverser { |
| 10 static bool isPrimitive(x) { | 10 static bool isPrimitive(x) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 | 43 |
| 44 /** Retrieves any information stored in the native object [o]. */ | 44 /** Retrieves any information stored in the native object [o]. */ |
| 45 _getInfo(var o) { | 45 _getInfo(var o) { |
| 46 return _getAttachedInfo(o); | 46 return _getAttachedInfo(o); |
| 47 } | 47 } |
| 48 | 48 |
| 49 _dispatch(var x) { | 49 _dispatch(var x) { |
| 50 if (isPrimitive(x)) return visitPrimitive(x); | 50 if (isPrimitive(x)) return visitPrimitive(x); |
| 51 if (x is List) return visitList(x); | 51 if (x is List) return visitList(x); |
| 52 if (x is Map) return visitMap(x); | 52 if (x is Map) return visitMap(x); |
| 53 if (x is SendPortImpl) return visitSendPort(x); | 53 if (x is NativeJsSendPort) return visitNativeJsSendPort(x); |
| 54 if (x is WorkerSendPort) return visitWorkerSendPort(x); | |
| 55 if (x is BufferingSendPort) return visitBufferingSendPort(x); | |
| 54 if (x is ReceivePortImpl) return visitReceivePort(x); | 56 if (x is ReceivePortImpl) return visitReceivePort(x); |
| 55 if (x is ReceivePortSingleShotImpl) return visitReceivePortSingleShot(x); | 57 if (x is ReceivePortSingleShotImpl) return visitReceivePortSingleShot(x); |
| 56 // TODO(floitsch): make this a real exception. (which one)? | 58 // TODO(floitsch): make this a real exception. (which one)? |
| 57 throw "Message serialization: Illegal value $x passed"; | 59 throw "Message serialization: Illegal value $x passed"; |
| 58 } | 60 } |
| 59 | 61 |
| 60 abstract visitPrimitive(x); | 62 abstract visitPrimitive(x); |
| 61 abstract visitList(List x); | 63 abstract visitList(List x); |
| 62 abstract visitMap(Map x); | 64 abstract visitMap(Map x); |
| 63 abstract visitSendPort(SendPortImpl x); | 65 abstract visitNativeJsSendPort(NativeJsSendPort x); |
| 66 abstract visitWorkerSendPort(WorkerSendPort x); | |
| 67 abstract visitBufferingSendPort(BufferingSendPort x); | |
| 64 abstract visitReceivePort(ReceivePortImpl x); | 68 abstract visitReceivePort(ReceivePortImpl x); |
| 65 abstract visitReceivePortSingleShot(ReceivePortSingleShotImpl x); | 69 abstract visitReceivePortSingleShot(ReceivePortSingleShotImpl x); |
| 66 | 70 |
| 67 List _taggedObjects; | 71 List _taggedObjects; |
| 68 | 72 |
| 69 _clearAttachedInfo(var o) native | 73 _clearAttachedInfo(var o) native |
| 70 "o['__MessageTraverser__attached_info__'] = (void 0);"; | 74 "o['__MessageTraverser__attached_info__'] = (void 0);"; |
| 71 | 75 |
| 72 _setAttachedInfo(var o, var info) native | 76 _setAttachedInfo(var o, var info) native |
| 73 "o['__MessageTraverser__attached_info__'] = info;"; | 77 "o['__MessageTraverser__attached_info__'] = info;"; |
| 74 | 78 |
| 75 _getAttachedInfo(var o) native | 79 _getAttachedInfo(var o) native |
| 76 "return o['__MessageTraverser__attached_info__'];"; | 80 "return o['__MessageTraverser__attached_info__'];"; |
| 81 | |
| 82 _visitNativeOrWorkerPort(SendPort p) { | |
| 83 if (p is NativeJsSendPort) return visitNativeJsSendPort(p); | |
| 84 if (p is WorkerSendPort) return visitWorkerSendPort(p); | |
| 85 throw "Illegal underlying port $p"; | |
| 86 } | |
| 77 } | 87 } |
| 78 | 88 |
| 79 /** A visitor that recursively copies a message. */ | 89 /** A visitor that recursively copies a message. */ |
| 80 class Copier extends MessageTraverser { | 90 class Copier extends MessageTraverser { |
| 81 Copier() : super(); | 91 Copier() : super(); |
| 82 | 92 |
| 83 visitPrimitive(x) => x; | 93 visitPrimitive(x) => x; |
| 84 | 94 |
| 85 List visitList(List list) { | 95 List visitList(List list) { |
| 86 List copy = _getInfo(list); | 96 List copy = _getInfo(list); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 103 | 113 |
| 104 // TODO(floitsch): we loose the generic type of the map. | 114 // TODO(floitsch): we loose the generic type of the map. |
| 105 copy = new Map(); | 115 copy = new Map(); |
| 106 _attachInfo(map, copy); | 116 _attachInfo(map, copy); |
| 107 map.forEach((key, val) { | 117 map.forEach((key, val) { |
| 108 copy[_dispatch(key)] = _dispatch(val); | 118 copy[_dispatch(key)] = _dispatch(val); |
| 109 }); | 119 }); |
| 110 return copy; | 120 return copy; |
| 111 } | 121 } |
| 112 | 122 |
| 113 SendPort visitSendPort(SendPortImpl port) { | 123 SendPort visitNativeJsSendPort(NativeJsSendPort port) { |
| 114 return new SendPortImpl(port._workerId, | 124 return new NativeJsSendPort(port._receivePort, port._isolateId); |
| 115 port._isolateId, | 125 } |
| 116 port._receivePortId); | 126 |
| 127 SendPort visitWorkerSendPort(WorkerSendPort port) { | |
| 128 return new WorkerSendPort( | |
| 129 port._workerId, port._isolateId, port._receivePortId); | |
| 130 } | |
| 131 | |
| 132 SendPort visitBufferingSendPort(BufferingSendPort port) { | |
| 133 if (port._port != null) { | |
| 134 return _visitNativeOrWorkerPort(port._port); | |
| 135 } else { | |
| 136 // TODO(floitsch): Use real exception (which one?). | |
| 137 throw "interal error: must call _waitForPendingPorts to ensure all" | |
|
eub
2012/02/10 22:46:19
"internal"
| |
| 138 + " ports are resolved at this point."; | |
| 139 } | |
| 117 } | 140 } |
| 118 | 141 |
| 119 SendPort visitReceivePort(ReceivePortImpl port) { | 142 SendPort visitReceivePort(ReceivePortImpl port) { |
| 120 return port.toSendPort(); | 143 return port.toSendPort(); |
| 121 } | 144 } |
| 122 | 145 |
| 123 SendPort visitReceivePortSingleShot(ReceivePortSingleShotImpl port) { | 146 SendPort visitReceivePortSingleShot(ReceivePortSingleShotImpl port) { |
| 124 return port.toSendPort(); | 147 return port.toSendPort(); |
| 125 } | 148 } |
| 126 } | 149 } |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 147 if (copyId !== null) return ['ref', copyId]; | 170 if (copyId !== null) return ['ref', copyId]; |
| 148 | 171 |
| 149 int id = _nextFreeRefId++; | 172 int id = _nextFreeRefId++; |
| 150 _attachInfo(map, id); | 173 _attachInfo(map, id); |
| 151 var keys = _serializeList(map.getKeys()); | 174 var keys = _serializeList(map.getKeys()); |
| 152 var values = _serializeList(map.getValues()); | 175 var values = _serializeList(map.getValues()); |
| 153 // TODO(floitsch): we are losing the generic type. | 176 // TODO(floitsch): we are losing the generic type. |
| 154 return ['map', id, keys, values]; | 177 return ['map', id, keys, values]; |
| 155 } | 178 } |
| 156 | 179 |
| 157 visitSendPort(SendPortImpl port) { | 180 visitNativeJsSendPort(NativeJsSendPort port) { |
| 181 return ['sendport', _globalState.currentWorkerId, | |
| 182 port._isolateId, port._receivePort._id]; | |
| 183 } | |
| 184 | |
| 185 visitWorkerSendPort(WorkerSendPort port) { | |
| 158 return ['sendport', port._workerId, port._isolateId, port._receivePortId]; | 186 return ['sendport', port._workerId, port._isolateId, port._receivePortId]; |
| 159 } | 187 } |
| 160 | 188 |
| 189 SendPort visitBufferingSendPort(BufferingSendPort port) { | |
| 190 if (port._port != null) { | |
| 191 return _visitNativeOrWorkerPort(port._port); | |
| 192 } else { | |
| 193 // TODO(floitsch): Use real exception (which one?). | |
| 194 throw "interal error: must call _waitForPendingPorts to ensure all" | |
| 195 + " ports are resolved at this point."; | |
| 196 } | |
| 197 } | |
| 198 | |
| 161 visitReceivePort(ReceivePortImpl port) { | 199 visitReceivePort(ReceivePortImpl port) { |
| 162 return visitSendPort(port.toSendPort());; | 200 return visitNativeJsSendPort(port.toSendPort());; |
| 163 } | 201 } |
| 164 | 202 |
| 165 visitReceivePortSingleShot(ReceivePortSingleShotImpl port) { | 203 visitReceivePortSingleShot(ReceivePortSingleShotImpl port) { |
| 166 return visitSendPort(port.toSendPort()); | 204 return visitNativeJsSendPort(port.toSendPort()); |
| 167 } | 205 } |
| 168 | 206 |
| 169 _serializeList(List list) { | 207 _serializeList(List list) { |
| 170 int len = list.length; | 208 int len = list.length; |
| 171 var result = new List(len); | 209 var result = new List(len); |
| 172 for (int i = 0; i < len; i++) { | 210 for (int i = 0; i < len; i++) { |
| 173 result[i] = _dispatch(list[i]); | 211 result[i] = _dispatch(list[i]); |
| 174 } | 212 } |
| 175 return result; | 213 return result; |
| 176 } | 214 } |
| 177 | 215 |
| 178 int _nextFreeRefId = 0; | 216 int _nextFreeRefId = 0; |
| 179 } | 217 } |
| 180 | 218 |
| 219 /** Visitor that finds all unresolved [SendPort]s in a message. */ | |
| 220 class PendingSendPortFinder extends MessageTraverser { | |
| 221 List<Future<SendPort>> ports; | |
| 222 PendingSendPortFinder() : super(), ports = []; | |
| 223 | |
| 224 visitPrimitive(x) {} | |
| 225 visitNativeJsSendPort(NativeJsSendPort port) {} | |
| 226 visitWorkerSendPort(WorkerSendPort port) {} | |
| 227 visitReceivePort(ReceivePortImpl port) {} | |
| 228 visitReceivePortSingleShot(ReceivePortSingleShotImpl port) {} | |
| 229 | |
| 230 visitList(List list) { | |
| 231 final visited = _getInfo(list); | |
| 232 if (visited !== null) return; | |
| 233 _attachInfo(list, true); | |
| 234 list.forEach(_dispatch); | |
| 235 } | |
| 236 | |
| 237 visitMap(Map map) { | |
| 238 final visited = _getInfo(map); | |
| 239 if (visited !== null) return; | |
| 240 | |
| 241 _attachInfo(map, true); | |
| 242 map.getValues().forEach(_dispatch); | |
| 243 } | |
| 244 | |
| 245 visitBufferingSendPort(BufferingSendPort port) { | |
| 246 if (port._port == null) { | |
| 247 ports.add(port._futurePort); | |
| 248 } | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 | |
| 181 /** Deserializes arrays created with [Serializer]. */ | 253 /** Deserializes arrays created with [Serializer]. */ |
| 182 class Deserializer { | 254 class Deserializer { |
| 183 Deserializer(); | 255 Deserializer(); |
| 184 | 256 |
| 185 static bool isPrimitive(x) { | 257 static bool isPrimitive(x) { |
| 186 return (x === null) || (x is String) || (x is num) || (x is bool); | 258 return (x === null) || (x is String) || (x is num) || (x is bool); |
| 187 } | 259 } |
| 188 | 260 |
| 189 deserialize(x) { | 261 deserialize(x) { |
| 190 if (isPrimitive(x)) return x; | 262 if (isPrimitive(x)) return x; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 var value = _deserializeHelper(values[i]); | 310 var value = _deserializeHelper(values[i]); |
| 239 result[key] = value; | 311 result[key] = value; |
| 240 } | 312 } |
| 241 return result; | 313 return result; |
| 242 } | 314 } |
| 243 | 315 |
| 244 SendPort _deserializeSendPort(List x) { | 316 SendPort _deserializeSendPort(List x) { |
| 245 int workerId = x[1]; | 317 int workerId = x[1]; |
| 246 int isolateId = x[2]; | 318 int isolateId = x[2]; |
| 247 int receivePortId = x[3]; | 319 int receivePortId = x[3]; |
| 248 return new SendPortImpl(workerId, isolateId, receivePortId); | 320 if (workerId == _globalState.currentWorkerId) { |
| 321 var isolate = _globalState.isolates[isolateId]; | |
| 322 if (isolate == null) return null; // Isolate has been closed. | |
| 323 var receivePort = isolate.lookup(receivePortId); | |
| 324 return new NativeJsSendPort(receivePort, isolateId); | |
| 325 } else { | |
| 326 return new WorkerSendPort(workerId, isolateId, receivePortId); | |
| 327 } | |
| 249 } | 328 } |
| 250 | 329 |
| 251 // TODO(floitsch): this should by Map<int, var> or Map<int, Dynamic>. | 330 // TODO(floitsch): this should by Map<int, var> or Map<int, Dynamic>. |
| 252 Map<int, Dynamic> _deserialized; | 331 Map<int, Dynamic> _deserialized; |
| 253 } | 332 } |
| OLD | NEW |