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 // Invokes [function] with [errorCode] and [result]. | |
| 3594 // | |
| 3595 // If (and as long as) the invocation throws, calls [function] again, | |
| 3596 // with an error-code. | |
| 3597 function(errorCode, result) { | |
| 3598 while (true) { | |
| 3599 try { | |
| 3600 #(errorCode, result); | |
| 3601 break; | |
| 3602 } catch (error) { | |
| 3603 result = error; | |
| 3604 errorCode = #; | |
| 3605 } | |
| 3606 } | |
| 3607 }""", function, async_error_codes.ERROR); | |
| 3592 return (result) { | 3608 return (result) { |
| 3593 JS('', '#(#, #)', function, errorCode, result); | 3609 JS('', '#(#, #)', protected, errorCode, result); |
| 3594 }; | 3610 }; |
| 3595 } | 3611 } |
| 3596 | 3612 |
| 3597 /// Implements the runtime support for async* functions. | 3613 /// Implements the runtime support for async* functions. |
| 3598 /// | 3614 /// |
| 3599 /// Called by the transformed function for each original return, await, yield, | 3615 /// Called by the transformed function for each original return, await, yield, |
| 3600 /// yield* and before starting the function. | 3616 /// yield* and before starting the function. |
| 3601 /// | 3617 /// |
| 3602 /// When the async* function wants to return it calls this function with | 3618 /// When the async* function wants to return it calls this function with |
| 3603 /// [asyncBody] == [async_error_codes.SUCCESS], the asyncStarHelper takes this | 3619 /// [asyncBody] == [async_error_codes.SUCCESS], the asyncStarHelper takes this |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3753 } | 3769 } |
| 3754 | 3770 |
| 3755 static uncaughtError(dynamic error) { | 3771 static uncaughtError(dynamic error) { |
| 3756 return new IterationMarker._(UNCAUGHT_ERROR, error); | 3772 return new IterationMarker._(UNCAUGHT_ERROR, error); |
| 3757 } | 3773 } |
| 3758 | 3774 |
| 3759 toString() => "IterationMarker($state, $value)"; | 3775 toString() => "IterationMarker($state, $value)"; |
| 3760 } | 3776 } |
| 3761 | 3777 |
| 3762 class SyncStarIterator implements Iterator { | 3778 class SyncStarIterator implements Iterator { |
| 3763 final Function _body; | 3779 final dynamic _body; |
| 3764 | 3780 |
| 3765 // If [runningNested] this is the nested iterator, otherwise it is the | 3781 // If [runningNested] this is the nested iterator, otherwise it is the |
| 3766 // current value. | 3782 // current value. |
| 3767 dynamic _current = null; | 3783 dynamic _current = null; |
| 3768 bool _runningNested = false; | 3784 bool _runningNested = false; |
| 3769 | 3785 |
| 3770 get current => _runningNested ? _current.current : _current; | 3786 get current => _runningNested ? _current.current : _current; |
| 3771 | 3787 |
| 3772 SyncStarIterator(body) | 3788 SyncStarIterator(this._body); |
| 3773 : _body = (() => JS('', '#()', body)); | 3789 |
| 3790 runBody() { | |
|
sra1
2015/02/27 20:34:51
Make this private, otherwise I can call it from us
sigurdm
2015/03/02 09:14:47
Good point. Thanks
| |
| 3791 return JS('', ''' | |
| 3792 // Invokes [body] with [errorCode] and [result]. | |
| 3793 // | |
| 3794 // If (and as long as) the invocation throws, calls [function] again, | |
| 3795 // with an error-code. | |
| 3796 (function(body) { | |
| 3797 var errorValue, errorCode = #; | |
| 3798 while (true) { | |
| 3799 try { | |
| 3800 return body(errorCode, errorValue); | |
| 3801 } catch (error) { | |
| 3802 errorValue = error; | |
| 3803 errorCode = # | |
| 3804 } | |
| 3805 } | |
| 3806 })(#)''', async_error_codes.SUCCESS, async_error_codes.ERROR, _body); | |
| 3807 } | |
| 3808 | |
| 3774 | 3809 |
| 3775 bool moveNext() { | 3810 bool moveNext() { |
| 3776 if (_runningNested) { | 3811 if (_runningNested) { |
| 3777 if (_current.moveNext()) { | 3812 if (_current.moveNext()) { |
| 3778 return true; | 3813 return true; |
| 3779 } else { | 3814 } else { |
| 3780 _runningNested = false; | 3815 _runningNested = false; |
| 3781 } | 3816 } |
| 3782 } | 3817 } |
| 3783 _current = _body(); | 3818 _current = runBody(); |
| 3784 if (_current is IterationMarker) { | 3819 if (_current is IterationMarker) { |
| 3785 if (_current.state == IterationMarker.ITERATION_ENDED) { | 3820 if (_current.state == IterationMarker.ITERATION_ENDED) { |
| 3786 _current = null; | 3821 _current = null; |
| 3787 // Rely on [_body] to repeatedly return `ITERATION_ENDED`. | 3822 // Rely on [_body] to repeatedly return `ITERATION_ENDED`. |
| 3788 return false; | 3823 return false; |
| 3789 } else if (_current.state == IterationMarker.UNCAUGHT_ERROR) { | 3824 } else if (_current.state == IterationMarker.UNCAUGHT_ERROR) { |
| 3790 // Rely on [_body] to repeatedly return `UNCAUGHT_ERROR`. | 3825 // Rely on [_body] to repeatedly return `UNCAUGHT_ERROR`. |
| 3791 // This is a wrapped exception, so we use JavaScript throw to throw it. | 3826 // This is a wrapped exception, so we use JavaScript throw to throw it. |
| 3792 JS('', 'throw #', _current.value); | 3827 JS('', 'throw #', _current.value); |
| 3793 } else { | 3828 } else { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 3808 // This is a function that will return a helper function that does the | 3843 // This is a function that will return a helper function that does the |
| 3809 // iteration of the sync*. | 3844 // iteration of the sync*. |
| 3810 // | 3845 // |
| 3811 // Each invocation should give a body with fresh state. | 3846 // Each invocation should give a body with fresh state. |
| 3812 final dynamic /* js function */ _outerHelper; | 3847 final dynamic /* js function */ _outerHelper; |
| 3813 | 3848 |
| 3814 SyncStarIterable(this._outerHelper); | 3849 SyncStarIterable(this._outerHelper); |
| 3815 | 3850 |
| 3816 Iterator get iterator => new SyncStarIterator(JS('', '#()', _outerHelper)); | 3851 Iterator get iterator => new SyncStarIterator(JS('', '#()', _outerHelper)); |
| 3817 } | 3852 } |
| OLD | NEW |