Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: frog/lib/isolate_serialization.dart

Issue 9317068: isolate lib: small refactor to distinguish protocols at the port level (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: '' Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« frog/lib/isolate.dart ('K') | « frog/lib/isolate.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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);
54 if (x is ReceivePortImpl) return visitReceivePort(x); 55 if (x is ReceivePortImpl) return visitReceivePort(x);
55 if (x is ReceivePortSingleShotImpl) return visitReceivePortSingleShot(x); 56 if (x is ReceivePortSingleShotImpl) return visitReceivePortSingleShot(x);
56 // TODO(floitsch): make this a real exception. (which one)? 57 // TODO(floitsch): make this a real exception. (which one)?
57 throw "Message serialization: Illegal value $x passed"; 58 throw "Message serialization: Illegal value $x passed";
58 } 59 }
59 60
60 abstract visitPrimitive(x); 61 abstract visitPrimitive(x);
61 abstract visitList(List x); 62 abstract visitList(List x);
62 abstract visitMap(Map x); 63 abstract visitMap(Map x);
63 abstract visitSendPort(SendPortImpl x); 64 abstract visitNativeJsSendPort(NativeJsSendPort x);
65 abstract visitWorkerSendPort(WorkerSendPort x);
64 abstract visitReceivePort(ReceivePortImpl x); 66 abstract visitReceivePort(ReceivePortImpl x);
65 abstract visitReceivePortSingleShot(ReceivePortSingleShotImpl x); 67 abstract visitReceivePortSingleShot(ReceivePortSingleShotImpl x);
66 68
67 List _taggedObjects; 69 List _taggedObjects;
68 70
69 _clearAttachedInfo(var o) native 71 _clearAttachedInfo(var o) native
70 "o['__MessageTraverser__attached_info__'] = (void 0);"; 72 "o['__MessageTraverser__attached_info__'] = (void 0);";
71 73
72 _setAttachedInfo(var o, var info) native 74 _setAttachedInfo(var o, var info) native
73 "o['__MessageTraverser__attached_info__'] = info;"; 75 "o['__MessageTraverser__attached_info__'] = info;";
(...skipping 29 matching lines...) Expand all
103 105
104 // TODO(floitsch): we loose the generic type of the map. 106 // TODO(floitsch): we loose the generic type of the map.
105 copy = new Map(); 107 copy = new Map();
106 _attachInfo(map, copy); 108 _attachInfo(map, copy);
107 map.forEach((key, val) { 109 map.forEach((key, val) {
108 copy[_dispatch(key)] = _dispatch(val); 110 copy[_dispatch(key)] = _dispatch(val);
109 }); 111 });
110 return copy; 112 return copy;
111 } 113 }
112 114
113 SendPort visitSendPort(SendPortImpl port) { 115 SendPort visitNativeJsSendPort(NativeJsSendPort port) {
114 return new SendPortImpl(port._workerId, 116 return new NativeJsSendPort(port._receivePort, port._isolateId);
115 port._isolateId, 117 }
116 port._receivePortId); 118
119 SendPort visitWorkerSendPort(WorkerSendPort port) {
120 return new WorkerSendPort(
121 port._workerId, port._isolateId, port._receivePortId);
117 } 122 }
118 123
119 SendPort visitReceivePort(ReceivePortImpl port) { 124 SendPort visitReceivePort(ReceivePortImpl port) {
120 return port.toSendPort(); 125 return port.toSendPort();
121 } 126 }
122 127
123 SendPort visitReceivePortSingleShot(ReceivePortSingleShotImpl port) { 128 SendPort visitReceivePortSingleShot(ReceivePortSingleShotImpl port) {
124 return port.toSendPort(); 129 return port.toSendPort();
125 } 130 }
126 } 131 }
(...skipping 20 matching lines...) Expand all
147 if (copyId !== null) return ['ref', copyId]; 152 if (copyId !== null) return ['ref', copyId];
148 153
149 int id = _nextFreeRefId++; 154 int id = _nextFreeRefId++;
150 _attachInfo(map, id); 155 _attachInfo(map, id);
151 var keys = _serializeList(map.getKeys()); 156 var keys = _serializeList(map.getKeys());
152 var values = _serializeList(map.getValues()); 157 var values = _serializeList(map.getValues());
153 // TODO(floitsch): we are losing the generic type. 158 // TODO(floitsch): we are losing the generic type.
154 return ['map', id, keys, values]; 159 return ['map', id, keys, values];
155 } 160 }
156 161
157 visitSendPort(SendPortImpl port) { 162 visitNativeJsSendPort(NativeJsSendPort port) {
163 return ['sendport', _globalState.currentWorkerId,
164 port._isolateId, port._receivePort._id];
165 }
166
167 visitWorkerSendPort(WorkerSendPort port) {
158 return ['sendport', port._workerId, port._isolateId, port._receivePortId]; 168 return ['sendport', port._workerId, port._isolateId, port._receivePortId];
159 } 169 }
160 170
161 visitReceivePort(ReceivePortImpl port) { 171 visitReceivePort(ReceivePortImpl port) {
162 return visitSendPort(port.toSendPort());; 172 return visitNativeJsSendPort(port.toSendPort());;
163 } 173 }
164 174
165 visitReceivePortSingleShot(ReceivePortSingleShotImpl port) { 175 visitReceivePortSingleShot(ReceivePortSingleShotImpl port) {
166 return visitSendPort(port.toSendPort()); 176 return visitNativeJsSendPort(port.toSendPort());
167 } 177 }
168 178
169 _serializeList(List list) { 179 _serializeList(List list) {
170 int len = list.length; 180 int len = list.length;
171 var result = new List(len); 181 var result = new List(len);
172 for (int i = 0; i < len; i++) { 182 for (int i = 0; i < len; i++) {
173 result[i] = _dispatch(list[i]); 183 result[i] = _dispatch(list[i]);
174 } 184 }
175 return result; 185 return result;
176 } 186 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 var value = _deserializeHelper(values[i]); 248 var value = _deserializeHelper(values[i]);
239 result[key] = value; 249 result[key] = value;
240 } 250 }
241 return result; 251 return result;
242 } 252 }
243 253
244 SendPort _deserializeSendPort(List x) { 254 SendPort _deserializeSendPort(List x) {
245 int workerId = x[1]; 255 int workerId = x[1];
246 int isolateId = x[2]; 256 int isolateId = x[2];
247 int receivePortId = x[3]; 257 int receivePortId = x[3];
248 return new SendPortImpl(workerId, isolateId, receivePortId); 258 if (workerId == _globalState.currentWorkerId) {
eub 2012/02/09 01:13:42 Do we want to distinguish the serialization token
Siggi Cherem (dart-lang) 2012/02/10 00:16:41 The magic is actually important for two reasons: c
259 var isolate = _globalState.isolates[isolateId];
260 if (isolate == null) return null; // Isolate has been closed.
261 var receivePort = isolate.lookup(receivePortId);
262 return new NativeJsSendPort(receivePort, isolateId);
263 } else {
264 return new WorkerSendPort(workerId, isolateId, receivePortId);
265 }
249 } 266 }
250 267
251 // TODO(floitsch): this should by Map<int, var> or Map<int, Dynamic>. 268 // TODO(floitsch): this should by Map<int, var> or Map<int, Dynamic>.
252 Map<int, Dynamic> _deserialized; 269 Map<int, Dynamic> _deserialized;
253 } 270 }
OLDNEW
« frog/lib/isolate.dart ('K') | « frog/lib/isolate.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698