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 library _isolate_helper; | 5 library _isolate_helper; |
6 | 6 |
7 import 'dart:_js_embedded_names' show | 7 import 'dart:_js_embedded_names' show |
8 CLASS_ID_EXTRACTOR, | 8 CLASS_ID_EXTRACTOR, |
9 CLASS_FIELDS_EXTRACTOR, | 9 CLASS_FIELDS_EXTRACTOR, |
10 CREATE_NEW_ISOLATE, | 10 CREATE_NEW_ISOLATE, |
11 CURRENT_SCRIPT, | 11 CURRENT_SCRIPT, |
12 GLOBAL_FUNCTIONS, | 12 GLOBAL_FUNCTIONS, |
13 INITIALIZE_EMPTY_INSTANCE, | 13 INITIALIZE_EMPTY_INSTANCE, |
14 INSTANCE_FROM_CLASS_ID; | 14 INSTANCE_FROM_CLASS_ID; |
15 | 15 |
16 import 'dart:async'; | 16 import 'dart:async'; |
17 import 'dart:collection' show Queue, HashMap; | 17 import 'dart:collection' show Queue; |
18 import 'dart:isolate'; | 18 import 'dart:isolate'; |
19 import 'dart:_native_typed_data' show NativeByteBuffer, NativeTypedData; | 19 import 'dart:_native_typed_data' show NativeByteBuffer, NativeTypedData; |
20 | 20 |
21 import 'dart:_js_helper' show | 21 import 'dart:_js_helper' show |
22 Closure, | 22 Closure, |
23 InternalMap, | 23 InternalMap, |
24 Null, | 24 Null, |
25 Primitives, | 25 Primitives, |
26 convertDartClosureToJS, | 26 convertDartClosureToJS, |
27 random64, | 27 random64, |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 _IsolateEvent event = delayedEvents.removeLast(); | 350 _IsolateEvent event = delayedEvents.removeLast(); |
351 _globalState.topEventLoop.prequeue(event); | 351 _globalState.topEventLoop.prequeue(event); |
352 } | 352 } |
353 isPaused = false; | 353 isPaused = false; |
354 } | 354 } |
355 _updateGlobalState(); | 355 _updateGlobalState(); |
356 } | 356 } |
357 | 357 |
358 void addDoneListener(SendPort responsePort, Object response) { | 358 void addDoneListener(SendPort responsePort, Object response) { |
359 if (doneHandlers == null) { | 359 if (doneHandlers == null) { |
360 // TODO(lrn): Use map optimized for few keys. | 360 doneHandlers = []; |
361 doneHandlers = new HashMap(); | |
362 } | 361 } |
363 doneHandlers[responsePort] = response; | 362 for (int i = 0; i < doneHandlers.length; i += 2) { |
| 363 if (responsePort == doneHandlers[i]) { |
| 364 doneHandlers[i + 1] = response; |
| 365 return; |
| 366 } |
| 367 } |
| 368 doneHandlers.add(responsePort); |
| 369 doneHandlers.add(response); |
364 } | 370 } |
365 | 371 |
366 void removeDoneListener(SendPort responsePort) { | 372 void removeDoneListener(SendPort responsePort) { |
367 if (doneHandlers == null) return; | 373 if (doneHandlers == null) return; |
368 doneHandlers.remove(responsePort); | 374 for (int i = 0; i < doneHandlers.length; i += 2) { |
| 375 if (responsePort == doneHandlers[i]) { |
| 376 doneHandlers.removeRange(i, i + 2); |
| 377 return; |
| 378 } |
| 379 } |
369 } | 380 } |
370 | 381 |
371 void setErrorsFatal(Capability authentification, bool errorsAreFatal) { | 382 void setErrorsFatal(Capability authentification, bool errorsAreFatal) { |
372 if (terminateCapability != authentification) return; | 383 if (terminateCapability != authentification) return; |
373 this.errorsAreFatal = errorsAreFatal; | 384 this.errorsAreFatal = errorsAreFatal; |
374 } | 385 } |
375 | 386 |
376 void handlePing(SendPort responsePort, int pingType, Object response) { | 387 void handlePing(SendPort responsePort, int pingType, Object response) { |
377 if (pingType == Isolate.IMMEDIATE || | 388 if (pingType == Isolate.IMMEDIATE || |
378 (pingType == Isolate.BEFORE_NEXT_EVENT && | 389 (pingType == Isolate.BEFORE_NEXT_EVENT && |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 // we are listening on ourselves. | 568 // we are listening on ourselves. |
558 // Closes all ports, including control port. | 569 // Closes all ports, including control port. |
559 for (var port in ports.values) { | 570 for (var port in ports.values) { |
560 port._close(); | 571 port._close(); |
561 } | 572 } |
562 ports.clear(); | 573 ports.clear(); |
563 weakPorts.clear(); | 574 weakPorts.clear(); |
564 _globalState.isolates.remove(id); // indicate this isolate is not active | 575 _globalState.isolates.remove(id); // indicate this isolate is not active |
565 errorPorts.clear(); | 576 errorPorts.clear(); |
566 if (doneHandlers != null) { | 577 if (doneHandlers != null) { |
567 doneHandlers.forEach((port, response) { port.send(response); }); | 578 for (int i = 0; i < doneHandlers.length; i += 2) { |
| 579 SendPort responsePort = doneHandlers[i]; |
| 580 Object response = doneHandlers[i + 1]; |
| 581 responsePort.send(response); |
| 582 } |
568 doneHandlers = null; | 583 doneHandlers = null; |
569 } | 584 } |
570 } | 585 } |
571 | 586 |
572 /** Unregister a port on this isolate. */ | 587 /** Unregister a port on this isolate. */ |
573 void unregister(int portId) { | 588 void unregister(int portId) { |
574 ports.remove(portId); | 589 ports.remove(portId); |
575 weakPorts.remove(portId); | 590 weakPorts.remove(portId); |
576 _updateGlobalState(); | 591 _updateGlobalState(); |
577 } | 592 } |
(...skipping 858 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1436 } | 1451 } |
1437 | 1452 |
1438 bool operator==(Object other) { | 1453 bool operator==(Object other) { |
1439 if (identical(other, this)) return true; | 1454 if (identical(other, this)) return true; |
1440 if (other is CapabilityImpl) { | 1455 if (other is CapabilityImpl) { |
1441 return identical(_id, other._id); | 1456 return identical(_id, other._id); |
1442 } | 1457 } |
1443 return false; | 1458 return false; |
1444 } | 1459 } |
1445 } | 1460 } |
OLD | NEW |