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

Uncaught RuntimeError: illegal cast - when trying to access string passed from Javascript to dart wasm #55715

Closed
akalankavinda opened this issue May 14, 2024 · 4 comments
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-question A question about expected behavior or functionality web-js-interop Issues that impact all js interop

Comments

@akalankavinda
Copy link

akalankavinda commented May 14, 2024

I have exported the following function in my dart wasm code

@pragma("wasm:export")
void foo(JSString x) {
   print(x.toString());
}

but whe I try to invoke this method from the javascript side,
I get the following error:

image

It points out to the following place in wasm library

image

Am I doing this the wrong way or is this an issue with compiler ?

PS: I was playing around with the following repo
https://github.com/mit-mit/sandbox/tree/main/demos/webwasm

@srujzs
Copy link
Contributor

srujzs commented May 14, 2024

Prefer using Function.toJS to export a Dart function.

Here's an example:

import 'dart:js_interop';

@JS()
external set foo(JSFunction f);

void main() {
  void f(JSString x) {
   print(x.toString());
  }
  foo = f.toJS;
}

Here, we wrap a Dart function with a JS function and set it in the property foo in globalThis. So, you can call globalThis.foo('hello world') in JS after this function is set.

@lrhn
Copy link
Member

lrhn commented May 14, 2024

(Maybe also use x.toDart instead of x.toString() to convert a JSString to a Dart String.)

@srujzs
Copy link
Contributor

srujzs commented May 14, 2024

I missed that. :) Yes, convert it back into a String using toDart. toString will likely do the right thing anyways, but prefer the conversion.

@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. type-question A question about expected behavior or functionality labels May 14, 2024
@akalankavinda
Copy link
Author

akalankavinda commented May 15, 2024

Thanks @srujzs @lrhn for the support.

With your suggestions, I modified my code as follows

import 'dart:js_interop';

@JS()
external set _onKeyup(JSFunction fn);

void main() {
  _onKeyup = _onKeyupDart.toJS;
}

void _onKeyupDart(JSString val) {
  print(val.toDart);
}

Now from the JavaScript side _onKeyup() is globally available and it is working as expected ✅✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-question A question about expected behavior or functionality web-js-interop Issues that impact all js interop
Projects
None yet
Development

No branches or pull requests

3 participants