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