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 import "dart:collection" show HashMap; | 5 import "dart:collection" show HashMap; |
6 | 6 |
7 patch class ReceivePort { | 7 patch class ReceivePort { |
8 /* patch */ factory ReceivePort() = _ReceivePortImpl; | 8 /* patch */ factory ReceivePort() = _ReceivePortImpl; |
9 | 9 |
10 /* patch */ factory ReceivePort.fromRawReceivePort(RawReceivePort rawPort) = | 10 /* patch */ factory ReceivePort.fromRawReceivePort(RawReceivePort rawPort) = |
11 _ReceivePortImpl.fromRawReceivePort; | 11 _ReceivePortImpl.fromRawReceivePort; |
12 } | 12 } |
13 | 13 |
| 14 patch class Capability { |
| 15 /* patch */ factory Capability() { |
| 16 throw new UnimplementedError(); |
| 17 } |
| 18 } |
| 19 |
| 20 class _CapabilityImpl { |
| 21 factory _CapabilityImpl() native "CapabilityImpl_factory"; |
| 22 } |
| 23 |
14 patch class RawReceivePort { | 24 patch class RawReceivePort { |
15 /** | 25 /** |
16 * Opens a long-lived port for receiving messages. | 26 * Opens a long-lived port for receiving messages. |
17 * | 27 * |
18 * A [RawReceivePort] is low level and does not work with [Zone]s. It | 28 * A [RawReceivePort] is low level and does not work with [Zone]s. It |
19 * can not be paused. The data-handler must be set before the first | 29 * can not be paused. The data-handler must be set before the first |
20 * event is received. | 30 * event is received. |
21 */ | 31 */ |
22 /* patch */ factory RawReceivePort([void handler(event)]) { | 32 /* patch */ factory RawReceivePort([void handler(event)]) { |
23 _RawReceivePortImpl result = new _RawReceivePortImpl(); | 33 _RawReceivePortImpl result = new _RawReceivePortImpl(); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 /// The embedder can execute this function to get hold of | 82 /// The embedder can execute this function to get hold of |
73 /// [_isolateScheduleImmediate] above. | 83 /// [_isolateScheduleImmediate] above. |
74 Function _getIsolateScheduleImmediateClosure() { | 84 Function _getIsolateScheduleImmediateClosure() { |
75 return _isolateScheduleImmediate; | 85 return _isolateScheduleImmediate; |
76 } | 86 } |
77 | 87 |
78 class _RawReceivePortImpl implements RawReceivePort { | 88 class _RawReceivePortImpl implements RawReceivePort { |
79 factory _RawReceivePortImpl() native "RawReceivePortImpl_factory"; | 89 factory _RawReceivePortImpl() native "RawReceivePortImpl_factory"; |
80 | 90 |
81 close() { | 91 close() { |
82 _portMap.remove(_id); | 92 // Close the port and remove it from the handler map. |
83 _closeInternal(_id); | 93 _handlerMap.remove(this._closeInternal()); |
84 } | 94 } |
85 | 95 |
86 SendPort get sendPort { | 96 SendPort get sendPort { |
87 return new _SendPortImpl._from(this); | 97 return _get_sendport(); |
| 98 } |
| 99 |
| 100 bool operator==(var other) { |
| 101 return (other is _RawReceivePortImpl) && |
| 102 (this._get_id() == other._get_id()); |
| 103 } |
| 104 |
| 105 int get hashCode { |
| 106 return sendPort.hashCode(); |
88 } | 107 } |
89 | 108 |
90 /**** Internal implementation details ****/ | 109 /**** Internal implementation details ****/ |
91 // Called from the VM to create a new RawReceivePort instance. | 110 _get_id() native "RawReceivePortImpl_get_id"; |
92 static _RawReceivePortImpl _get(int id) { | 111 _get_sendport() native "RawReceivePortImpl_get_sendport"; |
93 return _portMap[id]; | |
94 } | |
95 | 112 |
96 static _RawReceivePortImpl _create(int id) { | 113 // Called from the VM to retrieve the handler for a message. |
97 assert(_portMap[id]== null); | 114 static _lookupHandler(int id) { |
98 return new _RawReceivePortImpl._internal(id); | 115 var result = _handlerMap[id]; |
99 } | 116 return result; |
100 | |
101 _RawReceivePortImpl._internal(int id) : _id = id { | |
102 _portMap[id] = this; | |
103 } | |
104 | |
105 // Called from the VM to retrieve the RawReceivePort for a message. | |
106 static _RawReceivePortImpl _lookupReceivePort(int id) { | |
107 return _portMap[id]; | |
108 } | 117 } |
109 | 118 |
110 // Called from the VM to dispatch to the handler. | 119 // Called from the VM to dispatch to the handler. |
111 static void _handleMessage(_RawReceivePortImpl port, var message) { | 120 static void _handleMessage(Function handler, var message) { |
112 assert(port != null); | |
113 // TODO(floitsch): this relies on the fact that any exception aborts the | 121 // TODO(floitsch): this relies on the fact that any exception aborts the |
114 // VM. Once we have non-fatal global exceptions we need to catch errors | 122 // VM. Once we have non-fatal global exceptions we need to catch errors |
115 // so that we can run the immediate callbacks. | 123 // so that we can run the immediate callbacks. |
116 port._handler(message); | 124 handler(message); |
117 if (_pendingImmediateCallback != null) { | 125 if (_pendingImmediateCallback != null) { |
118 var callback = _pendingImmediateCallback; | 126 var callback = _pendingImmediateCallback; |
119 _pendingImmediateCallback = null; | 127 _pendingImmediateCallback = null; |
120 callback(); | 128 callback(); |
121 } | 129 } |
122 } | 130 } |
123 | 131 |
124 // Call into the VM to close the VM maintained mappings. | 132 // Call into the VM to close the VM maintained mappings. |
125 static _closeInternal(int id) native "RawReceivePortImpl_closeInternal"; | 133 _closeInternal() native "RawReceivePortImpl_closeInternal"; |
126 | 134 |
127 void set handler(Function newHandler) { | 135 void set handler(Function value) { |
128 this._handler = newHandler; | 136 _handlerMap[this._get_id()] = value; |
129 } | 137 } |
130 | 138 |
131 final int _id; | 139 // TODO(iposva): Ideally keep this map in the VM. |
132 Function _handler; | 140 // id to handler mapping. |
133 | 141 static final Map _handlerMap = new HashMap(); |
134 // id to RawReceivePort mapping. | |
135 static final Map _portMap = new HashMap(); | |
136 } | 142 } |
137 | 143 |
138 | 144 |
139 class _SendPortImpl implements SendPort { | 145 class _SendPortImpl implements SendPort { |
140 /*--- public interface ---*/ | 146 /*--- public interface ---*/ |
141 void send(var message) { | 147 void send(var message) { |
142 _sendInternal(_id, message); | 148 _sendInternal(message); |
143 } | 149 } |
144 | 150 |
145 bool operator==(var other) { | 151 bool operator==(var other) { |
146 return (other is _SendPortImpl) && _id == other._id; | 152 return (other is _SendPortImpl) && (this._get_id() == other._get_id()); |
147 } | 153 } |
148 | 154 |
149 int get hashCode { | 155 int get hashCode { |
150 const int MASK = 0x3FFFFFFF; | 156 return _get_hashcode(); |
151 int hash = _id; | |
152 hash = (hash + ((hash & (MASK >> 10)) << 10)) & MASK; | |
153 hash ^= (hash >> 6); | |
154 hash = (hash + ((hash & (MASK >> 3)) << 3)) & MASK; | |
155 hash ^= (hash >> 11); | |
156 hash = (hash + ((hash & (MASK >> 15)) << 15)) & MASK; | |
157 return hash; | |
158 } | 157 } |
159 | 158 |
160 /*--- private implementation ---*/ | 159 /*--- private implementation ---*/ |
161 _SendPortImpl._from(_RawReceivePortImpl from) : _id = from._id; | 160 _get_id() native "SendPortImpl_get_id"; |
162 _SendPortImpl._with(int id) : _id = id; | 161 _get_hashcode() native "SendPortImpl_get_hashcode"; |
163 | 162 |
164 // _SendPortImpl._create is called from the VM when a new SendPort instance is | 163 // Forward the implementation of sending messages to the VM. |
165 // needed by the VM code. | 164 void _sendInternal(var message) native "SendPortImpl_sendInternal_"; |
166 static SendPort _create(int id) { | |
167 return new _SendPortImpl._with(id); | |
168 } | |
169 | |
170 // Forward the implementation of sending messages to the VM. Only port ids | |
171 // are being handed to the VM. | |
172 static _sendInternal(int sendId, var message) | |
173 native "SendPortImpl_sendInternal_"; | |
174 | |
175 final int _id; | |
176 } | 165 } |
177 | 166 |
178 typedef _MainFunction(); | 167 typedef _MainFunction(); |
179 typedef _MainFunctionArgs(args); | 168 typedef _MainFunctionArgs(args); |
180 typedef _MainFunctionArgsMessage(args, message); | 169 typedef _MainFunctionArgsMessage(args, message); |
181 | 170 |
182 /** | 171 /** |
183 * Takes the real entry point as argument and invokes it with the initial | 172 * Takes the real entry point as argument and invokes it with the initial |
184 * message. | 173 * message. |
185 * | 174 * |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 } | 259 } |
271 | 260 |
272 static final RawReceivePort _self = _mainPort; | 261 static final RawReceivePort _self = _mainPort; |
273 static RawReceivePort get _mainPort native "Isolate_mainPort"; | 262 static RawReceivePort get _mainPort native "Isolate_mainPort"; |
274 | 263 |
275 static SendPort _spawnFunction(Function topLevelFunction) | 264 static SendPort _spawnFunction(Function topLevelFunction) |
276 native "Isolate_spawnFunction"; | 265 native "Isolate_spawnFunction"; |
277 | 266 |
278 static SendPort _spawnUri(String uri) native "Isolate_spawnUri"; | 267 static SendPort _spawnUri(String uri) native "Isolate_spawnUri"; |
279 } | 268 } |
280 | |
281 patch class Capability { | |
282 /* patch */ factory Capability() { | |
283 throw new UnimplementedError(); | |
284 } | |
285 } | |
OLD | NEW |