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 import "dart:_internal"; | 6 import "dart:_internal"; |
7 | 7 |
8 patch class ReceivePort { | 8 patch class ReceivePort { |
9 /* patch */ factory ReceivePort() = _ReceivePortImpl; | 9 /* patch */ factory ReceivePort() = _ReceivePortImpl; |
10 | 10 |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 void entryPoint(message), var message, | 277 void entryPoint(message), var message, |
278 {bool paused: false, bool errorsAreFatal, | 278 {bool paused: false, bool errorsAreFatal, |
279 SendPort onExit, SendPort onError}) { | 279 SendPort onExit, SendPort onError}) { |
280 // `paused` isn't handled yet. | 280 // `paused` isn't handled yet. |
281 RawReceivePort readyPort; | 281 RawReceivePort readyPort; |
282 try { | 282 try { |
283 // The VM will invoke [_startIsolate] with entryPoint as argument. | 283 // The VM will invoke [_startIsolate] with entryPoint as argument. |
284 readyPort = new RawReceivePort(); | 284 readyPort = new RawReceivePort(); |
285 _spawnFunction(readyPort.sendPort, entryPoint, message, | 285 _spawnFunction(readyPort.sendPort, entryPoint, message, |
286 paused, errorsAreFatal, onExit, onError); | 286 paused, errorsAreFatal, onExit, onError); |
287 Completer completer = new Completer<Isolate>.sync(); | 287 return _spawnCommon(readyPort); |
288 readyPort.handler = (readyMessage) { | |
289 readyPort.close(); | |
290 assert(readyMessage is List); | |
291 assert(readyMessage.length == 2); | |
292 SendPort controlPort = readyMessage[0]; | |
293 List capabilities = readyMessage[1]; | |
294 completer.complete(new Isolate(controlPort, | |
295 pauseCapability: capabilities[0], | |
296 terminateCapability: capabilities[1])); | |
297 }; | |
298 return completer.future; | |
299 } catch (e, st) { | 288 } catch (e, st) { |
300 if (readyPort != null) { | 289 if (readyPort != null) { |
301 readyPort.close(); | 290 readyPort.close(); |
302 } | 291 } |
303 return new Future<Isolate>.error(e, st); | 292 return new Future<Isolate>.error(e, st); |
304 } | 293 } |
305 } | 294 } |
306 | 295 |
307 /* patch */ static Future<Isolate> spawnUri( | 296 /* patch */ static Future<Isolate> spawnUri( |
308 Uri uri, List<String> args, var message, | 297 Uri uri, List<String> args, var message, |
(...skipping 12 matching lines...) Expand all Loading... |
321 var packageRootString = | 310 var packageRootString = |
322 (packageRoot == null) ? null : packageRoot.toString(); | 311 (packageRoot == null) ? null : packageRoot.toString(); |
323 var packagesList = null; | 312 var packagesList = null; |
324 | 313 |
325 _spawnUri(readyPort.sendPort, uri.toString(), | 314 _spawnUri(readyPort.sendPort, uri.toString(), |
326 args, message, | 315 args, message, |
327 paused, onExit, onError, | 316 paused, onExit, onError, |
328 errorsAreFatal, checked, | 317 errorsAreFatal, checked, |
329 null, /* environment */ | 318 null, /* environment */ |
330 packageRootString, packagesList); | 319 packageRootString, packagesList); |
331 Completer completer = new Completer<Isolate>.sync(); | 320 return _spawnCommon(readyPort); |
332 readyPort.handler = (readyMessage) { | |
333 readyPort.close(); | |
334 assert(readyMessage is List); | |
335 assert(readyMessage.length == 2); | |
336 SendPort controlPort = readyMessage[0]; | |
337 List capabilities = readyMessage[1]; | |
338 completer.complete(new Isolate(controlPort, | |
339 pauseCapability: capabilities[0], | |
340 terminateCapability: capabilities[1])); | |
341 }; | |
342 return completer.future; | |
343 } catch (e, st) { | 321 } catch (e, st) { |
344 if (readyPort != null) { | 322 if (readyPort != null) { |
345 readyPort.close(); | 323 readyPort.close(); |
346 } | 324 } |
347 return new Future<Isolate>.error(e, st); | 325 return new Future<Isolate>.error(e, st); |
348 } | 326 } |
| 327 } |
| 328 |
| 329 static Future<Isolate> _spawnCommon(RawReceivePort readyPort) { |
| 330 Completer completer = new Completer<Isolate>.sync(); |
| 331 readyPort.handler = (readyMessage) { |
| 332 readyPort.close(); |
| 333 if (readyMessage is List && readyMessage.length == 2) { |
| 334 SendPort controlPort = readyMessage[0]; |
| 335 List capabilities = readyMessage[1]; |
| 336 completer.complete(new Isolate(controlPort, |
| 337 pauseCapability: capabilities[0], |
| 338 terminateCapability: capabilities[1])); |
| 339 } else if (readyMessage is String) { |
| 340 // We encountered an error while starting the new isolate. |
| 341 completer.completeError(new IsolateSpawnException( |
| 342 'Unable to spawn isolate: ${readyMessage}')); |
| 343 } else { |
| 344 // This shouldn't happen. |
| 345 completer.completeError(new IsolateSpawnException( |
| 346 "Internal error: unexpected format for ready message: " |
| 347 "'${readyMessage}'")); |
| 348 } |
| 349 }; |
349 return completer.future; | 350 return completer.future; |
350 } | 351 } |
351 | 352 |
352 // TODO(iposva): Cleanup to have only one definition. | 353 // TODO(iposva): Cleanup to have only one definition. |
353 // These values need to be kept in sync with the class IsolateMessageHandler | 354 // These values need to be kept in sync with the class IsolateMessageHandler |
354 // in vm/isolate.cc. | 355 // in vm/isolate.cc. |
355 static const _PAUSE = 1; | 356 static const _PAUSE = 1; |
356 static const _RESUME = 2; | 357 static const _RESUME = 2; |
357 static const _PING = 3; | 358 static const _PING = 3; |
358 static const _KILL = 4; | 359 static const _KILL = 4; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 static Isolate _getCurrentIsolate() { | 463 static Isolate _getCurrentIsolate() { |
463 List portAndCapabilities = _getPortAndCapabilitiesOfCurrentIsolate(); | 464 List portAndCapabilities = _getPortAndCapabilitiesOfCurrentIsolate(); |
464 return new Isolate(portAndCapabilities[0], | 465 return new Isolate(portAndCapabilities[0], |
465 pauseCapability: portAndCapabilities[1], | 466 pauseCapability: portAndCapabilities[1], |
466 terminateCapability: portAndCapabilities[2]); | 467 terminateCapability: portAndCapabilities[2]); |
467 } | 468 } |
468 | 469 |
469 static List _getPortAndCapabilitiesOfCurrentIsolate() | 470 static List _getPortAndCapabilitiesOfCurrentIsolate() |
470 native "Isolate_getPortAndCapabilitiesOfCurrentIsolate"; | 471 native "Isolate_getPortAndCapabilitiesOfCurrentIsolate"; |
471 } | 472 } |
OLD | NEW |