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) = |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
50 | 50 |
51 close() { | 51 close() { |
52 _rawPort.close(); | 52 _rawPort.close(); |
53 _controller.close(); | 53 _controller.close(); |
54 } | 54 } |
55 | 55 |
56 final RawReceivePort _rawPort; | 56 final RawReceivePort _rawPort; |
57 StreamController _controller; | 57 StreamController _controller; |
58 } | 58 } |
59 | 59 |
60 typedef void ImmediateCallback(); | |
61 | |
62 /// The callback that has been registered through `scheduleImmediate`. | |
63 ImmediateCallback _pendingImmediateCallback; | |
64 | |
65 /// The closure that should be used as scheduleImmediateClosure, when the VM | |
66 /// is responsible for the event loop. | |
67 void _isolateScheduleImmediate(void callback()) { | |
68 assert(_pendingImmediateCallback == null); | |
69 _pendingImmediateCallback = callback; | |
70 } | |
71 | |
72 /// Provide closurized access to _isolateScheduleImmediate. | |
73 /// This makes it easier for embedders to set the scheduleImmediate closure. | |
74 Function get _isolateScheduleImmediateClosure => _isolateScheduleImmediate; | |
75 | |
76 /// The embedder can execute this function to get hold of | |
77 /// [_isolateScheduleImmediate] above. | |
78 Function _getIsolateScheduleImmediateClosure() { | |
79 return _isolateScheduleImmediate; | |
80 } | |
81 | |
60 class _RawReceivePortImpl implements RawReceivePort { | 82 class _RawReceivePortImpl implements RawReceivePort { |
61 factory _RawReceivePortImpl() native "RawReceivePortImpl_factory"; | 83 factory _RawReceivePortImpl() native "RawReceivePortImpl_factory"; |
62 | 84 |
63 close() { | 85 close() { |
64 _portMap.remove(_id); | 86 _portMap.remove(_id); |
65 _closeInternal(_id); | 87 _closeInternal(_id); |
66 } | 88 } |
67 | 89 |
68 SendPort get sendPort { | 90 SendPort get sendPort { |
69 return new _SendPortImpl(_id); | 91 return new _SendPortImpl(_id); |
(...skipping 15 matching lines...) Expand all Loading... | |
85 | 107 |
86 // Called from the VM to retrieve the RawReceivePort for a message. | 108 // Called from the VM to retrieve the RawReceivePort for a message. |
87 static _RawReceivePortImpl _lookupReceivePort(int id) { | 109 static _RawReceivePortImpl _lookupReceivePort(int id) { |
88 return _portMap[id]; | 110 return _portMap[id]; |
89 } | 111 } |
90 | 112 |
91 // Called from the VM to dispatch to the handler. | 113 // Called from the VM to dispatch to the handler. |
92 static void _handleMessage( | 114 static void _handleMessage( |
93 _RawReceivePortImpl port, int replyId, var message) { | 115 _RawReceivePortImpl port, int replyId, var message) { |
94 assert(port != null); | 116 assert(port != null); |
117 // TODO(floitsch): this relies on the fact that any exception aborts the | |
Lasse Reichstein Nielsen
2013/11/27 11:41:28
Bugnumber for catching uncaught exceptions?
floitsch
2013/11/27 13:57:06
I have added 8875.
| |
118 // VM. Once we have non-fatal global exceptions we need to catch errors | |
119 // so that we can run the immediate callbacks. | |
95 port._handler(message); | 120 port._handler(message); |
121 if (_pendingImmediateCallback != null) { | |
122 var callback = _pendingImmediateCallback; | |
123 _pendingImmediateCallback = null; | |
124 callback(); | |
125 } | |
96 } | 126 } |
97 | 127 |
98 // Call into the VM to close the VM maintained mappings. | 128 // Call into the VM to close the VM maintained mappings. |
99 static _closeInternal(int id) native "RawReceivePortImpl_closeInternal"; | 129 static _closeInternal(int id) native "RawReceivePortImpl_closeInternal"; |
100 | 130 |
101 void set handler(Function newHandler) { | 131 void set handler(Function newHandler) { |
102 this._handler = newHandler; | 132 this._handler = newHandler; |
103 } | 133 } |
104 | 134 |
105 final int _id; | 135 final int _id; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 // The VM will invoke [_startIsolate] with entryPoint as argument. | 238 // The VM will invoke [_startIsolate] with entryPoint as argument. |
209 SendPort controlPort = _spawnFunction(entryPoint); | 239 SendPort controlPort = _spawnFunction(entryPoint); |
210 RawReceivePort readyPort = new RawReceivePort(); | 240 RawReceivePort readyPort = new RawReceivePort(); |
211 controlPort.send([readyPort.sendPort, message]); | 241 controlPort.send([readyPort.sendPort, message]); |
212 readyPort.handler = (readyMessage) { | 242 readyPort.handler = (readyMessage) { |
213 assert(readyMessage == 'started'); | 243 assert(readyMessage == 'started'); |
214 readyPort.close(); | 244 readyPort.close(); |
215 completer.complete(new Isolate._fromControlPort(controlPort)); | 245 completer.complete(new Isolate._fromControlPort(controlPort)); |
216 }; | 246 }; |
217 } catch(e, st) { | 247 } catch(e, st) { |
218 // TODO(14718): we want errors to go into the returned future. | 248 // Don't complete immediately with an error, since the caller didn't |
219 rethrow; | 249 // yet have the time to install an error handler. |
250 scheduleMicrotask(() { | |
251 completer.completeError(e, st); | |
Lasse Reichstein Nielsen
2013/11/27 11:41:28
Could we add this to the head of the queue instead
floitsch
2013/11/27 13:57:06
The current implementation is consistent with a no
| |
252 }); | |
220 }; | 253 }; |
221 return completer.future; | 254 return completer.future; |
222 } | 255 } |
223 | 256 |
224 /* patch */ static Future<Isolate> spawnUri( | 257 /* patch */ static Future<Isolate> spawnUri( |
225 Uri uri, List<String> args, var message) { | 258 Uri uri, List<String> args, var message) { |
226 Completer completer = new Completer<Isolate>.sync(); | 259 Completer completer = new Completer<Isolate>.sync(); |
227 try { | 260 try { |
228 // The VM will invoke [_startIsolate] and not `main`. | 261 // The VM will invoke [_startIsolate] and not `main`. |
229 SendPort controlPort = _spawnUri(uri.toString()); | 262 SendPort controlPort = _spawnUri(uri.toString()); |
230 RawReceivePort readyPort = new RawReceivePort(); | 263 RawReceivePort readyPort = new RawReceivePort(); |
231 controlPort.send([readyPort.sendPort, args, message]); | 264 controlPort.send([readyPort.sendPort, args, message]); |
232 readyPort.handler = (readyMessage) { | 265 readyPort.handler = (readyMessage) { |
233 assert(readyMessage == 'started'); | 266 assert(readyMessage == 'started'); |
234 readyPort.close(); | 267 readyPort.close(); |
235 completer.complete(new Isolate._fromControlPort(controlPort)); | 268 completer.complete(new Isolate._fromControlPort(controlPort)); |
236 }; | 269 }; |
237 } catch(e, st) { | 270 } catch(e, st) { |
238 // TODO(14718): we want errors to go into the returned future. | 271 // Don't complete immediately with an error, since the caller didn't |
239 rethrow; | 272 // yet have the time to install an error handler. |
273 scheduleMicrotask(() { | |
274 completer.completeError(e, st); | |
275 }); | |
240 }; | 276 }; |
241 return completer.future; | 277 return completer.future; |
242 } | 278 } |
243 | 279 |
244 static final RawReceivePort _self = _mainPort; | 280 static final RawReceivePort _self = _mainPort; |
245 static RawReceivePort get _mainPort native "Isolate_mainPort"; | 281 static RawReceivePort get _mainPort native "Isolate_mainPort"; |
246 | 282 |
247 static SendPort _spawnFunction(Function topLevelFunction) | 283 static SendPort _spawnFunction(Function topLevelFunction) |
248 native "Isolate_spawnFunction"; | 284 native "Isolate_spawnFunction"; |
249 | 285 |
250 static SendPort _spawnUri(String uri) native "Isolate_spawnUri"; | 286 static SendPort _spawnUri(String uri) native "Isolate_spawnUri"; |
251 } | 287 } |
OLD | NEW |