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

Side by Side Diff: pkg/dev_compiler/tool/input_sdk/private/ddc_runtime/generators.dart

Issue 2769883002: Fix async iterator runtime type errors (Closed)
Patch Set: Created 3 years, 9 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 unified diff | Download patch
« no previous file with comments | « pkg/dev_compiler/lib/sdk/ddc_sdk.sum ('k') | pkg/dev_compiler/tool/run.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 /// This library adapts ES6 generators to implement Dart's async/await. 5 /// This library adapts ES6 generators to implement Dart's async/await.
6 /// It's designed to interact with Dart's Future/Stream and follow Dart 6 /// It's designed to interact with Dart's Future/Stream and follow Dart
7 /// async/await semantics. 7 /// async/await semantics.
8 /// See https://github.com/dart-lang/sdk/issues/27315 for ideas on 8 /// See https://github.com/dart-lang/sdk/issues/27315 for ideas on
9 /// reconciling Dart's Future and ES6 Promise. 9 /// reconciling Dart's Future and ES6 Promise.
10 /// Inspired by `co`: https://github.com/tj/co/blob/master/index.js, which is a 10 /// Inspired by `co`: https://github.com/tj/co/blob/master/index.js, which is a
11 /// stepping stone for proposed ES7 async/await, and uses ES6 Promises. 11 /// stepping stone for proposed ES7 async/await, and uses ES6 Promises.
12 part of dart._runtime; 12 part of dart._runtime;
13 13
14 final _jsIterator = JS('', 'Symbol("_jsIterator")'); 14 final _jsIterator = JS('', 'Symbol("_jsIterator")');
15 final _current = JS('', 'Symbol("_current")'); 15 final _current = JS('', 'Symbol("_current")');
16 16
17 syncStar(gen, E, @rest args) => JS( 17 syncStar(gen, E, @rest args) => JS(
18 '', 18 '',
19 '''(() => { 19 '''(() => {
20 const SyncIterable_E = ${getGenericClass(SyncIterable)}($E); 20 const SyncIterable_E = ${getGenericClass(SyncIterable)}($E);
21 return new SyncIterable_E($gen, $args); 21 return new SyncIterable_E($gen, $args);
22 })()'''); 22 })()''');
23 23
24 @JSExportName('async') 24 @JSExportName('async')
25 async_(gen, T, @rest args) => JS( 25 async_(gen, T, @rest args) => JS(
26 '', 26 '',
27 '''(() => { 27 '''(() => {
28 let FutureRaw = ${getGenericClass(Future)};
29 let FutureT = FutureRaw($T);
30 let FutureD = FutureRaw(dart.dynamic);
28 let iter; 31 let iter;
29 function onValue(res) { 32 function onValue(res) {
30 if (res === void 0) res = null; 33 if (res === void 0) res = null;
31 return next(iter.next(res)); 34 return next(iter.next(res));
32 } 35 }
33 function onError(err) { 36 function onError(err) {
34 // If the awaited Future throws, we want to convert this to an exception 37 // If the awaited Future throws, we want to convert this to an exception
35 // thrown from the `yield` point, as if it was thrown there. 38 // thrown from the `yield` point, as if it was thrown there.
36 // 39 //
37 // If the exception is not caught inside `gen`, it will emerge here, which 40 // If the exception is not caught inside `gen`, it will emerge here, which
38 // will send it to anyone listening on this async function's Future<T>. 41 // will send it to anyone listening on this async function's Future<T>.
39 // 42 //
40 // In essence, we are giving the code inside the generator a chance to 43 // In essence, we are giving the code inside the generator a chance to
41 // use try-catch-finally. 44 // use try-catch-finally.
42 return next(iter.throw(err)); 45 return next(iter.throw(err));
43 } 46 }
44 function next(ret) { 47 function next(ret) {
45 if (ret.done) return ret.value; 48 if (ret.done) return ret.value;
46 // Checks if the awaited value is a Future. 49 // Checks if the awaited value is a Future.
47 let future = ret.value; 50 let future = ret.value;
48 if (!$instanceOf(future, ${getGenericClass(Future)})) { 51 if (!$instanceOf(future, FutureD)) {
49 future = $Future.value(future); 52 future = FutureT.value(future);
Jennifer Messerly 2017/03/23 00:31:19 Chatted w/ Vijay offline ... I don't think this is
50 } 53 }
51 // Chain the Future so `await` receives the Future's value. 54 // Chain the Future so `await` receives the Future's value.
52 return future.then($dynamic)(onValue, {onError: onError}); 55 return future.then($T)(onValue, {onError: onError});
53 } 56 }
54 return ${getGenericClass(Future)}($T).microtask(function() { 57 return FutureT.microtask(function() {
55 iter = $gen.apply(null, $args)[Symbol.iterator](); 58 iter = $gen.apply(null, $args)[Symbol.iterator]();
56 return onValue(); 59 return onValue();
57 }); 60 });
58 })()'''); 61 })()''');
59 62
60 // Implementation inspired by _AsyncStarStreamController in 63 // Implementation inspired by _AsyncStarStreamController in
61 // dart-lang/sdk's runtime/lib/core_patch.dart 64 // dart-lang/sdk's runtime/lib/core_patch.dart
62 // 65 //
63 // Given input like: 66 // Given input like:
64 // 67 //
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 } 226 }
224 '''); 227 ''');
225 228
226 /// Returns a Stream of T implemented by an async* function. */ 229 /// Returns a Stream of T implemented by an async* function. */
227 /// 230 ///
228 asyncStar(gen, T, @rest args) => JS( 231 asyncStar(gen, T, @rest args) => JS(
229 '', 232 '',
230 '''(() => { 233 '''(() => {
231 return new $_AsyncStarStreamController($gen, $T, $args).controller.stream; 234 return new $_AsyncStarStreamController($gen, $T, $args).controller.stream;
232 })()'''); 235 })()''');
OLDNEW
« no previous file with comments | « pkg/dev_compiler/lib/sdk/ddc_sdk.sum ('k') | pkg/dev_compiler/tool/run.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698