| 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 1762 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1773 if (thrownStackTrace == null) { | 1773 if (thrownStackTrace == null) { |
| 1774 JS('void', r'#.$thrownJsError = #', error, ex); | 1774 JS('void', r'#.$thrownJsError = #', error, ex); |
| 1775 } | 1775 } |
| 1776 } | 1776 } |
| 1777 return error; | 1777 return error; |
| 1778 } | 1778 } |
| 1779 | 1779 |
| 1780 // Note that we are checking if the object has the property. If it | 1780 // Note that we are checking if the object has the property. If it |
| 1781 // has, it could be set to null if the thrown value is null. | 1781 // has, it could be set to null if the thrown value is null. |
| 1782 if (ex == null) return null; | 1782 if (ex == null) return null; |
| 1783 if (ex is ExceptionAndStackTrace) { |
| 1784 return saveStackTrace(ex.dartException); |
| 1785 } |
| 1783 if (JS('bool', 'typeof # !== "object"', ex)) return ex; | 1786 if (JS('bool', 'typeof # !== "object"', ex)) return ex; |
| 1784 | 1787 |
| 1785 if (JS('bool', r'"dartException" in #', ex)) { | 1788 if (JS('bool', r'"dartException" in #', ex)) { |
| 1786 return saveStackTrace(JS('', r'#.dartException', ex)); | 1789 return saveStackTrace(JS('', r'#.dartException', ex)); |
| 1787 } else if (!JS('bool', r'"message" in #', ex)) { | 1790 } else if (!JS('bool', r'"message" in #', ex)) { |
| 1788 return ex; | 1791 return ex; |
| 1789 } | 1792 } |
| 1790 | 1793 |
| 1791 // Grab hold of the exception message. This field is available on | 1794 // Grab hold of the exception message. This field is available on |
| 1792 // all supported browsers. | 1795 // all supported browsers. |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1896 // Just return the exception. We should not wrap it because in case | 1899 // Just return the exception. We should not wrap it because in case |
| 1897 // the exception comes from the DOM, it is a JavaScript | 1900 // the exception comes from the DOM, it is a JavaScript |
| 1898 // object backed by a native Dart class. | 1901 // object backed by a native Dart class. |
| 1899 return ex; | 1902 return ex; |
| 1900 } | 1903 } |
| 1901 | 1904 |
| 1902 /** | 1905 /** |
| 1903 * Called by generated code to fetch the stack trace from an | 1906 * Called by generated code to fetch the stack trace from an |
| 1904 * exception. Should never return null. | 1907 * exception. Should never return null. |
| 1905 */ | 1908 */ |
| 1906 StackTrace getTraceFromException(exception) => new _StackTrace(exception); | 1909 StackTrace getTraceFromException(exception) { |
| 1910 if (exception is ExceptionAndStackTrace) { |
| 1911 return exception.stackTrace; |
| 1912 } |
| 1913 return new _StackTrace(exception); |
| 1914 } |
| 1907 | 1915 |
| 1908 class _StackTrace implements StackTrace { | 1916 class _StackTrace implements StackTrace { |
| 1909 var _exception; | 1917 var _exception; |
| 1910 String _trace; | 1918 String _trace; |
| 1911 _StackTrace(this._exception); | 1919 _StackTrace(this._exception); |
| 1912 | 1920 |
| 1913 String toString() { | 1921 String toString() { |
| 1914 if (_trace != null) return JS('String', '#', _trace); | 1922 if (_trace != null) return JS('String', '#', _trace); |
| 1915 | 1923 |
| 1916 String trace; | 1924 String trace; |
| (...skipping 1627 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3544 } | 3552 } |
| 3545 | 3553 |
| 3546 void badMain() { | 3554 void badMain() { |
| 3547 throw new MainError("'main' is not a function."); | 3555 throw new MainError("'main' is not a function."); |
| 3548 } | 3556 } |
| 3549 | 3557 |
| 3550 void mainHasTooManyParameters() { | 3558 void mainHasTooManyParameters() { |
| 3551 throw new MainError("'main' expects too many parameters."); | 3559 throw new MainError("'main' expects too many parameters."); |
| 3552 } | 3560 } |
| 3553 | 3561 |
| 3562 /// A wrapper around an exception, much like the one created by [wrapException] |
| 3563 /// but with a pre-given stack-trace. |
| 3564 class ExceptionAndStackTrace { |
| 3565 dynamic dartException; |
| 3566 StackTrace stackTrace; |
| 3567 |
| 3568 ExceptionAndStackTrace(this.dartException, this.stackTrace); |
| 3569 } |
| 3570 |
| 3554 /// Runtime support for async-await transformation. | 3571 /// Runtime support for async-await transformation. |
| 3555 /// | 3572 /// |
| 3556 /// This function is called by a transformed function on each await and return | 3573 /// This function is called by a transformed function on each await and return |
| 3557 /// in the untransformed function, and before starting. | 3574 /// in the untransformed function, and before starting. |
| 3558 /// | 3575 /// |
| 3559 /// If [object] is not a future it will be wrapped in a `new Future.value`. | 3576 /// If [object] is not a future it will be wrapped in a `new Future.value`. |
| 3560 /// | 3577 /// |
| 3561 /// If [asyncBody] is [async_error_codes.SUCCESS]/[async_error_codes.ERROR] it | 3578 /// If [asyncBody] is [async_error_codes.SUCCESS]/[async_error_codes.ERROR] it |
| 3562 /// indicates a return or throw from the async function, and | 3579 /// indicates a return or throw from the async function, and |
| 3563 /// complete/completeError is called on [completer] with [object]. | 3580 /// complete/completeError is called on [completer] with [object]. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 3575 return; | 3592 return; |
| 3576 } else if (identical(bodyFunctionOrErrorCode, async_error_codes.ERROR)) { | 3593 } else if (identical(bodyFunctionOrErrorCode, async_error_codes.ERROR)) { |
| 3577 // The error is a js-error. | 3594 // The error is a js-error. |
| 3578 completer.completeError(unwrapException(object), | 3595 completer.completeError(unwrapException(object), |
| 3579 getTraceFromException(object)); | 3596 getTraceFromException(object)); |
| 3580 return; | 3597 return; |
| 3581 } | 3598 } |
| 3582 Future future = object is Future ? object : new Future.value(object); | 3599 Future future = object is Future ? object : new Future.value(object); |
| 3583 future.then(_wrapJsFunctionForAsync(bodyFunctionOrErrorCode, | 3600 future.then(_wrapJsFunctionForAsync(bodyFunctionOrErrorCode, |
| 3584 async_error_codes.SUCCESS), | 3601 async_error_codes.SUCCESS), |
| 3585 onError: _wrapJsFunctionForAsync(bodyFunctionOrErrorCode, | 3602 onError: (dynamic error, StackTrace stackTrace) { |
| 3586 async_error_codes.ERROR)); | 3603 ExceptionAndStackTrace wrapped = |
| 3604 new ExceptionAndStackTrace(error, stackTrace); |
| 3605 return _wrapJsFunctionForAsync(bodyFunctionOrErrorCode, |
| 3606 async_error_codes.ERROR)(wrapped); |
| 3607 }); |
| 3587 return completer.future; | 3608 return completer.future; |
| 3588 } | 3609 } |
| 3589 | 3610 |
| 3590 Function _wrapJsFunctionForAsync(dynamic /* js function */ function, | 3611 Function _wrapJsFunctionForAsync(dynamic /* js function */ function, |
| 3591 int errorCode) { | 3612 int errorCode) { |
| 3592 var protected = JS('', """ | 3613 var protected = JS('', """ |
| 3593 // Invokes [function] with [errorCode] and [result]. | 3614 // Invokes [function] with [errorCode] and [result]. |
| 3594 // | 3615 // |
| 3595 // If (and as long as) the invocation throws, calls [function] again, | 3616 // If (and as long as) the invocation throws, calls [function] again, |
| 3596 // with an error-code. | 3617 // with an error-code. |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3691 _wrapJsFunctionForAsync(bodyFunctionOrErrorCode, | 3712 _wrapJsFunctionForAsync(bodyFunctionOrErrorCode, |
| 3692 async_error_codes.SUCCESS)(null); | 3713 async_error_codes.SUCCESS)(null); |
| 3693 }); | 3714 }); |
| 3694 return; | 3715 return; |
| 3695 } | 3716 } |
| 3696 } | 3717 } |
| 3697 | 3718 |
| 3698 Future future = object is Future ? object : new Future.value(object); | 3719 Future future = object is Future ? object : new Future.value(object); |
| 3699 future.then(_wrapJsFunctionForAsync(bodyFunctionOrErrorCode, | 3720 future.then(_wrapJsFunctionForAsync(bodyFunctionOrErrorCode, |
| 3700 async_error_codes.SUCCESS), | 3721 async_error_codes.SUCCESS), |
| 3701 onError: _wrapJsFunctionForAsync(bodyFunctionOrErrorCode, | 3722 onError: (error, StackTrace stackTrace) { |
| 3702 async_error_codes.ERROR)); | 3723 ExceptionAndStackTrace wrapped = |
| 3724 new ExceptionAndStackTrace(error, stackTrace); |
| 3725 return _wrapJsFunctionForAsync(bodyFunctionOrErrorCode, |
| 3726 async_error_codes.ERROR) |
| 3727 (wrapped); |
| 3728 }); |
| 3703 } | 3729 } |
| 3704 | 3730 |
| 3705 Stream streamOfController(AsyncStarStreamController controller) { | 3731 Stream streamOfController(AsyncStarStreamController controller) { |
| 3706 return controller.stream; | 3732 return controller.stream; |
| 3707 } | 3733 } |
| 3708 | 3734 |
| 3709 /// A wrapper around a [StreamController] that remembers if that controller | 3735 /// A wrapper around a [StreamController] that remembers if that controller |
| 3710 /// got a cancel. | 3736 /// got a cancel. |
| 3711 /// | 3737 /// |
| 3712 /// Also has a subSubscription that when not null will provide events for the | 3738 /// Also has a subSubscription that when not null will provide events for the |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3845 // This is a function that will return a helper function that does the | 3871 // This is a function that will return a helper function that does the |
| 3846 // iteration of the sync*. | 3872 // iteration of the sync*. |
| 3847 // | 3873 // |
| 3848 // Each invocation should give a body with fresh state. | 3874 // Each invocation should give a body with fresh state. |
| 3849 final dynamic /* js function */ _outerHelper; | 3875 final dynamic /* js function */ _outerHelper; |
| 3850 | 3876 |
| 3851 SyncStarIterable(this._outerHelper); | 3877 SyncStarIterable(this._outerHelper); |
| 3852 | 3878 |
| 3853 Iterator get iterator => new SyncStarIterator(JS('', '#()', _outerHelper)); | 3879 Iterator get iterator => new SyncStarIterator(JS('', '#()', _outerHelper)); |
| 3854 } | 3880 } |
| OLD | NEW |