Files
sdk/tests/web/wasm/js_exceptions_test.dart
T
Lasse R.H. Nielsen f8086c81ae Collect all test-related files in package:expect.
Collects files from `package:async_helper` and `tests/language`
that are generally useful, so that all test-related helpers are
in `package:expect`.

Moves the two libraries from `package:async_helper` into `package:expect`,
and the `tests/language/static_type_helper.dart` file too.

Deprecates `async_minitest.dart`, to follow `minitest.dart`,
expecting the Flutter use of it to have been fixed to not break
on deprecation (I believe Flutter no longer breaks builds on deprecations at all).

Patch 1 is the actual change.
Patch 2+4+8 is changing all existing references to the files.
Patch 6 ignores deprecation in files still using `async_minitest.dart`.

3+5+7+9 are updating this text to make the numbers match.
Then it's just test-expectations and small tweaks from there.

Change-Id: I1b665135b5fef9b9a0c3b340ffe9daf874d0174c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/373120
Reviewed-by: Nate Bosch <nbosch@google.com>
Reviewed-by: Devon Carew <devoncarew@google.com>
Commit-Queue: Lasse Nielsen <lrn@google.com>
2024-10-11 16:53:52 +00:00

218 lines
4.9 KiB
Dart

// 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.
// dart2wasmOptions=--extra-compiler-option=--enable-experimental-wasm-interop
import 'dart:js_interop';
import 'dart:_wasm';
import 'package:expect/async_helper.dart';
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 {
asyncStart();
defineThrowJSException();
jsExceptionCatch();
jsExceptionFinally();
await jsExceptionCatchAsync();
await jsExceptionFinallyAsync();
await jsExceptionCatchAsyncDirect();
await jsExceptionFinallyAsyncDirect();
await jsExceptionFinallyPropagateAsync();
await jsExceptionFinallyPropagateAsyncDirect();
await jsExceptionTypeTest1();
await jsExceptionTypeTest2();
asyncEnd();
}
@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();
}
// A simple async function used to create suspension points.
Future<int> yield_() async => runtimeTrue() ? 1 : throw '';
// 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");
}
// Catch a JS exception thrown without `await` in `catch`.
Future<void> jsExceptionCatchAsyncDirect() async {
try {
throwJSException();
} catch (e) {
return;
}
Expect.fail("Exception not caught");
}
// Catch a JS exception thrown without `await` in `finally`.
Future<void> jsExceptionFinallyAsyncDirect() async {
if (runtimeTrue()) {
try {
throwJSException();
} finally {
return;
}
}
Expect.fail("Exception not caught");
}
// Check that the finally blocks rethrow JS exceptions, when `await` throws.
Future<void> jsExceptionFinallyPropagateAsync() async {
int i = 0;
try {
if (runtimeTrue()) {
try {
await throwJSExceptionAsync();
} finally {
i += 1;
}
}
} catch (e) {
i += 1;
}
Expect.equals(i, 2);
}
// Check that the finally blocks rethrow JS exceptions, when a function directly throws (no `await`).
Future<void> jsExceptionFinallyPropagateAsyncDirect() async {
int i = 0;
try {
if (runtimeTrue()) {
try {
throwJSException();
} finally {
i += 1;
}
}
} catch (e) {
i += 1;
}
Expect.equals(i, 2);
}
// Catch JS exception and run type tests. Dummy `await` statements to generate
// suspension points before and after every statement. Type test should succeed
// in the same try-catch statement.
Future<void> jsExceptionTypeTest1() async {
bool exceptionCaught = false;
bool errorCaught = false;
try {
await yield_();
if (runtimeTrue()) {
try {
await yield_();
throwJSException();
await yield_();
} on Exception catch (_) {
await yield_();
exceptionCaught = true;
await yield_();
} on Error catch (_) {
await yield_();
errorCaught = true;
await yield_();
}
}
} catch (_) {
await yield_();
}
Expect.equals(exceptionCaught, false);
Expect.equals(errorCaught, true);
}
// Similar to `jsExceptionTypeTest1`, but the type test should succeed in a
// parent try-catch.
Future<void> jsExceptionTypeTest2() async {
bool exceptionCaught = false;
bool errorCaught = false;
try {
await yield_();
if (runtimeTrue()) {
try {
await yield_();
throwJSException();
await yield_();
} on Exception catch (_) {
await yield_();
exceptionCaught = true;
await yield_();
}
}
} on Exception catch (_) {
await yield_();
exceptionCaught = true;
await yield_();
} on Error catch (_) {
await yield_();
errorCaught = true;
await yield_();
}
Expect.equals(exceptionCaught, false);
Expect.equals(errorCaught, true);
}