| 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 /** | 5 /** |
| 6 * A native object that is shared across isolates. This object is visible to all | 6 * A native object that is shared across isolates. This object is visible to all |
| 7 * isolates running on the same worker (either UI or background web worker). | 7 * isolates running on the same worker (either UI or background web worker). |
| 8 * | 8 * |
| 9 * This is code that is intended to 'escape' the isolate boundaries in order to | 9 * This is code that is intended to 'escape' the isolate boundaries in order to |
| 10 * implement the semantics of isolates in JavaScript. Without this we would have | 10 * implement the semantics of isolates in JavaScript. Without this we would have |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 | 190 |
| 191 /** Unregister a port on this isolate. */ | 191 /** Unregister a port on this isolate. */ |
| 192 void unregister(int portId) { | 192 void unregister(int portId) { |
| 193 ports.remove(portId); | 193 ports.remove(portId); |
| 194 if (ports.isEmpty()) { | 194 if (ports.isEmpty()) { |
| 195 _globalState.isolates.remove(id); // indicate this isolate is not active | 195 _globalState.isolates.remove(id); // indicate this isolate is not active |
| 196 } | 196 } |
| 197 } | 197 } |
| 198 } | 198 } |
| 199 | 199 |
| 200 // We don't want to import the DOM library just because of window.setTimeout, |
| 201 // so we reconstruct the Window class here. The only conflict that could happen |
| 202 // with the other DOMWindow class would be because of subclasses. |
| 203 // Currently, none of the two Dart classes have subclasses. |
| 204 typedef void _TimeoutHandler(); |
| 205 class _Window native "@*DOMWindow" { |
| 206 int setTimeout(_TimeoutHandler handler, int timeout) native; |
| 207 } |
| 208 _Window get _window() native |
| 209 """return typeof window != 'undefined' ? window : (void 0);"""; |
| 200 | 210 |
| 201 /** Represent the event loop on a javascript thread (DOM or worker). */ | 211 /** Represent the event loop on a javascript thread (DOM or worker). */ |
| 202 class _EventLoop { | 212 class _EventLoop { |
| 203 Queue<_IsolateEvent> events; | 213 Queue<_IsolateEvent> events; |
| 204 | 214 |
| 205 _EventLoop() : events = new Queue<_IsolateEvent>(); | 215 _EventLoop() : events = new Queue<_IsolateEvent>(); |
| 206 | 216 |
| 207 void enqueue(isolate, fn, msg) { | 217 void enqueue(isolate, fn, msg) { |
| 208 events.addLast(new _IsolateEvent(isolate, fn, msg)); | 218 events.addLast(new _IsolateEvent(isolate, fn, msg)); |
| 209 } | 219 } |
| 210 | 220 |
| 211 _IsolateEvent dequeue() { | 221 _IsolateEvent dequeue() { |
| 212 if (events.isEmpty()) return null; | 222 if (events.isEmpty()) return null; |
| 213 return events.removeFirst(); | 223 return events.removeFirst(); |
| 214 } | 224 } |
| 215 | 225 |
| 216 /** Process a single event, if any. */ | 226 /** Process a single event, if any. */ |
| 217 bool runIteration() { | 227 bool runIteration() { |
| 218 final event = dequeue(); | 228 final event = dequeue(); |
| 219 if (event == null) { | 229 if (event == null) { |
| 220 _globalState.closeWorker(); | 230 _globalState.closeWorker(); |
| 221 return false; | 231 return false; |
| 222 } | 232 } |
| 223 event.process(); | 233 event.process(); |
| 224 return true; | 234 return true; |
| 225 } | 235 } |
| 226 | 236 |
| 227 /** Function equivalent to [:window.setTimeout:] when available, or null. */ | |
| 228 static Function _wrapSetTimeout() native """ | |
| 229 return typeof window != 'undefined' ? | |
| 230 function(a, b) { window.setTimeout(a, b); } : undefined; | |
| 231 """; | |
| 232 | |
| 233 /** | 237 /** |
| 234 * Runs multiple iterations of the run-loop. If possible, each iteration is | 238 * Runs multiple iterations of the run-loop. If possible, each iteration is |
| 235 * run asynchronously. | 239 * run asynchronously. |
| 236 */ | 240 */ |
| 237 void _runHelper() { | 241 void _runHelper() { |
| 238 final setTimeout = _wrapSetTimeout(); | 242 if (_window != null) { |
| 239 if (setTimeout != null) { | |
| 240 // Run each iteration from the browser's top event loop. | 243 // Run each iteration from the browser's top event loop. |
| 241 void next() { | 244 void next() { |
| 242 if (!runIteration()) return; | 245 if (!runIteration()) return; |
| 243 setTimeout(next, 0); | 246 _window.setTimeout(next, 0); |
| 244 } | 247 } |
| 245 next(); | 248 next(); |
| 246 } else { | 249 } else { |
| 247 // Run synchronously until no more iterations are available. | 250 // Run synchronously until no more iterations are available. |
| 248 while (runIteration()) {} | 251 while (runIteration()) {} |
| 249 } | 252 } |
| 250 } | 253 } |
| 251 | 254 |
| 252 /** | 255 /** |
| 253 * Call [_runHelper] but ensure that worker exceptions are propragated. Note | 256 * Call [_runHelper] but ensure that worker exceptions are propragated. Note |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 'command': 'start2', | 634 'command': 'start2', |
| 632 'id': workerId, | 635 'id': workerId, |
| 633 // Note: we serialize replyPort twice because the child worker needs to | 636 // Note: we serialize replyPort twice because the child worker needs to |
| 634 // first deserialize the worker id, before it can correctly deserialize | 637 // first deserialize the worker id, before it can correctly deserialize |
| 635 // the port (port deserialization is sensitive to what is the current | 638 // the port (port deserialization is sensitive to what is the current |
| 636 // workerId). | 639 // workerId). |
| 637 'replyTo': _serializeMessage(replyPort), | 640 'replyTo': _serializeMessage(replyPort), |
| 638 'functionName': functionName })); | 641 'functionName': functionName })); |
| 639 } | 642 } |
| 640 } | 643 } |
| OLD | NEW |