Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Unified Diff: runtime/lib/core_patch.dart

Issue 944893005: Implement async* functions in VM (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | runtime/vm/flow_graph_builder.h » ('j') | runtime/vm/flow_graph_builder.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/core_patch.dart
===================================================================
--- runtime/lib/core_patch.dart (revision 43883)
+++ runtime/lib/core_patch.dart (working copy)
@@ -22,11 +22,77 @@
String toString() => _enum_names[index];
}
-typedef bool SyncGeneratorCallback(Iterator iterator);
+// _AsyncStarStreamController is used by the compiler to implement
+// async* generator functions.
+class _AsyncStarStreamController {
+ StreamController controller;
+ Function asyncStarBody;
+ bool isCancelled = false;
+ bool isAdding = false;
+
+ Stream get stream => controller.stream;
+ //bool get isPaused => isAdding || controller.isPaused;
+
+ // Adds element to steam, returns true iff the caller should suspend
+ // execution of the generator.
+ bool add(event) {
+ controller.add(event);
+ return controller.isPaused;
+ }
+
+ // Adds stream, tells caller to suspend execution of the generator
+ // function. The generator will be scheduled again when all of the
+ // elements of the added stream have been consumed.
+ bool addStream(Stream stream) {
+ isAdding = true;
+ var whenDoneAdding =
+ 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
+ whenDoneAdding.then((_) {
+ isAdding = false;
+ if (!controller.isPaused) {
+ scheduleMicrotask(asyncStarBody);
+ }
+ });
+ return true;
+ }
+
+ addError(error, stackTrace) {
+ controller.addError(error, stackTrace);
+ }
+
+ close() {
+ controller.close();
+ }
+
+ _AsyncStarStreamController(this.asyncStarBody) {
+ controller = new StreamController(onListen: this.onListen,
+ onResume: this.onResume,
+ onCancel: this.onCancel);
+ }
+
+ onListen() {
+ scheduleMicrotask(asyncStarBody);
+ }
+
+ onResume() {
+ if (!isAdding) {
+ scheduleMicrotask(asyncStarBody);
+ }
+ }
+
+ onCancel() {
+ isCancelled = true;
+ }
+}
+
+
// _SyncIterable and _syncIterator are used by the compiler to
// implement sync* generator functions. A sync* generator allocates
// and returns a new _SyncIterable object.
+
+typedef bool SyncGeneratorCallback(Iterator iterator);
+
class _SyncIterable extends IterableBase {
// moveNextFn is the closurized body of the generator function.
final SyncGeneratorCallback moveNextFn;
« no previous file with comments | « no previous file | runtime/vm/flow_graph_builder.h » ('j') | runtime/vm/flow_graph_builder.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698