Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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) { |
| 11 return (x === null) || (x is String) || (x is num) || (x is bool); | 11 return (x === null) || (x is String) || (x is num) || (x is bool); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 NativeJsSendPort) return visitNativeJsSendPort(x); | 53 if (x is NativeJsSendPort) return visitNativeJsSendPort(x); |
| 54 if (x is WorkerSendPort) return visitWorkerSendPort(x); | 54 if (x is WorkerSendPort) return visitWorkerSendPort(x); |
| 55 if (x is BufferingSendPort) return visitBufferingSendPort(x); | |
| 55 if (x is ReceivePortImpl) return visitReceivePort(x); | 56 if (x is ReceivePortImpl) return visitReceivePort(x); |
| 56 if (x is ReceivePortSingleShotImpl) return visitReceivePortSingleShot(x); | 57 if (x is ReceivePortSingleShotImpl) return visitReceivePortSingleShot(x); |
| 57 // TODO(floitsch): make this a real exception. (which one)? | 58 // TODO(floitsch): make this a real exception. (which one)? |
| 58 throw "Message serialization: Illegal value $x passed"; | 59 throw "Message serialization: Illegal value $x passed"; |
| 59 } | 60 } |
| 60 | 61 |
| 61 abstract visitPrimitive(x); | 62 abstract visitPrimitive(x); |
| 62 abstract visitList(List x); | 63 abstract visitList(List x); |
| 63 abstract visitMap(Map x); | 64 abstract visitMap(Map x); |
| 64 abstract visitNativeJsSendPort(NativeJsSendPort x); | 65 abstract visitNativeJsSendPort(NativeJsSendPort x); |
| 65 abstract visitWorkerSendPort(WorkerSendPort x); | 66 abstract visitWorkerSendPort(WorkerSendPort x); |
| 67 abstract visitBufferingSendPort(BufferingSendPort x); | |
| 66 abstract visitReceivePort(ReceivePortImpl x); | 68 abstract visitReceivePort(ReceivePortImpl x); |
| 67 abstract visitReceivePortSingleShot(ReceivePortSingleShotImpl x); | 69 abstract visitReceivePortSingleShot(ReceivePortSingleShotImpl x); |
| 68 | 70 |
| 69 List _taggedObjects; | 71 List _taggedObjects; |
| 70 | 72 |
| 71 _clearAttachedInfo(var o) native | 73 _clearAttachedInfo(var o) native |
| 72 "o['__MessageTraverser__attached_info__'] = (void 0);"; | 74 "o['__MessageTraverser__attached_info__'] = (void 0);"; |
| 73 | 75 |
| 74 _setAttachedInfo(var o, var info) native | 76 _setAttachedInfo(var o, var info) native |
| 75 "o['__MessageTraverser__attached_info__'] = info;"; | 77 "o['__MessageTraverser__attached_info__'] = info;"; |
| 76 | 78 |
| 77 _getAttachedInfo(var o) native | 79 _getAttachedInfo(var o) native |
| 78 "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 } | |
| 79 } | 87 } |
| 80 | 88 |
| 81 /** A visitor that recursively copies a message. */ | 89 /** A visitor that recursively copies a message. */ |
| 82 class Copier extends MessageTraverser { | 90 class Copier extends MessageTraverser { |
| 83 Copier() : super(); | 91 Copier() : super(); |
| 84 | 92 |
| 85 visitPrimitive(x) => x; | 93 visitPrimitive(x) => x; |
| 86 | 94 |
| 87 List visitList(List list) { | 95 List visitList(List list) { |
| 88 List copy = _getInfo(list); | 96 List copy = _getInfo(list); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 114 | 122 |
| 115 SendPort visitNativeJsSendPort(NativeJsSendPort port) { | 123 SendPort visitNativeJsSendPort(NativeJsSendPort port) { |
| 116 return new NativeJsSendPort(port._receivePort, port._isolateId); | 124 return new NativeJsSendPort(port._receivePort, port._isolateId); |
| 117 } | 125 } |
| 118 | 126 |
| 119 SendPort visitWorkerSendPort(WorkerSendPort port) { | 127 SendPort visitWorkerSendPort(WorkerSendPort port) { |
| 120 return new WorkerSendPort( | 128 return new WorkerSendPort( |
| 121 port._workerId, port._isolateId, port._receivePortId); | 129 port._workerId, port._isolateId, port._receivePortId); |
| 122 } | 130 } |
| 123 | 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"
and below
Siggi Cherem (dart-lang)
2012/02/10 23:42:22
Done.
| |
| 138 + " ports are resolved at this point."; | |
| 139 } | |
| 140 } | |
| 141 | |
| 124 SendPort visitReceivePort(ReceivePortImpl port) { | 142 SendPort visitReceivePort(ReceivePortImpl port) { |
| 125 return port.toSendPort(); | 143 return port.toSendPort(); |
| 126 } | 144 } |
| 127 | 145 |
| 128 SendPort visitReceivePortSingleShot(ReceivePortSingleShotImpl port) { | 146 SendPort visitReceivePortSingleShot(ReceivePortSingleShotImpl port) { |
| 129 return port.toSendPort(); | 147 return port.toSendPort(); |
| 130 } | 148 } |
| 131 } | 149 } |
| 132 | 150 |
| 133 /** Visitor that serializes a message as a JSON array. */ | 151 /** Visitor that serializes a message as a JSON array. */ |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 161 | 179 |
| 162 visitNativeJsSendPort(NativeJsSendPort port) { | 180 visitNativeJsSendPort(NativeJsSendPort port) { |
| 163 return ['sendport', _globalState.currentWorkerId, | 181 return ['sendport', _globalState.currentWorkerId, |
| 164 port._isolateId, port._receivePort._id]; | 182 port._isolateId, port._receivePort._id]; |
| 165 } | 183 } |
| 166 | 184 |
| 167 visitWorkerSendPort(WorkerSendPort port) { | 185 visitWorkerSendPort(WorkerSendPort port) { |
| 168 return ['sendport', port._workerId, port._isolateId, port._receivePortId]; | 186 return ['sendport', port._workerId, port._isolateId, port._receivePortId]; |
| 169 } | 187 } |
| 170 | 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 | |
| 171 visitReceivePort(ReceivePortImpl port) { | 199 visitReceivePort(ReceivePortImpl port) { |
| 172 return visitNativeJsSendPort(port.toSendPort());; | 200 return visitNativeJsSendPort(port.toSendPort());; |
| 173 } | 201 } |
| 174 | 202 |
| 175 visitReceivePortSingleShot(ReceivePortSingleShotImpl port) { | 203 visitReceivePortSingleShot(ReceivePortSingleShotImpl port) { |
| 176 return visitNativeJsSendPort(port.toSendPort()); | 204 return visitNativeJsSendPort(port.toSendPort()); |
| 177 } | 205 } |
| 178 | 206 |
| 179 _serializeList(List list) { | 207 _serializeList(List list) { |
| 180 int len = list.length; | 208 int len = list.length; |
| 181 var result = new List(len); | 209 var result = new List(len); |
| 182 for (int i = 0; i < len; i++) { | 210 for (int i = 0; i < len; i++) { |
| 183 result[i] = _dispatch(list[i]); | 211 result[i] = _dispatch(list[i]); |
| 184 } | 212 } |
| 185 return result; | 213 return result; |
| 186 } | 214 } |
| 187 | 215 |
| 188 int _nextFreeRefId = 0; | 216 int _nextFreeRefId = 0; |
| 189 } | 217 } |
| 190 | 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 | |
| 191 /** Deserializes arrays created with [Serializer]. */ | 253 /** Deserializes arrays created with [Serializer]. */ |
| 192 class Deserializer { | 254 class Deserializer { |
| 193 Deserializer(); | 255 Deserializer(); |
| 194 | 256 |
| 195 static bool isPrimitive(x) { | 257 static bool isPrimitive(x) { |
| 196 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); |
| 197 } | 259 } |
| 198 | 260 |
| 199 deserialize(x) { | 261 deserialize(x) { |
| 200 if (isPrimitive(x)) return x; | 262 if (isPrimitive(x)) return x; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 if (isolate == null) return null; // Isolate has been closed. | 324 if (isolate == null) return null; // Isolate has been closed. |
| 263 var receivePort = isolate.lookup(receivePortId); | 325 var receivePort = isolate.lookup(receivePortId); |
| 264 return new NativeJsSendPort(receivePort, isolateId); | 326 return new NativeJsSendPort(receivePort, isolateId); |
| 265 } else { | 327 } else { |
| 266 return new WorkerSendPort(workerId, isolateId, receivePortId); | 328 return new WorkerSendPort(workerId, isolateId, receivePortId); |
| 267 } | 329 } |
| 268 } | 330 } |
| 269 | 331 |
| 270 Map<int, Dynamic> _deserialized; | 332 Map<int, Dynamic> _deserialized; |
| 271 } | 333 } |
| OLD | NEW |