| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 class _ReceivePortFactory { | |
| 6 factory ReceivePort() { | |
| 7 return new _ReceivePortImpl(); | |
| 8 } | |
| 9 } | |
| 10 | |
| 11 | |
| 12 class _ReceivePortImpl implements ReceivePort { | |
| 13 /*--- public interface ---*/ | |
| 14 factory _ReceivePortImpl() native "ReceivePortImpl_factory"; | |
| 15 | |
| 16 receive(void onMessage(var message, SendPort replyTo)) { | |
| 17 _onMessage = onMessage; | |
| 18 } | |
| 19 | |
| 20 close() { | |
| 21 _portMap.remove(_id); | |
| 22 _closeInternal(_id); | |
| 23 } | |
| 24 | |
| 25 SendPort toSendPort() { | |
| 26 return new _SendPortImpl(_id); | |
| 27 } | |
| 28 | |
| 29 /**** Internal implementation details ****/ | |
| 30 // Called from the VM to create a new ReceivePort instance. | |
| 31 static _ReceivePortImpl _get_or_create(int id) { | |
| 32 if (_portMap !== null) { | |
| 33 _ReceivePortImpl port = _portMap[id]; | |
| 34 if (port !== null) { | |
| 35 return port; | |
| 36 } | |
| 37 } | |
| 38 return new _ReceivePortImpl._internal(id); | |
| 39 } | |
| 40 _ReceivePortImpl._internal(int id) : _id = id { | |
| 41 if (_portMap === null) { | |
| 42 _portMap = new Map(); | |
| 43 } | |
| 44 _portMap[id] = this; | |
| 45 } | |
| 46 | |
| 47 // Called from the VM to dispatch to the handler. | |
| 48 static void _handleMessage(int id, int replyId, var message) { | |
| 49 assert(_portMap !== null); | |
| 50 ReceivePort port = _portMap[id]; | |
| 51 SendPort replyTo = (replyId == 0) ? null : new _SendPortImpl(replyId); | |
| 52 (port._onMessage)(message, replyTo); | |
| 53 } | |
| 54 | |
| 55 // Call into the VM to close the VM maintained mappings. | |
| 56 static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; | |
| 57 | |
| 58 final int _id; | |
| 59 var _onMessage; | |
| 60 | |
| 61 // id to ReceivePort mapping. | |
| 62 static Map _portMap; | |
| 63 } | |
| 64 | |
| 65 | |
| 66 class _SendPortImpl implements SendPort { | |
| 67 /*--- public interface ---*/ | |
| 68 void send(var message, [SendPort replyTo = null]) { | |
| 69 this._sendNow(message, replyTo); | |
| 70 } | |
| 71 | |
| 72 void _sendNow(var message, SendPort replyTo) { | |
| 73 int replyId = (replyTo === null) ? 0 : replyTo._id; | |
| 74 _sendInternal(_id, replyId, message); | |
| 75 } | |
| 76 | |
| 77 Future call(var message) { | |
| 78 final completer = new Completer(); | |
| 79 final port = new _ReceivePortImpl(); | |
| 80 send(message, port.toSendPort()); | |
| 81 port.receive((value, ignoreReplyTo) { | |
| 82 port.close(); | |
| 83 if (value is Exception) { | |
| 84 completer.completeException(value); | |
| 85 } else { | |
| 86 completer.complete(value); | |
| 87 } | |
| 88 }); | |
| 89 return completer.future; | |
| 90 } | |
| 91 | |
| 92 bool operator==(var other) { | |
| 93 return (other is _SendPortImpl) && _id == other._id; | |
| 94 } | |
| 95 | |
| 96 int hashCode() { | |
| 97 return _id; | |
| 98 } | |
| 99 | |
| 100 /*--- private implementation ---*/ | |
| 101 const _SendPortImpl(int id) : _id = id; | |
| 102 | |
| 103 // _SendPortImpl._create is called from the VM when a new SendPort instance is | |
| 104 // needed by the VM code. | |
| 105 static SendPort _create(int id) { | |
| 106 return new _SendPortImpl(id); | |
| 107 } | |
| 108 | |
| 109 // Forward the implementation of sending messages to the VM. Only port ids | |
| 110 // are being handed to the VM. | |
| 111 static _sendInternal(int sendId, int replyId, var message) | |
| 112 native "SendPortImpl_sendInternal_"; | |
| 113 | |
| 114 final int _id; | |
| 115 } | |
| 116 | |
| 117 _getPortInternal() native "isolate_getPortInternal"; | |
| 118 | |
| 119 ReceivePort _portInternal; | |
| 120 | |
| 121 ReceivePort get _port() { | |
| 122 if (_portInternal === null) { | |
| 123 _portInternal = _getPortInternal(); | |
| 124 } | |
| 125 return _portInternal; | |
| 126 } | |
| 127 | |
| 128 _spawnFunction(void topLevelFunction()) native "isolate_spawnFunction"; | |
| 129 | |
| 130 _spawnUri(String uri) native "isolate_spawnUri"; | |
| OLD | NEW |