| OLD | NEW | 
|---|
| 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 class ReceivePortFactory { | 5 class _ReceivePortFactory { | 
| 6   factory ReceivePort() { | 6   factory ReceivePort() { | 
| 7     return new ReceivePortImpl(); | 7     return new _ReceivePortImpl(); | 
| 8   } | 8   } | 
| 9 | 9 | 
| 10   factory ReceivePort.singleShot() { | 10   factory ReceivePort.singleShot() { | 
| 11     return new ReceivePortSingleShotImpl(); | 11     return new _ReceivePortSingleShotImpl(); | 
| 12   } | 12   } | 
| 13 } | 13 } | 
| 14 | 14 | 
| 15 | 15 | 
| 16 class ReceivePortImpl implements ReceivePort { | 16 class _ReceivePortImpl implements ReceivePort { | 
| 17   /*--- public interface ---*/ | 17   /*--- public interface ---*/ | 
| 18   factory ReceivePortImpl() native "ReceivePortImpl_factory"; | 18   factory _ReceivePortImpl() native "ReceivePortImpl_factory"; | 
| 19 | 19 | 
| 20   receive(void onMessage(var message, SendPort replyTo)) { | 20   receive(void onMessage(var message, SendPort replyTo)) { | 
| 21     _onMessage = onMessage; | 21     _onMessage = onMessage; | 
| 22   } | 22   } | 
| 23 | 23 | 
| 24   close() { | 24   close() { | 
| 25     _portMap.remove(_id); | 25     _portMap.remove(_id); | 
| 26     _closeInternal(_id); | 26     _closeInternal(_id); | 
| 27   } | 27   } | 
| 28 | 28 | 
| 29   SendPort toSendPort() { | 29   SendPort toSendPort() { | 
| 30     return new SendPortImpl(_id); | 30     return new _SendPortImpl(_id); | 
| 31   } | 31   } | 
| 32 | 32 | 
| 33   /**** Internal implementation details ****/ | 33   /**** Internal implementation details ****/ | 
| 34   // Called from the VM to create a new ReceivePort instance. | 34   // Called from the VM to create a new ReceivePort instance. | 
| 35   static ReceivePortImpl _get_or_create(int id) { | 35   static _ReceivePortImpl _get_or_create(int id) { | 
| 36     if (_portMap !== null) { | 36     if (_portMap !== null) { | 
| 37       ReceivePortImpl port = _portMap[id]; | 37       _ReceivePortImpl port = _portMap[id]; | 
| 38       if (port !== null) { | 38       if (port !== null) { | 
| 39         return port; | 39         return port; | 
| 40       } | 40       } | 
| 41     } | 41     } | 
| 42     return new ReceivePortImpl._internal(id); | 42     return new _ReceivePortImpl._internal(id); | 
| 43   } | 43   } | 
| 44   ReceivePortImpl._internal(int id) : _id = id { | 44   _ReceivePortImpl._internal(int id) : _id = id { | 
| 45     if (_portMap === null) { | 45     if (_portMap === null) { | 
| 46       _portMap = new Map(); | 46       _portMap = new Map(); | 
| 47     } | 47     } | 
| 48     _portMap[id] = this; | 48     _portMap[id] = this; | 
| 49   } | 49   } | 
| 50 | 50 | 
| 51   // Called from the VM to dispatch to the handler. | 51   // Called from the VM to dispatch to the handler. | 
| 52   static void _handleMessage(int id, int replyId, var message) { | 52   static void _handleMessage(int id, int replyId, var message) { | 
| 53     assert(_portMap !== null); | 53     assert(_portMap !== null); | 
| 54     ReceivePort port = _portMap[id]; | 54     ReceivePort port = _portMap[id]; | 
| 55     SendPort replyTo = (replyId == 0) ? null : new SendPortImpl(replyId); | 55     SendPort replyTo = (replyId == 0) ? null : new _SendPortImpl(replyId); | 
| 56     (port._onMessage)(message, replyTo); | 56     (port._onMessage)(message, replyTo); | 
| 57   } | 57   } | 
| 58 | 58 | 
| 59   // Call into the VM to close the VM maintained mappings. | 59   // Call into the VM to close the VM maintained mappings. | 
| 60   static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; | 60   static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; | 
| 61 | 61 | 
| 62   final int _id; | 62   final int _id; | 
| 63   var _onMessage; | 63   var _onMessage; | 
| 64 | 64 | 
| 65   // id to ReceivePort mapping. | 65   // id to ReceivePort mapping. | 
| 66   static Map _portMap; | 66   static Map _portMap; | 
| 67 } | 67 } | 
| 68 | 68 | 
| 69 | 69 | 
| 70 class ReceivePortSingleShotImpl implements ReceivePort { | 70 class _ReceivePortSingleShotImpl implements ReceivePort { | 
| 71 | 71 | 
| 72   ReceivePortSingleShotImpl() : _port = new ReceivePortImpl() { } | 72   _ReceivePortSingleShotImpl() : _port = new _ReceivePortImpl() { } | 
| 73 | 73 | 
| 74   void receive(void callback(var message, SendPort replyTo)) { | 74   void receive(void callback(var message, SendPort replyTo)) { | 
| 75     _port.receive((var message, SendPort replyTo) { | 75     _port.receive((var message, SendPort replyTo) { | 
| 76       _port.close(); | 76       _port.close(); | 
| 77       callback(message, replyTo); | 77       callback(message, replyTo); | 
| 78     }); | 78     }); | 
| 79   } | 79   } | 
| 80 | 80 | 
| 81   void close() { | 81   void close() { | 
| 82     _port.close(); | 82     _port.close(); | 
| 83   } | 83   } | 
| 84 | 84 | 
| 85   SendPort toSendPort() { | 85   SendPort toSendPort() { | 
| 86     return _port.toSendPort(); | 86     return _port.toSendPort(); | 
| 87   } | 87   } | 
| 88 | 88 | 
| 89   final ReceivePortImpl _port; | 89   final _ReceivePortImpl _port; | 
| 90 | 90 | 
| 91 } | 91 } | 
| 92 | 92 | 
| 93 | 93 | 
| 94 class SendPortImpl implements SendPort { | 94 class _SendPortImpl implements SendPort { | 
| 95   /*--- public interface ---*/ | 95   /*--- public interface ---*/ | 
| 96   void send(var message, [SendPort replyTo = null]) { | 96   void send(var message, [SendPort replyTo = null]) { | 
| 97     this._sendNow(message, replyTo); | 97     this._sendNow(message, replyTo); | 
| 98   } | 98   } | 
| 99 | 99 | 
| 100   void _sendNow(var message, SendPort replyTo) { | 100   void _sendNow(var message, SendPort replyTo) { | 
| 101     int replyId = (replyTo === null) ? 0 : replyTo._id; | 101     int replyId = (replyTo === null) ? 0 : replyTo._id; | 
| 102     _sendInternal(_id, replyId, message); | 102     _sendInternal(_id, replyId, message); | 
| 103   } | 103   } | 
| 104 | 104 | 
| 105   ReceivePortSingleShotImpl call(var message) { | 105   _ReceivePortSingleShotImpl call(var message) { | 
| 106     final result = new ReceivePortSingleShotImpl(); | 106     final result = new _ReceivePortSingleShotImpl(); | 
| 107     this.send(message, result.toSendPort()); | 107     this.send(message, result.toSendPort()); | 
| 108     return result; | 108     return result; | 
| 109   } | 109   } | 
| 110 | 110 | 
| 111   ReceivePortSingleShotImpl _callNow(var message) { | 111   _ReceivePortSingleShotImpl _callNow(var message) { | 
| 112     final result = new ReceivePortSingleShotImpl(); | 112     final result = new _ReceivePortSingleShotImpl(); | 
| 113     this._sendNow(message, result.toSendPort()); | 113     this._sendNow(message, result.toSendPort()); | 
| 114     return result; | 114     return result; | 
| 115   } | 115   } | 
| 116 | 116 | 
| 117   bool operator==(var other) { | 117   bool operator==(var other) { | 
| 118     return (other is SendPortImpl) && _id == other._id; | 118     return (other is _SendPortImpl) && _id == other._id; | 
| 119   } | 119   } | 
| 120 | 120 | 
| 121   int hashCode() { | 121   int hashCode() { | 
| 122     return _id; | 122     return _id; | 
| 123   } | 123   } | 
| 124 | 124 | 
| 125   /*--- private implementation ---*/ | 125   /*--- private implementation ---*/ | 
| 126   const SendPortImpl(int id) : _id = id; | 126   const _SendPortImpl(int id) : _id = id; | 
| 127 | 127 | 
| 128   // SendPortImpl._create is called from the VM when a new SendPort instance is | 128   // _SendPortImpl._create is called from the VM when a new SendPort instance is | 
| 129   // needed by the VM code. | 129   // needed by the VM code. | 
| 130   static SendPort _create(int id) { | 130   static SendPort _create(int id) { | 
| 131     return new SendPortImpl(id); | 131     return new _SendPortImpl(id); | 
| 132   } | 132   } | 
| 133 | 133 | 
| 134   // Forward the implementation of sending messages to the VM. Only port ids | 134   // Forward the implementation of sending messages to the VM. Only port ids | 
| 135   // are being handed to the VM. | 135   // are being handed to the VM. | 
| 136   static _sendInternal(int sendId, int replyId, var message) | 136   static _sendInternal(int sendId, int replyId, var message) | 
| 137       native "SendPortImpl_sendInternal_"; | 137       native "SendPortImpl_sendInternal_"; | 
| 138 | 138 | 
| 139   final int _id; | 139   final int _id; | 
| 140 } | 140 } | 
| 141 | 141 | 
| 142 | 142 class _IsolateNatives { | 
| 143 class IsolateNatives { |  | 
| 144   static Future<SendPort> spawn(Isolate isolate, bool isLight) { | 143   static Future<SendPort> spawn(Isolate isolate, bool isLight) { | 
| 145     Completer<SendPort> completer = new Completer<SendPort>(); | 144     Completer<SendPort> completer = new Completer<SendPort>(); | 
| 146     SendPort port = _start(isolate, isLight); | 145     SendPort port = _start(isolate, isLight); | 
| 147     completer.complete(port); | 146     completer.complete(port); | 
| 148     return completer.future; | 147     return completer.future; | 
| 149   } | 148   } | 
| 150 | 149 | 
| 151   // Starts a new isolate calling the run method on a new instance of the | 150   // Starts a new isolate calling the run method on a new instance of the | 
| 152   // remote class's type. | 151   // remote class's type. | 
| 153   // Returns the send port which is passed to the newly created isolate. | 152   // Returns the send port which is passed to the newly created isolate. | 
| 154   // This method is being dispatched to from the public core library code. | 153   // This method is being dispatched to from the public core library code. | 
| 155   static SendPort _start(Isolate isolate, bool light) | 154   static SendPort _start(Isolate isolate, bool light) | 
| 156       native "IsolateNatives_start"; | 155       native "IsolateNatives_start"; | 
| 157 } | 156 } | 
|  | 157 | 
|  | 158 class _IsolateFactory { | 
|  | 159 | 
|  | 160   factory Isolate2.fromCode(Function topLevelFunction) { | 
|  | 161     throw new NotImplementedException(); | 
|  | 162   } | 
|  | 163 | 
|  | 164   factory Isolate2.fromUri(String uri) { | 
|  | 165     throw new NotImplementedException(); | 
|  | 166   } | 
|  | 167 } | 
| OLD | NEW | 
|---|