Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 import "dart:math"; | 5 import "dart:math"; |
| 6 import "dart:typed_data"; | 6 import "dart:typed_data"; |
| 7 | 7 |
| 8 // We need to pass the exception and stack trace objects as second and third | 8 // We need to pass the exception and stack trace objects as second and third |
| 9 // parameter to the continuation. See vm/ast_transformer.cc for usage. | 9 // parameter to the continuation. See vm/ast_transformer.cc for usage. |
| 10 void _asyncCatchHelper(catchFunction, continuation) { | 10 void _asyncCatchHelper(catchFunction, continuation) { |
| 11 catchFunction((e, s) => continuation(null, e, s)); | 11 catchFunction((e, s) => continuation(null, e, s)); |
| 12 } | 12 } |
| 13 | 13 |
| 14 // The members of this class are cloned and added to each class that | 14 // The members of this class are cloned and added to each class that |
| 15 // represents an enum type. | 15 // represents an enum type. |
| 16 class _EnumHelper { | 16 class _EnumHelper { |
| 17 // Declare the list of enum value names private. When this field is | 17 // Declare the list of enum value names private. When this field is |
| 18 // cloned into a user-defined enum class, the field will be inaccessible | 18 // cloned into a user-defined enum class, the field will be inaccessible |
| 19 // because of the library-specific name suffix. The toString() function | 19 // because of the library-specific name suffix. The toString() function |
| 20 // below can access it because it uses the same name suffix. | 20 // below can access it because it uses the same name suffix. |
| 21 static const List<String> _enum_names = null; | 21 static const List<String> _enum_names = null; |
| 22 String toString() => _enum_names[index]; | 22 String toString() => _enum_names[index]; |
| 23 } | 23 } |
| 24 | 24 |
| 25 typedef bool SyncGeneratorCallback(Iterator iterator); | 25 |
| 26 // _AsyncStarStreamController is used by the compiler to implement | |
| 27 // async* generator functions. | |
| 28 class _AsyncStarStreamController { | |
| 29 StreamController controller; | |
| 30 Function asyncStarBody; | |
| 31 bool isCancelled = false; | |
| 32 bool isAdding = false; | |
| 33 | |
| 34 Stream get stream => controller.stream; | |
| 35 //bool get isPaused => isAdding || controller.isPaused; | |
| 36 | |
| 37 // Adds element to steam, returns true iff the caller should suspend | |
| 38 // execution of the generator. | |
| 39 bool add(event) { | |
| 40 controller.add(event); | |
| 41 return controller.isPaused; | |
| 42 } | |
| 43 | |
| 44 // Adds stream, tells caller to suspend execution of the generator | |
| 45 // function. The generator will be scheduled again when all of the | |
| 46 // elements of the added stream have been consumed. | |
| 47 bool addStream(Stream stream) { | |
| 48 isAdding = true; | |
| 49 var whenDoneAdding = | |
| 50 controller.addStream(stream as Stream, cancelOnError: false); | |
|
Ivan Posva
2015/02/23 08:23:36
Why is there an "as Stream" here?
hausner
2015/02/23 17:12:01
Because the spec says it is a runtime error if yie
| |
| 51 whenDoneAdding.then((_) { | |
| 52 isAdding = false; | |
| 53 if (!controller.isPaused) { | |
| 54 scheduleMicrotask(asyncStarBody); | |
| 55 } | |
| 56 }); | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 addError(error, stackTrace) { | |
| 61 controller.addError(error, stackTrace); | |
| 62 } | |
| 63 | |
| 64 close() { | |
| 65 controller.close(); | |
| 66 } | |
| 67 | |
| 68 _AsyncStarStreamController(this.asyncStarBody) { | |
| 69 controller = new StreamController(onListen: this.onListen, | |
| 70 onResume: this.onResume, | |
| 71 onCancel: this.onCancel); | |
| 72 } | |
| 73 | |
| 74 onListen() { | |
| 75 scheduleMicrotask(asyncStarBody); | |
| 76 } | |
| 77 | |
| 78 onResume() { | |
| 79 if (!isAdding) { | |
| 80 scheduleMicrotask(asyncStarBody); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 onCancel() { | |
| 85 isCancelled = true; | |
| 86 } | |
| 87 } | |
| 88 | |
| 26 | 89 |
| 27 // _SyncIterable and _syncIterator are used by the compiler to | 90 // _SyncIterable and _syncIterator are used by the compiler to |
| 28 // implement sync* generator functions. A sync* generator allocates | 91 // implement sync* generator functions. A sync* generator allocates |
| 29 // and returns a new _SyncIterable object. | 92 // and returns a new _SyncIterable object. |
| 93 | |
| 94 typedef bool SyncGeneratorCallback(Iterator iterator); | |
| 95 | |
| 30 class _SyncIterable extends IterableBase { | 96 class _SyncIterable extends IterableBase { |
| 31 // moveNextFn is the closurized body of the generator function. | 97 // moveNextFn is the closurized body of the generator function. |
| 32 final SyncGeneratorCallback moveNextFn; | 98 final SyncGeneratorCallback moveNextFn; |
| 33 | 99 |
| 34 const _SyncIterable(this.moveNextFn); | 100 const _SyncIterable(this.moveNextFn); |
| 35 | 101 |
| 36 get iterator { | 102 get iterator { |
| 37 return new _SyncIterator(moveNextFn._clone()); | 103 return new _SyncIterator(moveNextFn._clone()); |
| 38 } | 104 } |
| 39 } | 105 } |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 67 if (isYieldEach) { | 133 if (isYieldEach) { |
| 68 // Spec mandates: it is a dynamic error if the class of [the object | 134 // Spec mandates: it is a dynamic error if the class of [the object |
| 69 // returned by yield*] does not implement Iterable. | 135 // returned by yield*] does not implement Iterable. |
| 70 yieldEachIterator = (current as Iterable).iterator; | 136 yieldEachIterator = (current as Iterable).iterator; |
| 71 continue; | 137 continue; |
| 72 } | 138 } |
| 73 return true; | 139 return true; |
| 74 } | 140 } |
| 75 } | 141 } |
| 76 } | 142 } |
| OLD | NEW |