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 // Patch file for the dart:isolate library. | 5 // Patch file for the dart:isolate library. |
6 | 6 |
7 import 'dart:_js_helper' show patch; | 7 import 'dart:_js_helper' show patch; |
8 import 'dart:_isolate_helper' show CapabilityImpl, | 8 import 'dart:_isolate_helper' show CapabilityImpl, |
9 CloseToken, | |
10 IsolateNatives, | 9 IsolateNatives, |
11 JsIsolateSink, | |
12 ReceivePortImpl, | 10 ReceivePortImpl, |
13 RawReceivePortImpl; | 11 RawReceivePortImpl; |
14 | 12 |
| 13 typedef _UnaryFunction(arg); |
| 14 |
15 @patch | 15 @patch |
16 class Isolate { | 16 class Isolate { |
17 static final _currentIsolateCache = IsolateNatives.currentIsolate; | 17 static final _currentIsolateCache = IsolateNatives.currentIsolate; |
18 | 18 |
19 // `current` must be a getter, not just a final field, | 19 // `current` must be a getter, not just a final field, |
20 // to match the external declaration. | 20 // to match the external declaration. |
21 @patch | 21 @patch |
22 static Isolate get current => _currentIsolateCache; | 22 static Isolate get current => _currentIsolateCache; |
23 | 23 |
24 @patch | 24 @patch |
| 25 static Future<Uri> get packageRoot { |
| 26 throw new UnsupportedError("Isolate.packageRoot"); |
| 27 } |
| 28 |
| 29 @patch |
| 30 static Future<Uri> get packageConfig { |
| 31 throw new UnsupportedError("Isolate.packageConfig"); |
| 32 } |
| 33 |
| 34 static Uri _packageBase = Uri.base.resolve(IsolateNatives.packagesBase); |
| 35 |
| 36 @patch |
| 37 static Future<Uri> resolvePackageUri(Uri packageUri) async { |
| 38 if (packageUri.scheme != 'package') return packageUri; |
| 39 return _packageBase.resolveUri(packageUri.replace(scheme: '')); |
| 40 } |
| 41 |
| 42 @patch |
25 static Future<Isolate> spawn(void entryPoint(message), var message, | 43 static Future<Isolate> spawn(void entryPoint(message), var message, |
26 { bool paused: false }) { | 44 {bool paused: false, bool errorsAreFatal, |
| 45 SendPort onExit, SendPort onError}) { |
| 46 bool forcePause = (errorsAreFatal != null) || |
| 47 (onExit != null) || |
| 48 (onError != null); |
27 try { | 49 try { |
28 return IsolateNatives.spawnFunction(entryPoint, message, paused) | 50 // Check for the type of `entryPoint` on the spawning isolate to make |
29 .then((msg) => new Isolate(msg[1], | 51 // error-handling easier. |
30 pauseCapability: msg[2], | 52 if (entryPoint is! _UnaryFunction) { |
31 terminateCapability: msg[3])); | 53 throw new ArgumentError(entryPoint); |
| 54 } |
| 55 // TODO: Consider passing the errorsAreFatal/onExit/onError values |
| 56 // as arguments to the internal spawnUri instead of setting |
| 57 // them after the isolate has been created. |
| 58 return IsolateNatives.spawnFunction(entryPoint, message, |
| 59 paused || forcePause) |
| 60 .then((msg) { |
| 61 var isolate = new Isolate(msg[1], |
| 62 pauseCapability: msg[2], |
| 63 terminateCapability: msg[3]); |
| 64 if (forcePause) { |
| 65 if (errorsAreFatal != null) { |
| 66 isolate.setErrorsFatal(errorsAreFatal); |
| 67 } |
| 68 if (onExit != null) { |
| 69 isolate.addOnExitListener(onExit); |
| 70 } |
| 71 if (onError != null) { |
| 72 isolate.addErrorListener(onError); |
| 73 } |
| 74 if (!paused) { |
| 75 isolate.resume(isolate.pauseCapability); |
| 76 } |
| 77 } |
| 78 return isolate; |
| 79 }); |
32 } catch (e, st) { | 80 } catch (e, st) { |
33 return new Future<Isolate>.error(e, st); | 81 return new Future<Isolate>.error(e, st); |
34 } | 82 } |
35 } | 83 } |
36 | 84 |
37 @patch | 85 @patch |
38 static Future<Isolate> spawnUri( | 86 static Future<Isolate> spawnUri( |
39 Uri uri, List<String> args, var message, { bool paused: false, | 87 Uri uri, List<String> args, var message, |
40 Uri packageRoot }) { | 88 {bool paused: false, |
| 89 SendPort onExit, |
| 90 SendPort onError, |
| 91 bool errorsAreFatal, |
| 92 bool checked, |
| 93 Map<String, String> environment, |
| 94 Uri packageRoot, |
| 95 Uri packageConfig, |
| 96 bool automaticPackageResolution: false}) { |
| 97 if (environment != null) throw new UnimplementedError("environment"); |
41 if (packageRoot != null) throw new UnimplementedError("packageRoot"); | 98 if (packageRoot != null) throw new UnimplementedError("packageRoot"); |
| 99 if (packageConfig != null) throw new UnimplementedError("packageConfig"); |
| 100 // TODO(lrn): Figure out how to handle the automaticPackageResolution |
| 101 // parameter. |
| 102 bool forcePause = (errorsAreFatal != null) || |
| 103 (onExit != null) || |
| 104 (onError != null); |
42 try { | 105 try { |
43 if (args is List) { | 106 if (args is List<String>) { |
44 for (int i = 0; i < args.length; i++) { | 107 for (int i = 0; i < args.length; i++) { |
45 if (args[i] is! String) { | 108 if (args[i] is! String) { |
46 throw new ArgumentError("Args must be a list of Strings $args"); | 109 throw new ArgumentError("Args must be a list of Strings $args"); |
47 } | 110 } |
48 } | 111 } |
49 } else if (args != null) { | 112 } else if (args != null) { |
50 throw new ArgumentError("Args must be a list of Strings $args"); | 113 throw new ArgumentError("Args must be a list of Strings $args"); |
51 } | 114 } |
52 return IsolateNatives.spawnUri(uri, args, message, paused) | 115 // TODO: Handle [packageRoot] somehow, possibly by throwing. |
53 .then((msg) => new Isolate(msg[1], | 116 // TODO: Consider passing the errorsAreFatal/onExit/onError values |
54 pauseCapability: msg[2], | 117 // as arguments to the internal spawnUri instead of setting |
55 terminateCapability: msg[3])); | 118 // them after the isolate has been created. |
| 119 return IsolateNatives.spawnUri(uri, args, message, paused || forcePause) |
| 120 .then((msg) { |
| 121 var isolate = new Isolate(msg[1], |
| 122 pauseCapability: msg[2], |
| 123 terminateCapability: msg[3]); |
| 124 if (forcePause) { |
| 125 if (errorsAreFatal != null) { |
| 126 isolate.setErrorsFatal(errorsAreFatal); |
| 127 } |
| 128 if (onExit != null) { |
| 129 isolate.addOnExitListener(onExit); |
| 130 } |
| 131 if (onError != null) { |
| 132 isolate.addErrorListener(onError); |
| 133 } |
| 134 if (!paused) { |
| 135 isolate.resume(isolate.pauseCapability); |
| 136 } |
| 137 } |
| 138 return isolate; |
| 139 }); |
56 } catch (e, st) { | 140 } catch (e, st) { |
57 return new Future<Isolate>.error(e, st); | 141 return new Future<Isolate>.error(e, st); |
58 } | 142 } |
59 } | 143 } |
60 | 144 |
61 @patch | 145 @patch |
62 void _pause(Capability resumeCapability) { | 146 void _pause(Capability resumeCapability) { |
63 var message = new List(3) | 147 var message = new List(3) |
64 ..[0] = "pause" | 148 ..[0] = "pause" |
65 ..[1] = pauseCapability | 149 ..[1] = pauseCapability |
66 ..[2] = resumeCapability; | 150 ..[2] = resumeCapability; |
67 controlPort.send(message); | 151 controlPort.send(message); |
68 } | 152 } |
69 | 153 |
70 @patch | 154 @patch |
71 void resume(Capability resumeCapability) { | 155 void resume(Capability resumeCapability) { |
72 var message = new List(2) | 156 var message = new List(2) |
73 ..[0] = "resume" | 157 ..[0] = "resume" |
74 ..[1] = resumeCapability; | 158 ..[1] = resumeCapability; |
75 controlPort.send(message); | 159 controlPort.send(message); |
76 } | 160 } |
77 | 161 |
78 @patch | 162 @patch |
79 void addOnExitListener(SendPort responsePort) { | 163 void addOnExitListener(SendPort responsePort, {Object response}) { |
80 // TODO(lrn): Can we have an internal method that checks if the receiving | 164 // TODO(lrn): Can we have an internal method that checks if the receiving |
81 // isolate of a SendPort is still alive? | 165 // isolate of a SendPort is still alive? |
82 var message = new List(2) | 166 var message = new List(3) |
83 ..[0] = "add-ondone" | 167 ..[0] = "add-ondone" |
84 ..[1] = responsePort; | 168 ..[1] = responsePort |
| 169 ..[2] = response; |
85 controlPort.send(message); | 170 controlPort.send(message); |
86 } | 171 } |
87 | 172 |
88 @patch | 173 @patch |
89 void removeOnExitListener(SendPort responsePort) { | 174 void removeOnExitListener(SendPort responsePort) { |
90 var message = new List(2) | 175 var message = new List(2) |
91 ..[0] = "remove-ondone" | 176 ..[0] = "remove-ondone" |
92 ..[1] = responsePort; | 177 ..[1] = responsePort; |
93 controlPort.send(message); | 178 controlPort.send(message); |
94 } | 179 } |
95 | 180 |
96 @patch | 181 @patch |
97 void setErrorsFatal(bool errorsAreFatal) { | 182 void setErrorsFatal(bool errorsAreFatal) { |
98 var message = new List(3) | 183 var message = new List(3) |
99 ..[0] = "set-errors-fatal" | 184 ..[0] = "set-errors-fatal" |
100 ..[1] = terminateCapability | 185 ..[1] = terminateCapability |
101 ..[2] = errorsAreFatal; | 186 ..[2] = errorsAreFatal; |
102 controlPort.send(message); | 187 controlPort.send(message); |
103 } | 188 } |
104 | 189 |
105 @patch | 190 @patch |
106 void kill([int priority = BEFORE_NEXT_EVENT]) { | 191 void kill({int priority: BEFORE_NEXT_EVENT}) { |
107 controlPort.send(["kill", terminateCapability, priority]); | 192 controlPort.send(["kill", terminateCapability, priority]); |
108 } | 193 } |
109 | 194 |
110 @patch | 195 @patch |
111 void ping(SendPort responsePort, [int pingType = IMMEDIATE]) { | 196 void ping(SendPort responsePort, {Object response, |
112 var message = new List(3) | 197 int priority: IMMEDIATE}) { |
| 198 var message = new List(4) |
113 ..[0] = "ping" | 199 ..[0] = "ping" |
114 ..[1] = responsePort | 200 ..[1] = responsePort |
115 ..[2] = pingType; | 201 ..[2] = priority |
| 202 ..[3] = response; |
116 controlPort.send(message); | 203 controlPort.send(message); |
117 } | 204 } |
118 | 205 |
119 @patch | 206 @patch |
120 void addErrorListener(SendPort port) { | 207 void addErrorListener(SendPort port) { |
121 var message = new List(2) | 208 var message = new List(2) |
122 ..[0] = "getErrors" | 209 ..[0] = "getErrors" |
123 ..[1] = port; | 210 ..[1] = port; |
124 controlPort.send(message); | 211 controlPort.send(message); |
125 } | 212 } |
(...skipping 25 matching lines...) Expand all Loading... |
151 factory RawReceivePort([void handler(event)]) { | 238 factory RawReceivePort([void handler(event)]) { |
152 return new RawReceivePortImpl(handler); | 239 return new RawReceivePortImpl(handler); |
153 } | 240 } |
154 } | 241 } |
155 | 242 |
156 @patch | 243 @patch |
157 class Capability { | 244 class Capability { |
158 @patch | 245 @patch |
159 factory Capability() = CapabilityImpl; | 246 factory Capability() = CapabilityImpl; |
160 } | 247 } |
OLD | NEW |