OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 library unittest.dart; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:io'; | |
9 import 'dart:isolate'; | |
10 | |
11 import 'package:path/path.dart' as p; | |
12 import 'package:stack_trace/stack_trace.dart'; | |
kevmoo
2015/02/12 22:31:15
unused import
nweiz
2015/02/12 22:37:52
Done.
| |
13 | |
14 import 'remote_exception.dart'; | |
kevmoo
2015/02/12 22:31:15
sort
nweiz
2015/02/12 22:37:51
Done.
| |
15 import 'io.dart'; | |
16 | |
17 /// Runs [code] in an isolate. | |
18 /// | |
19 /// [code] should be the contents of a Dart entrypoint. It may contain imports; | |
20 /// they will be resolved in the same context as the host isolate. [message] is | |
21 /// passed to the [main] method of the code being run; the caller is responsible | |
22 /// for using this to establish communication with the isolate. | |
23 /// | |
24 /// [packageRoot] controls the package root of the isolate. It may be either a | |
25 /// [String] or a [Uri]. | |
26 Future<Isolate> runInIsolate(String code, message, {packageRoot}) { | |
27 // TODO(nweiz): load code from a local server rather than from a file. | |
28 return withTempDir((dir) { | |
29 var dartPath = p.join(dir, 'runInIsolate.dart'); | |
30 new File(dartPath).writeAsStringSync(code); | |
31 var port = new ReceivePort(); | |
32 return Isolate.spawn(_isolateBuffer, { | |
33 'replyTo': port.sendPort, | |
34 'uri': p.toUri(dartPath).toString(), | |
35 'packageRoot': packageRoot == null ? null : packageRoot.toString(), | |
36 'message': message | |
37 }).then((isolate) { | |
38 return port.first.then((response) { | |
39 if (response['type'] != 'error') return isolate; | |
40 isolate.kill(); | |
41 var asyncError = RemoteException.deserialize(response['error']); | |
42 return new Future.error(asyncError.error, asyncError.stackTrace); | |
43 }); | |
44 }); | |
45 }); | |
46 } | |
47 | |
48 // TODO(nweiz): remove this when issue 12617 is fixed. | |
49 /// A function used as a buffer between the host isolate and [spawnUri]. | |
50 /// | |
51 /// [spawnUri] synchronously loads the file and its imports, which can deadlock | |
52 /// the host isolate if there's an HTTP import pointing at a server in the host. | |
53 /// Adding an additional isolate in the middle works around this. | |
54 void _isolateBuffer(message) { | |
55 var replyTo = message['replyTo']; | |
56 var packageRoot = message['packageRoot']; | |
57 if (packageRoot != null) packageRoot = Uri.parse(packageRoot); | |
58 Isolate.spawnUri(Uri.parse(message['uri']), [], message['message'], | |
59 packageRoot: packageRoot) | |
60 .then((_) => replyTo.send({'type': 'success'})) | |
61 .catchError((error, stackTrace) { | |
62 replyTo.send({ | |
63 'type': 'error', | |
64 'error': RemoteException.serialize(error, stackTrace) | |
65 }); | |
66 }); | |
67 } | |
OLD | NEW |