OLD | NEW |
1 // Copyright (c) 2012, 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 // Defines message visitors, serialization, and deserialization. | 5 // Defines message visitors, serialization, and deserialization. |
6 | 6 |
7 /** Serialize [message] (or simulate serialization). */ | 7 /** Serialize [message] (or simulate serialization). */ |
8 _serializeMessage(message) { | 8 _serializeMessage(message) { |
9 if (_globalState.needSerialization) { | 9 if (_globalState.needSerialization) { |
10 return new _Serializer().traverse(message); | 10 return new _Serializer().traverse(message); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 return _getAttachedInfo(o); | 63 return _getAttachedInfo(o); |
64 } | 64 } |
65 | 65 |
66 _dispatch(var x) { | 66 _dispatch(var x) { |
67 if (isPrimitive(x)) return visitPrimitive(x); | 67 if (isPrimitive(x)) return visitPrimitive(x); |
68 if (x is List) return visitList(x); | 68 if (x is List) return visitList(x); |
69 if (x is Map) return visitMap(x); | 69 if (x is Map) return visitMap(x); |
70 if (x is _NativeJsSendPort) return visitNativeJsSendPort(x); | 70 if (x is _NativeJsSendPort) return visitNativeJsSendPort(x); |
71 if (x is _WorkerSendPort) return visitWorkerSendPort(x); | 71 if (x is _WorkerSendPort) return visitWorkerSendPort(x); |
72 if (x is _BufferingSendPort) return visitBufferingSendPort(x); | 72 if (x is _BufferingSendPort) return visitBufferingSendPort(x); |
73 if (x is _ReceivePortImpl) return visitReceivePort(x); | |
74 if (x is _ReceivePortSingleShotImpl) return visitReceivePortSingleShot(x); | |
75 // TODO(floitsch): make this a real exception. (which one)? | 73 // TODO(floitsch): make this a real exception. (which one)? |
76 throw "Message serialization: Illegal value $x passed"; | 74 throw "Message serialization: Illegal value $x passed"; |
77 } | 75 } |
78 | 76 |
79 abstract visitPrimitive(x); | 77 abstract visitPrimitive(x); |
80 abstract visitList(List x); | 78 abstract visitList(List x); |
81 abstract visitMap(Map x); | 79 abstract visitMap(Map x); |
82 abstract visitNativeJsSendPort(_NativeJsSendPort x); | 80 abstract visitNativeJsSendPort(_NativeJsSendPort x); |
83 abstract visitWorkerSendPort(_WorkerSendPort x); | 81 abstract visitWorkerSendPort(_WorkerSendPort x); |
84 abstract visitBufferingSendPort(_BufferingSendPort x); | 82 abstract visitBufferingSendPort(_BufferingSendPort x); |
85 abstract visitReceivePort(_ReceivePortImpl x); | |
86 abstract visitReceivePortSingleShot(_ReceivePortSingleShotImpl x); | |
87 | 83 |
88 _clearAttachedInfo(var o) native | 84 _clearAttachedInfo(var o) native |
89 "o['__MessageTraverser__attached_info__'] = (void 0);"; | 85 "o['__MessageTraverser__attached_info__'] = (void 0);"; |
90 | 86 |
91 _setAttachedInfo(var o, var info) native | 87 _setAttachedInfo(var o, var info) native |
92 "o['__MessageTraverser__attached_info__'] = info;"; | 88 "o['__MessageTraverser__attached_info__'] = info;"; |
93 | 89 |
94 _getAttachedInfo(var o) native | 90 _getAttachedInfo(var o) native |
95 "return o['__MessageTraverser__attached_info__'];"; | 91 "return o['__MessageTraverser__attached_info__'];"; |
96 | 92 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 | 147 |
152 SendPort visitBufferingSendPort(_BufferingSendPort port) { | 148 SendPort visitBufferingSendPort(_BufferingSendPort port) { |
153 if (port._port != null) { | 149 if (port._port != null) { |
154 return _visitNativeOrWorkerPort(port._port); | 150 return _visitNativeOrWorkerPort(port._port); |
155 } else { | 151 } else { |
156 // TODO(floitsch): Use real exception (which one?). | 152 // TODO(floitsch): Use real exception (which one?). |
157 throw "internal error: must call _waitForPendingPorts to ensure all" | 153 throw "internal error: must call _waitForPendingPorts to ensure all" |
158 + " ports are resolved at this point."; | 154 + " ports are resolved at this point."; |
159 } | 155 } |
160 } | 156 } |
161 | |
162 SendPort visitReceivePort(_ReceivePortImpl port) { | |
163 return port.toSendPort(); | |
164 } | |
165 | |
166 SendPort visitReceivePortSingleShot(_ReceivePortSingleShotImpl port) { | |
167 return port.toSendPort(); | |
168 } | |
169 } | 157 } |
170 | 158 |
171 /** Visitor that serializes a message as a JSON array. */ | 159 /** Visitor that serializes a message as a JSON array. */ |
172 class _Serializer extends _MessageTraverser { | 160 class _Serializer extends _MessageTraverser { |
173 int _nextFreeRefId = 0; | 161 int _nextFreeRefId = 0; |
174 | 162 |
175 _Serializer() : super(); | 163 _Serializer() : super(); |
176 | 164 |
177 visitPrimitive(x) => x; | 165 visitPrimitive(x) => x; |
178 | 166 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 visitBufferingSendPort(_BufferingSendPort port) { | 199 visitBufferingSendPort(_BufferingSendPort port) { |
212 if (port._port != null) { | 200 if (port._port != null) { |
213 return _visitNativeOrWorkerPort(port._port); | 201 return _visitNativeOrWorkerPort(port._port); |
214 } else { | 202 } else { |
215 // TODO(floitsch): Use real exception (which one?). | 203 // TODO(floitsch): Use real exception (which one?). |
216 throw "internal error: must call _waitForPendingPorts to ensure all" | 204 throw "internal error: must call _waitForPendingPorts to ensure all" |
217 + " ports are resolved at this point."; | 205 + " ports are resolved at this point."; |
218 } | 206 } |
219 } | 207 } |
220 | 208 |
221 visitReceivePort(_ReceivePortImpl port) { | |
222 return visitNativeJsSendPort(port.toSendPort());; | |
223 } | |
224 | |
225 visitReceivePortSingleShot(_ReceivePortSingleShotImpl port) { | |
226 return visitNativeJsSendPort(port.toSendPort()); | |
227 } | |
228 | |
229 _serializeList(List list) { | 209 _serializeList(List list) { |
230 int len = list.length; | 210 int len = list.length; |
231 var result = new List(len); | 211 var result = new List(len); |
232 for (int i = 0; i < len; i++) { | 212 for (int i = 0; i < len; i++) { |
233 result[i] = _dispatch(list[i]); | 213 result[i] = _dispatch(list[i]); |
234 } | 214 } |
235 return result; | 215 return result; |
236 } | 216 } |
237 } | 217 } |
238 | 218 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 return new _Copier().traverse(x); | 305 return new _Copier().traverse(x); |
326 } | 306 } |
327 | 307 |
328 // only visible for testing purposes | 308 // only visible for testing purposes |
329 static serialize(x) { | 309 static serialize(x) { |
330 _Serializer serializer = new _Serializer(); | 310 _Serializer serializer = new _Serializer(); |
331 _Deserializer deserializer = new _Deserializer(); | 311 _Deserializer deserializer = new _Deserializer(); |
332 return deserializer.deserialize(serializer.traverse(x)); | 312 return deserializer.deserialize(serializer.traverse(x)); |
333 } | 313 } |
334 } | 314 } |
OLD | NEW |