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