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 library _js_helper; | 5 library _js_helper; |
| 6 | 6 |
| 7 import 'dart:_async_await_error_codes' as async_error_codes; | 7 import 'dart:_async_await_error_codes' as async_error_codes; |
| 8 | 8 |
| 9 import 'dart:_js_embedded_names' show | 9 import 'dart:_js_embedded_names' show |
| 10 GET_TYPE_FROM_NAME, | 10 GET_TYPE_FROM_NAME, |
| (...skipping 3571 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3582 Future future = object is Future ? object : new Future.value(object); | 3582 Future future = object is Future ? object : new Future.value(object); |
| 3583 future.then(_wrapJsFunctionForAsync(bodyFunctionOrErrorCode, | 3583 future.then(_wrapJsFunctionForAsync(bodyFunctionOrErrorCode, |
| 3584 async_error_codes.SUCCESS), | 3584 async_error_codes.SUCCESS), |
| 3585 onError: _wrapJsFunctionForAsync(bodyFunctionOrErrorCode, | 3585 onError: _wrapJsFunctionForAsync(bodyFunctionOrErrorCode, |
| 3586 async_error_codes.ERROR)); | 3586 async_error_codes.ERROR)); |
| 3587 return completer.future; | 3587 return completer.future; |
| 3588 } | 3588 } |
| 3589 | 3589 |
| 3590 Function _wrapJsFunctionForAsync(dynamic /* js function */ function, | 3590 Function _wrapJsFunctionForAsync(dynamic /* js function */ function, |
| 3591 int errorCode) { | 3591 int errorCode) { |
| 3592 var protected = JS('', """ | |
| 3593 function(errorCode, result) { | |
| 3594 while (true) { | |
|
floitsch
2015/02/18 12:42:45
Add comment.
// Invokes [function] with [errorCode
sigurdm
2015/02/19 09:14:02
Done.
| |
| 3595 try { | |
| 3596 #(errorCode, result); | |
| 3597 break; | |
| 3598 } catch (error) { | |
| 3599 result = error; | |
| 3600 errorCode = #; | |
| 3601 } | |
| 3602 } while (false); | |
|
floitsch
2015/02/18 12:42:45
while (false); ?
sigurdm
2015/02/19 09:14:02
I started out with
do {
try {
....
} catch
| |
| 3603 }""", function, async_error_codes.ERROR); | |
| 3592 return (result) { | 3604 return (result) { |
| 3593 JS('', '#(#, #)', function, errorCode, result); | 3605 JS('', '#(#, #)', protected, errorCode, result); |
| 3594 }; | 3606 }; |
| 3595 } | 3607 } |
| 3596 | 3608 |
| 3597 /// Implements the runtime support for async* functions. | 3609 /// Implements the runtime support for async* functions. |
| 3598 /// | 3610 /// |
| 3599 /// Called by the transformed function for each original return, await, yield, | 3611 /// Called by the transformed function for each original return, await, yield, |
| 3600 /// yield* and before starting the function. | 3612 /// yield* and before starting the function. |
| 3601 /// | 3613 /// |
| 3602 /// When the async* function wants to return it calls this function with | 3614 /// When the async* function wants to return it calls this function with |
| 3603 /// [asyncBody] == [async_error_codes.SUCCESS], the asyncStarHelper takes this | 3615 /// [asyncBody] == [async_error_codes.SUCCESS], the asyncStarHelper takes this |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3753 } | 3765 } |
| 3754 | 3766 |
| 3755 static uncaughtError(dynamic error) { | 3767 static uncaughtError(dynamic error) { |
| 3756 return new IterationMarker._(UNCAUGHT_ERROR, error); | 3768 return new IterationMarker._(UNCAUGHT_ERROR, error); |
| 3757 } | 3769 } |
| 3758 | 3770 |
| 3759 toString() => "IterationMarker($state, $value)"; | 3771 toString() => "IterationMarker($state, $value)"; |
| 3760 } | 3772 } |
| 3761 | 3773 |
| 3762 class SyncStarIterator implements Iterator { | 3774 class SyncStarIterator implements Iterator { |
| 3763 final Function _body; | 3775 final dynamic _body; |
| 3764 | 3776 |
| 3765 // If [runningNested] this is the nested iterator, otherwise it is the | 3777 // If [runningNested] this is the nested iterator, otherwise it is the |
| 3766 // current value. | 3778 // current value. |
| 3767 dynamic _current = null; | 3779 dynamic _current = null; |
| 3768 bool _runningNested = false; | 3780 bool _runningNested = false; |
| 3769 | 3781 |
| 3770 get current => _runningNested ? _current.current : _current; | 3782 get current => _runningNested ? _current.current : _current; |
| 3771 | 3783 |
| 3772 SyncStarIterator(body) | 3784 SyncStarIterator(this._body); |
| 3773 : _body = (() => JS('', '#()', body)); | 3785 |
| 3786 runBody() { | |
| 3787 return JS('', ''' | |
| 3788 (function(body) { | |
| 3789 var errorValue, errorCode = #; | |
| 3790 while (true) { | |
|
floitsch
2015/02/18 12:42:45
ditto.
sigurdm
2015/02/19 09:14:02
Done.
| |
| 3791 try { | |
| 3792 return body(errorCode, errorValue); | |
| 3793 } catch (error) { | |
| 3794 errorValue = error; | |
| 3795 errorCode = # | |
| 3796 } | |
| 3797 } | |
| 3798 })(#)''', async_error_codes.SUCCESS, async_error_codes.ERROR, _body); | |
| 3799 } | |
| 3800 | |
| 3774 | 3801 |
| 3775 bool moveNext() { | 3802 bool moveNext() { |
| 3776 if (_runningNested) { | 3803 if (_runningNested) { |
| 3777 if (_current.moveNext()) { | 3804 if (_current.moveNext()) { |
| 3778 return true; | 3805 return true; |
| 3779 } else { | 3806 } else { |
| 3780 _runningNested = false; | 3807 _runningNested = false; |
| 3781 } | 3808 } |
| 3782 } | 3809 } |
| 3783 _current = _body(); | 3810 _current = runBody(); |
| 3784 if (_current is IterationMarker) { | 3811 if (_current is IterationMarker) { |
| 3785 if (_current.state == IterationMarker.ITERATION_ENDED) { | 3812 if (_current.state == IterationMarker.ITERATION_ENDED) { |
| 3786 _current = null; | 3813 _current = null; |
| 3787 // Rely on [_body] to repeatedly return `ITERATION_ENDED`. | 3814 // Rely on [_body] to repeatedly return `ITERATION_ENDED`. |
| 3788 return false; | 3815 return false; |
| 3789 } else if (_current.state == IterationMarker.UNCAUGHT_ERROR) { | 3816 } else if (_current.state == IterationMarker.UNCAUGHT_ERROR) { |
| 3790 // Rely on [_body] to repeatedly return `UNCAUGHT_ERROR`. | 3817 // Rely on [_body] to repeatedly return `UNCAUGHT_ERROR`. |
| 3791 // This is a wrapped exception, so we use JavaScript throw to throw it. | 3818 // This is a wrapped exception, so we use JavaScript throw to throw it. |
| 3792 JS('', 'throw #', _current.value); | 3819 JS('', 'throw #', _current.value); |
| 3793 } else { | 3820 } else { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 3808 // This is a function that will return a helper function that does the | 3835 // This is a function that will return a helper function that does the |
| 3809 // iteration of the sync*. | 3836 // iteration of the sync*. |
| 3810 // | 3837 // |
| 3811 // Each invocation should give a body with fresh state. | 3838 // Each invocation should give a body with fresh state. |
| 3812 final dynamic /* js function */ _outerHelper; | 3839 final dynamic /* js function */ _outerHelper; |
| 3813 | 3840 |
| 3814 SyncStarIterable(this._outerHelper); | 3841 SyncStarIterable(this._outerHelper); |
| 3815 | 3842 |
| 3816 Iterator get iterator => new SyncStarIterator(JS('', '#()', _outerHelper)); | 3843 Iterator get iterator => new SyncStarIterator(JS('', '#()', _outerHelper)); |
| 3817 } | 3844 } |
| OLD | NEW |