forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_module.dart
More file actions
48 lines (41 loc) · 1.5 KB
/
Copy patheval_module.dart
File metadata and controls
48 lines (41 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import "dart:isolate";
import "dart:async";
Uri toDartDataUri(String source) {
return Uri.parse("data:application/dart;charset=utf-8,"
"${Uri.encodeComponent(source)}");
}
createIsolateSource(String moduleSource, List<List<String>> moduleImports) {
var moduleSourceParts = [];
moduleImports.forEach((sourceImport) {
String modName = sourceImport[0];
String modAlias = sourceImport[1];
moduleSourceParts.add("import 'package:${modName}.dart' as ${modAlias};");
});
moduleSourceParts.add(moduleSource);
return """
import "dart:isolate";
import "${toDartDataUri(moduleSourceParts.join('\n'))}" as mut;
main(List args, SendPort replyPort) {
replyPort.send(mut.run(args));
}
""";
}
dynamic callModule(dynamic data) { return data.map( (a) => a+1); }
evalModule(String moduleSource, List<List<String>> moduleImports, List args) {
String source = createIsolateSource(moduleSource, moduleImports);
Completer completer = new Completer();
RawReceivePort receivePort;
receivePort = new RawReceivePort( (message) {
receivePort.close();
completer.complete(message);
});
var packageRoot = Uri.parse('/packages');
return Isolate.spawnUri(toDartDataUri(source), args, receivePort.sendPort, packageRoot: packageRoot).then( (isolate) {
RawReceivePort errorPort;
errorPort = new RawReceivePort( (message) {
completer.completeError(message);
});
isolate.addErrorListener(errorPort.sendPort);
return completer.future;
});
}