| 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 patch class _ReceivePortFactory { | 5 patch class _ReceivePortFactory { |
| 6 /* patch */ factory ReceivePort() { | 6 /* patch */ factory ReceivePort() { |
| 7 return new _ReceivePortImpl(); | 7 return new _ReceivePortImpl(); |
| 8 } | 8 } |
| 9 } | 9 } |
| 10 | 10 |
| 11 class _ReceivePortImpl implements ReceivePort { | 11 class _ReceivePortImpl implements ReceivePort { |
| 12 factory _ReceivePortImpl() native "ReceivePortImpl_factory"; | 12 factory _ReceivePortImpl() native "ReceivePortImpl_factory"; |
| 13 | 13 |
| 14 receive(void onMessage(var message, SendPort replyTo)) { | 14 receive(void onMessage(var message, SendPort replyTo)) { |
| 15 _onMessage = onMessage; | 15 _onMessage = onMessage; |
| 16 } | 16 } |
| 17 | 17 |
| 18 close() { | 18 close() { |
| 19 _portMap.remove(_id); | 19 _portMap.remove(_id); |
| 20 _closeInternal(_id); | 20 _closeInternal(_id); |
| 21 } | 21 } |
| 22 | 22 |
| 23 SendPort toSendPort() { | 23 SendPort toSendPort() { |
| 24 return new _SendPortImpl(_id); | 24 return new _SendPortImpl(_id); |
| 25 } | 25 } |
| 26 | 26 |
| 27 /**** Internal implementation details ****/ | 27 /**** Internal implementation details ****/ |
| 28 // Called from the VM to create a new ReceivePort instance. | 28 // Called from the VM to create a new ReceivePort instance. |
| 29 static _ReceivePortImpl _get_or_create(int id) { | 29 static _ReceivePortImpl _get_or_create(int id) { |
| 30 if (_portMap !== null) { | 30 if (_portMap != null) { |
| 31 _ReceivePortImpl port = _portMap[id]; | 31 _ReceivePortImpl port = _portMap[id]; |
| 32 if (port !== null) { | 32 if (port != null) { |
| 33 return port; | 33 return port; |
| 34 } | 34 } |
| 35 } | 35 } |
| 36 return new _ReceivePortImpl._internal(id); | 36 return new _ReceivePortImpl._internal(id); |
| 37 } | 37 } |
| 38 _ReceivePortImpl._internal(int id) : _id = id { | 38 _ReceivePortImpl._internal(int id) : _id = id { |
| 39 if (_portMap === null) { | 39 if (_portMap == null) { |
| 40 _portMap = new Map(); | 40 _portMap = new Map(); |
| 41 } | 41 } |
| 42 _portMap[id] = this; | 42 _portMap[id] = this; |
| 43 } | 43 } |
| 44 | 44 |
| 45 // Called from the VM to dispatch to the handler. | 45 // Called from the VM to dispatch to the handler. |
| 46 static void _handleMessage(int id, int replyId, var message) { | 46 static void _handleMessage(int id, int replyId, var message) { |
| 47 assert(_portMap !== null); | 47 assert(_portMap != null); |
| 48 ReceivePort port = _portMap[id]; | 48 ReceivePort port = _portMap[id]; |
| 49 SendPort replyTo = (replyId == 0) ? null : new _SendPortImpl(replyId); | 49 SendPort replyTo = (replyId == 0) ? null : new _SendPortImpl(replyId); |
| 50 (port._onMessage)(message, replyTo); | 50 (port._onMessage)(message, replyTo); |
| 51 } | 51 } |
| 52 | 52 |
| 53 // Call into the VM to close the VM maintained mappings. | 53 // Call into the VM to close the VM maintained mappings. |
| 54 static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; | 54 static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; |
| 55 | 55 |
| 56 final int _id; | 56 final int _id; |
| 57 var _onMessage; | 57 var _onMessage; |
| 58 | 58 |
| 59 // id to ReceivePort mapping. | 59 // id to ReceivePort mapping. |
| 60 static Map _portMap; | 60 static Map _portMap; |
| 61 } | 61 } |
| 62 | 62 |
| 63 | 63 |
| 64 class _SendPortImpl implements SendPort { | 64 class _SendPortImpl implements SendPort { |
| 65 /*--- public interface ---*/ | 65 /*--- public interface ---*/ |
| 66 void send(var message, [SendPort replyTo = null]) { | 66 void send(var message, [SendPort replyTo = null]) { |
| 67 this._sendNow(message, replyTo); | 67 this._sendNow(message, replyTo); |
| 68 } | 68 } |
| 69 | 69 |
| 70 void _sendNow(var message, SendPort replyTo) { | 70 void _sendNow(var message, SendPort replyTo) { |
| 71 int replyId = (replyTo === null) ? 0 : replyTo._id; | 71 int replyId = (replyTo == null) ? 0 : replyTo._id; |
| 72 _sendInternal(_id, replyId, message); | 72 _sendInternal(_id, replyId, message); |
| 73 } | 73 } |
| 74 | 74 |
| 75 Future call(var message) { | 75 Future call(var message) { |
| 76 final completer = new Completer(); | 76 final completer = new Completer(); |
| 77 final port = new _ReceivePortImpl(); | 77 final port = new _ReceivePortImpl(); |
| 78 send(message, port.toSendPort()); | 78 send(message, port.toSendPort()); |
| 79 port.receive((value, ignoreReplyTo) { | 79 port.receive((value, ignoreReplyTo) { |
| 80 port.close(); | 80 port.close(); |
| 81 if (value is Exception) { | 81 if (value is Exception) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 110 native "SendPortImpl_sendInternal_"; | 110 native "SendPortImpl_sendInternal_"; |
| 111 | 111 |
| 112 final int _id; | 112 final int _id; |
| 113 } | 113 } |
| 114 | 114 |
| 115 _getPortInternal() native "isolate_getPortInternal"; | 115 _getPortInternal() native "isolate_getPortInternal"; |
| 116 | 116 |
| 117 ReceivePort _portInternal; | 117 ReceivePort _portInternal; |
| 118 | 118 |
| 119 patch ReceivePort get port { | 119 patch ReceivePort get port { |
| 120 if (_portInternal === null) { | 120 if (_portInternal == null) { |
| 121 _portInternal = _getPortInternal(); | 121 _portInternal = _getPortInternal(); |
| 122 } | 122 } |
| 123 return _portInternal; | 123 return _portInternal; |
| 124 } | 124 } |
| 125 | 125 |
| 126 patch spawnFunction(void topLevelFunction()) native "isolate_spawnFunction"; | 126 patch spawnFunction(void topLevelFunction()) native "isolate_spawnFunction"; |
| 127 | 127 |
| 128 patch spawnUri(String uri) native "isolate_spawnUri"; | 128 patch spawnUri(String uri) native "isolate_spawnUri"; |
| 129 | 129 |
| OLD | NEW |