Files
sdk/pkg/dart2wasm/test/ir_tests/try_blocks.dart
Ömer Ağacan 590b656877 [dart2wasm] Update JS exception catching
This updates JS exception catching as discussed in #55481:

- Only catch JS exceptions when the exception type is `dynamic`,
  `Object`, or an extension of `JSValue`. (nullable or not)

  (Previously we also caught JS exceptions when the type is `Error`.)

- When the JS value caught in Wasm is `null` or `undefined`, box it as a
  non-interop class. For compatibility with dart2js, this class is
  copied from dart2js and has the same `toString` as the dart2js class.

- In other cases: box the JS values as `JSValue`. This means the value
  can be passed as any of the interop types, and can be passed back to
  JS without manual jsification.

Fixes #55481.

Issue: https://github.com/dart-lang/sdk/issues/55481
Change-Id: I23e73074729f740b90df2ca8b3c713fb39966556
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/479640
Reviewed-by: Srujan Gaddam <srujzs@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2026-02-24 01:43:10 -08:00

59 lines
1.2 KiB
Dart

// Copyright (c) 2026, 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.
// functionFilter=tryBlocks.*
// tableFilter=NoMatch
// globalFilter=NoMatch
// typeFilter=NoMatch
// compilerOption=--no-minify
// Tests Wasm `catch` block tags based on Dart types being caught.
import 'dart:js_interop';
void main() {
tryBlocks1();
tryBlocks2();
tryBlocks3();
}
// Catch `JSAny`: this should generate a Wasm `try` that catches both Dart and
// JS exceptions.
@pragma('wasm:never-inline')
void tryBlocks1() {
try {
f();
} on JSAny {
print("Caught JSAny");
}
}
// Catch `Object`: same as above.
@pragma('wasm:never-inline')
void tryBlocks2() {
try {
f();
} on Object {
print("Caught Object");
}
}
// Catch a non-interop type: this shouldn't catch JS exceptions, so the Wasm
// code should only catch the Dart exception tag.
@pragma('wasm:never-inline')
void tryBlocks3() {
try {
f();
} on Error {
print("Caught Error");
}
}
@pragma('wasm:never-inline')
void f() {
if (int.parse('1') == 0) {
throw "Hi";
}
}