-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dart2wasm] Handle JS exceptions in
await
exprs, finally
blocks
Fixes #54024. Change-Id: I5c92f12a453a550ce76b340116f57b329ffa97f7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335740 Reviewed-by: Jackson Gardner <jacksongardner@google.com> Commit-Queue: Ömer Ağacan <omersa@google.com>
- Loading branch information
Showing
3 changed files
with
129 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:js_interop'; | ||
import 'dart:_wasm'; | ||
|
||
import 'package:expect/expect.dart'; | ||
|
||
// Catch JS exceptions in try-catch and try-finally, in sync and async | ||
// functions. Also in `await`. | ||
void main() async { | ||
defineThrowJSException(); | ||
|
||
jsExceptionCatch(); | ||
jsExceptionFinally(); | ||
jsExceptionCatchAsync(); | ||
jsExceptionFinallyAsync(); | ||
} | ||
|
||
@JS() | ||
external void eval(String code); | ||
|
||
@JS() | ||
external void throwJSException(); | ||
|
||
bool runtimeTrue() => int.parse('1') == 1; | ||
|
||
void defineThrowJSException() { | ||
eval(r''' | ||
globalThis.throwJSException = function() { | ||
throw "Hi from JS"; | ||
} | ||
'''); | ||
} | ||
|
||
// Catch a JS exception in `catch`. | ||
void jsExceptionCatch() { | ||
try { | ||
throwJSException(); | ||
} catch (e) { | ||
return; | ||
} | ||
Expect.fail("Exception not caught"); | ||
} | ||
|
||
// Catch a JS exception in `finally`. | ||
void jsExceptionFinally() { | ||
if (runtimeTrue()) { | ||
try { | ||
throwJSException(); | ||
} finally { | ||
return; | ||
} | ||
} | ||
Expect.fail("Exception not caught"); | ||
} | ||
|
||
Future<void> throwJSExceptionAsync() async { | ||
return throwJSException(); | ||
} | ||
|
||
// Catch a JS exception throw by `await` in `catch`. | ||
Future<void> jsExceptionCatchAsync() async { | ||
try { | ||
await throwJSExceptionAsync(); | ||
} catch (e) { | ||
return; | ||
} | ||
Expect.fail("Exception not caught"); | ||
} | ||
|
||
// Catch a JS exception thrown by `await` in `finally`. | ||
Future<void> jsExceptionFinallyAsync() async { | ||
if (runtimeTrue()) { | ||
try { | ||
await throwJSExceptionAsync(); | ||
} finally { | ||
return; | ||
} | ||
} | ||
Expect.fail("Exception not caught"); | ||
} |