Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dart2wasm] JS errors can't be caught using an interop type #55481

Open
srujzs opened this issue Apr 16, 2024 · 2 comments
Open

[dart2wasm] JS errors can't be caught using an interop type #55481

srujzs opened this issue Apr 16, 2024 · 2 comments
Labels
area-dart2wasm Issues for the dart2wasm compiler. area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. web-js-interop Issues that impact all js interop

Comments

@srujzs
Copy link
Contributor

srujzs commented Apr 16, 2024

import 'dart:js_interop';

@JS()
external void eval(String code);

@JS()
external void throwFunction();

void main() {
  eval('''
    self.throwFunction = function() {
      throw new Error('hello world');
    }
  ''');
  try {
    throwFunction();
  } on JSObject catch (e) {
    print(e);
  }
}

DDC and dart2js catch the JS error as it is a subtype of interceptors.JavaScriptObject (and therefore a subtype of JSObject). dart2wasm does not, as its implementation of JS errors is not a subtype of JSObject:

class _JavaScriptError extends Error {
. A proposal is to make that implementation a subtype of JSValue, but it's not clear if a Wasm instruction exists to convert the error to an externref that can then be boxed. Making this a subtype of JSValue also has the other issue that if we replaced JSObject with JSString, dart2wasm would catch the error (because JSString is a JSValue) whereas DDC and dart2js will not.

For now, users should avoid catching JS errors using an interop type for consistency.

@srujzs srujzs added web-js-interop Issues that impact all js interop area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. area-dart2wasm Issues for the dart2wasm compiler. labels Apr 16, 2024
@srujzs srujzs changed the title JS errors can't be caught using an interop type [dart2wasm] JS errors can't be caught using an interop type Apr 16, 2024
@srujzs
Copy link
Contributor Author

srujzs commented Apr 16, 2024

Another alternative is simply linting/disallowing such a catch syntax as it relies on a runtime check of interop types.

@osa1
Copy link
Member

osa1 commented Apr 17, 2024

but it's not clear if a Wasm instruction exists to convert the error to an externref that can then be boxed

This will be possible with WebAssembly/exception-handling#269. Chrome already supports it, but the latest Firefox (125.0.1) doesn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dart2wasm Issues for the dart2wasm compiler. area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. web-js-interop Issues that impact all js interop
Projects
Status: No status
Development

No branches or pull requests

2 participants