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

Supported when compiling Flutter with --wasm? #8

Open
robmllze opened this issue Oct 17, 2024 · 12 comments
Open

Supported when compiling Flutter with --wasm? #8

robmllze opened this issue Oct 17, 2024 · 12 comments

Comments

@robmllze
Copy link

Hi mate,

Great package. I'd like to see more development going into this. I can contribute. Quick, question:

I managed to make some Rust code work, via lookupFunction. I can run my app through flutter run -d chrome, however, when I do flutter run -d chrome --wasm, the lookup throws an error and my function isn't found. Any ideas why this might be?

Looking forward to your response,

Robert

@vm75
Copy link
Owner

vm75 commented Oct 17, 2024

Hi Robert.
I haven't had much experience with Rust myself, but I understand the dart integration is very thorough.
I would like to know more about your setup. I haven't the slightest clue about the issue you are facing, but it will be good learning.
Just wanted to let you know that my contribution is very bursty. I will have a few days of furious development then a big lull.
Would love to collaborate with you on this.
Vijay

@CaptainDario
Copy link

@vm75 this woule be great, I compiled some c++ code using emscripten and it runs fine with flutter run -d chrome but the library cannot be loaded when running via flutter run -d chrome --wasm

@robmllze
Copy link
Author

robmllze commented Dec 6, 2024

Hi Vijay, sorry for only getting back at you now. @CaptainDario I started working on a solution...

The idea was to create a way to easily use Rust (or we can extend it to C++) with Flutter on the web. I looked at flutter_rust_bridge but they also have the same WASM issue.

This worked for me using WASM but it's waaaay incomplete and messy. But the point is, I proved it can be done.

https://github.com/robmllze/df_rust_interop_web

We can team up and make a proper interop! I understand the WASM basics now after a bit of investigation.

I would love to contribute on projects! I've been completely solo for years and it's getting lonely! XD

I have more packages here that I'm maintaining (trying) but it's a lot of unpaid work for a single dev!

https://github.com/DevCetra/
https://pub.dev/publishers/devcetra.com/packages

@vm75
Copy link
Owner

vm75 commented Dec 6, 2024

Thanks @robmllze . I will take a look. Time is on short supply for me as well.
Rust is very well integrated with Dart/Flutter, compared to C.
My main motivation was to enable Sweph

@vm75
Copy link
Owner

vm75 commented Dec 12, 2024

@CaptainDario @robmllze , i was able to run wasm on vanilla Dart as well as Flutter.
Please refer to the following:

  • Makefile in wasm_ffi/example on how to compile c to wasm
  • wasm_ffi/example on how to use for web
  • universal_ffi/example on how to use for all platforms

I am incorporating it into Sweph, and that will give an idea of how to create a Flutter plugin using universal_ffi

@vm75
Copy link
Owner

vm75 commented Dec 12, 2024

@robmllze
I was reading up on flutter run with '--wasm'. That one aims to compile Dart code as wasm (rather than use wasm module in Dart).
Can you clarify if you want to run your project in wasm mode and include an external wasm?

@vm75
Copy link
Owner

vm75 commented Dec 12, 2024

@CaptainDario , I think the wasms may be invisible to each other. There may not be an easy solution for this.

@CaptainDario
Copy link

CaptainDario commented Dec 12, 2024

This is awesome, thanks!

@CaptainDario , I think the wasms may be invisible to each other. There may not be an easy solution for this.

What do you mean by this?
That one wasm library cannot interact with another one ?

@robmllze
Copy link
Author

robmllze commented Dec 12, 2024

@vm75 Thanks for your help, however I'm already running my project with --wasm for performance.

The idea is, if we can add reusable .wasm modules to our code, we essentially can hide parts of the code from some of our temporary contractors who are working on the project. You can basically create proprietary packages without revealing the source code. Kind of like a DLL or shared library. We can also code part of the project in systems programming languages like Rust, specifically parts that Dart is always going to be slow at.

I was able to do a quick experiment and I successfully managed to get this process working. I haven't had time to turn this into a package yet.

The wasms all run on the main thread by default, and they can communicate via things like SharedArrayBuffer or you can just take the output of one function from a module and send it as input to another function in another module in Dart.

@vm75
Copy link
Owner

vm75 commented Dec 13, 2024

Thanks @robmllze . It is an interesting way to use wasm. I did realist that we can use dart and stitch multiple wasms together. It is a bummer that it is not straightforward to share memory between wasms (especially standalone wasm). I do plan to try some hacks there eventually.

@vm75
Copy link
Owner

vm75 commented Dec 13, 2024

What do you mean by this?
That one wasm library cannot interact with another one ?

@CaptainDario methods in once wasm cannot directly call into methods of another wasm. Even sharing memory between wasms is tricky. As @robmllze mentioned, there are some workarounds.

When using --wasm, I understand that the main dart code itself becomes a wasm. So I got confused on how it will share memory, etc. I will also play around with --wasm and see.

@iphydf
Copy link

iphydf commented Feb 10, 2025

wasm_ffi needs some changes for wasm-loading to work from dart2wasm. I've just spent a full day investigating this possibility and have concluded that it's more than a week of work for me (with zero experience in wasm FFI). Basically, what I find is that the "wasmExports" lookup looks very different, and the js_interop_unsafe return values are a bit different on the wasm target, so the first problem I ran into was JSValue can't be cast to () => JSValue. Fixing that, I run into more problems of the wasmExports value being different.

In any case, I'd love to see some progress here and I'm happy to test any potential improvements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants