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 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 } else { | 263 } else { |
264 entryPoint(message); | 264 entryPoint(message); |
265 } | 265 } |
266 }; | 266 }; |
267 // Make sure the message handler is triggered. | 267 // Make sure the message handler is triggered. |
268 port.sendPort.send(null); | 268 port.sendPort.send(null); |
269 } | 269 } |
270 | 270 |
271 patch class Isolate { | 271 patch class Isolate { |
272 static final _currentIsolate = _getCurrentIsolate(); | 272 static final _currentIsolate = _getCurrentIsolate(); |
| 273 static final _rootUri = _getCurrentRootUri(); |
273 | 274 |
274 /* patch */ static Isolate get current => _currentIsolate; | 275 /* patch */ static Isolate get current => _currentIsolate; |
275 | 276 |
| 277 /* patch */ static Future<Uri> get packageRoot { |
| 278 var hook = VMLibraryHooks.packageRootUriFuture; |
| 279 if (hook == null) { |
| 280 throw new UnsupportedError("Isolate.packageRoot"); |
| 281 } |
| 282 return hook(); |
| 283 } |
| 284 |
| 285 /* patch */ static Future<Uri> get packageConfig { |
| 286 var hook = VMLibraryHooks.packageConfigUriFuture; |
| 287 if (hook == null) { |
| 288 throw new UnsupportedError("Isolate.packageConfig"); |
| 289 } |
| 290 return hook(); |
| 291 } |
| 292 |
| 293 /* patch */ static Future<Uri> resolvePackageUri(Uri packageUri) { |
| 294 var hook = VMLibraryHooks.resolvePackageUriFuture; |
| 295 if (hook == null) { |
| 296 throw new UnsupportedError("Isolate.resolvePackageUri"); |
| 297 } |
| 298 return hook(packageUri); |
| 299 } |
| 300 |
| 301 static bool _packageSupported() => |
| 302 (VMLibraryHooks.packageRootUriFuture != null) && |
| 303 (VMLibraryHooks.packageConfigUriFuture != null); |
| 304 |
276 /* patch */ static Future<Isolate> spawn( | 305 /* patch */ static Future<Isolate> spawn( |
277 void entryPoint(message), var message, | 306 void entryPoint(message), var message, |
278 {bool paused: false, bool errorsAreFatal, | 307 {bool paused: false, bool errorsAreFatal, |
279 SendPort onExit, SendPort onError}) { | 308 SendPort onExit, SendPort onError}) async { |
280 // `paused` isn't handled yet. | 309 // `paused` isn't handled yet. |
281 RawReceivePort readyPort; | 310 RawReceivePort readyPort; |
282 try { | 311 try { |
283 // The VM will invoke [_startIsolate] with entryPoint as argument. | 312 // The VM will invoke [_startIsolate] with entryPoint as argument. |
284 readyPort = new RawReceivePort(); | 313 readyPort = new RawReceivePort(); |
| 314 var packageRoot = null; |
| 315 var packageConfig = null; |
| 316 if (Isolate._packageSupported()) { |
| 317 packageRoot = (await Isolate.packageRoot)?.toString(); |
| 318 packageConfig = (await Isolate.packageConfig)?.toString(); |
| 319 } |
| 320 |
285 _spawnFunction(readyPort.sendPort, entryPoint, message, | 321 _spawnFunction(readyPort.sendPort, entryPoint, message, |
286 paused, errorsAreFatal, onExit, onError); | 322 paused, errorsAreFatal, onExit, onError, |
287 return _spawnCommon(readyPort); | 323 packageRoot, packageConfig); |
| 324 return await _spawnCommon(readyPort); |
288 } catch (e, st) { | 325 } catch (e, st) { |
289 if (readyPort != null) { | 326 if (readyPort != null) { |
290 readyPort.close(); | 327 readyPort.close(); |
291 } | 328 } |
292 return new Future<Isolate>.error(e, st); | 329 return await new Future<Isolate>.error(e, st); |
293 } | 330 } |
294 } | 331 } |
295 | 332 |
296 /* patch */ static Future<Isolate> spawnUri( | 333 /* patch */ static Future<Isolate> spawnUri( |
297 Uri uri, List<String> args, var message, | 334 Uri uri, List<String> args, var message, |
298 {bool paused: false, | 335 {bool paused: false, |
299 SendPort onExit, | 336 SendPort onExit, |
300 SendPort onError, | 337 SendPort onError, |
301 bool errorsAreFatal, | 338 bool errorsAreFatal, |
302 bool checked, | 339 bool checked, |
303 Map<String, String> environment, | 340 Map<String, String> environment, |
304 Uri packageRoot}) { | 341 Uri packageRoot, |
| 342 Uri packageConfig, |
| 343 bool automaticPackageResolution: false}) async { |
305 RawReceivePort readyPort; | 344 RawReceivePort readyPort; |
306 if (environment != null) throw new UnimplementedError("environment"); | 345 if (environment != null) { |
| 346 throw new UnimplementedError("environment"); |
| 347 } |
| 348 |
| 349 // Verify that no mutually exclusive arguments have been passed. |
| 350 if (automaticPackageResolution) { |
| 351 if (packageRoot != null) { |
| 352 throw new ArgumentError("Cannot simultaneously request " |
| 353 "automaticPackageResolution and specify a" |
| 354 "packageRoot."); |
| 355 } |
| 356 if (packageConfig != null) { |
| 357 throw new ArgumentError("Cannot simultaneously request " |
| 358 "automaticPackageResolution and specify a" |
| 359 "packageConfig."); |
| 360 } |
| 361 } else { |
| 362 if ((packageRoot != null) && (packageConfig != null)) { |
| 363 throw new ArgumentError("Cannot simultaneously specify a " |
| 364 "packageRoot and a packageConfig."); |
| 365 } |
| 366 } |
307 try { | 367 try { |
| 368 // Resolve the uri agains the current isolate's root Uri first. |
| 369 var spawnedUri = _rootUri.resolveUri(uri); |
| 370 |
| 371 // Inherit this isolate's package resolution setup if not overridden. |
| 372 if (!automaticPackageResolution && |
| 373 (packageRoot == null) && |
| 374 (packageConfig == null)) { |
| 375 if (Isolate._packageSupported()) { |
| 376 packageRoot = await Isolate.packageRoot; |
| 377 packageConfig = await Isolate.packageConfig; |
| 378 } |
| 379 } |
| 380 |
308 // The VM will invoke [_startIsolate] and not `main`. | 381 // The VM will invoke [_startIsolate] and not `main`. |
309 readyPort = new RawReceivePort(); | 382 readyPort = new RawReceivePort(); |
310 var packageRootString = | 383 var packageRootString = packageRoot?.toString(); |
311 (packageRoot == null) ? null : packageRoot.toString(); | 384 var packageConfigString = packageConfig?.toString(); |
312 var packagesList = null; | |
313 | 385 |
314 _spawnUri(readyPort.sendPort, uri.toString(), | 386 _spawnUri(readyPort.sendPort, spawnedUri.toString(), |
315 args, message, | 387 args, message, |
316 paused, onExit, onError, | 388 paused, onExit, onError, |
317 errorsAreFatal, checked, | 389 errorsAreFatal, checked, |
318 null, /* environment */ | 390 null, /* environment */ |
319 packageRootString, packagesList); | 391 packageRootString, packageConfigString); |
320 return _spawnCommon(readyPort); | 392 return await _spawnCommon(readyPort); |
321 } catch (e, st) { | 393 } catch (e, st) { |
322 if (readyPort != null) { | 394 if (readyPort != null) { |
323 readyPort.close(); | 395 readyPort.close(); |
324 } | 396 } |
325 return new Future<Isolate>.error(e, st); | 397 return await new Future<Isolate>.error(e, st); |
326 } | 398 } |
327 } | 399 } |
328 | 400 |
329 static Future<Isolate> _spawnCommon(RawReceivePort readyPort) { | 401 static Future<Isolate> _spawnCommon(RawReceivePort readyPort) { |
330 Completer completer = new Completer<Isolate>.sync(); | 402 Completer completer = new Completer<Isolate>.sync(); |
331 readyPort.handler = (readyMessage) { | 403 readyPort.handler = (readyMessage) { |
332 readyPort.close(); | 404 readyPort.close(); |
333 if (readyMessage is List && readyMessage.length == 2) { | 405 if (readyMessage is List && readyMessage.length == 2) { |
334 SendPort controlPort = readyMessage[0]; | 406 SendPort controlPort = readyMessage[0]; |
335 List capabilities = readyMessage[1]; | 407 List capabilities = readyMessage[1]; |
(...skipping 23 matching lines...) Expand all Loading... |
359 static const _KILL = 4; | 431 static const _KILL = 4; |
360 static const _ADD_EXIT = 5; | 432 static const _ADD_EXIT = 5; |
361 static const _DEL_EXIT = 6; | 433 static const _DEL_EXIT = 6; |
362 static const _ADD_ERROR = 7; | 434 static const _ADD_ERROR = 7; |
363 static const _DEL_ERROR = 8; | 435 static const _DEL_ERROR = 8; |
364 static const _ERROR_FATAL = 9; | 436 static const _ERROR_FATAL = 9; |
365 | 437 |
366 | 438 |
367 static void _spawnFunction(SendPort readyPort, Function topLevelFunction, | 439 static void _spawnFunction(SendPort readyPort, Function topLevelFunction, |
368 var message, bool paused, bool errorsAreFatal, | 440 var message, bool paused, bool errorsAreFatal, |
369 SendPort onExit, SendPort onError) | 441 SendPort onExit, SendPort onError, |
| 442 String packageRoot, String packageConfig) |
370 native "Isolate_spawnFunction"; | 443 native "Isolate_spawnFunction"; |
371 | 444 |
372 static void _spawnUri(SendPort readyPort, String uri, | 445 static void _spawnUri(SendPort readyPort, String uri, |
373 List<String> args, var message, | 446 List<String> args, var message, |
374 bool paused, SendPort onExit, SendPort onError, | 447 bool paused, SendPort onExit, SendPort onError, |
375 bool errorsAreFatal, bool checked, | 448 bool errorsAreFatal, bool checked, |
376 List environment, | 449 List environment, |
377 String packageRoot, List packages) | 450 String packageRoot, String packageConfig) |
378 native "Isolate_spawnUri"; | 451 native "Isolate_spawnUri"; |
379 | 452 |
380 static void _sendOOB(port, msg) native "Isolate_sendOOB"; | 453 static void _sendOOB(port, msg) native "Isolate_sendOOB"; |
381 | 454 |
382 /* patch */ void _pause(Capability resumeCapability) { | 455 /* patch */ void _pause(Capability resumeCapability) { |
383 var msg = new List(4) | 456 var msg = new List(4) |
384 ..[0] = 0 // Make room for OOB message type. | 457 ..[0] = 0 // Make room for OOB message type. |
385 ..[1] = _PAUSE | 458 ..[1] = _PAUSE |
386 ..[2] = pauseCapability | 459 ..[2] = pauseCapability |
387 ..[3] = resumeCapability; | 460 ..[3] = resumeCapability; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 | 535 |
463 static Isolate _getCurrentIsolate() { | 536 static Isolate _getCurrentIsolate() { |
464 List portAndCapabilities = _getPortAndCapabilitiesOfCurrentIsolate(); | 537 List portAndCapabilities = _getPortAndCapabilitiesOfCurrentIsolate(); |
465 return new Isolate(portAndCapabilities[0], | 538 return new Isolate(portAndCapabilities[0], |
466 pauseCapability: portAndCapabilities[1], | 539 pauseCapability: portAndCapabilities[1], |
467 terminateCapability: portAndCapabilities[2]); | 540 terminateCapability: portAndCapabilities[2]); |
468 } | 541 } |
469 | 542 |
470 static List _getPortAndCapabilitiesOfCurrentIsolate() | 543 static List _getPortAndCapabilitiesOfCurrentIsolate() |
471 native "Isolate_getPortAndCapabilitiesOfCurrentIsolate"; | 544 native "Isolate_getPortAndCapabilitiesOfCurrentIsolate"; |
| 545 |
| 546 static Uri _getCurrentRootUri() { |
| 547 try { |
| 548 return Uri.parse(_getCurrentRootUriStr()); |
| 549 } catch (e, s) { |
| 550 return null; |
| 551 } |
| 552 } |
| 553 |
| 554 static String _getCurrentRootUriStr() |
| 555 native "Isolate_getCurrentRootUriStr"; |
472 } | 556 } |
OLD | NEW |