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

Web support #589

Merged
merged 73 commits into from
Sep 7, 2022
Merged

Web support #589

merged 73 commits into from
Sep 7, 2022

Conversation

Desdaemon
Copy link
Contributor

@Desdaemon Desdaemon commented Jul 24, 2022

Closes #315. Continues from #386.

Additions:

  • --wasm flag to emit WASM-specific files
    • Requires --dart-decl-output
  • --inline-rust to inline platform modules into a single file
  • WASM shims for allo_isolate (merge upstream?)

Changes:

  • Migrate to clap v3

Internal changes:

  • Functions that used to pluck values from Opts now receive a reference to Opts

Checklist

  • New tests are added to ensure new features are working (probably by modifying the frb_example/pure_dart example).
  • All existing and new tests are passing.
  • If this PR adds/changes features, documentations (in book folder) are modified as well.

@Desdaemon Desdaemon changed the title Wasm workers Web support Jul 24, 2022
frb_rust/src/ffi.rs Outdated Show resolved Hide resolved
@fzyzcjy
Copy link
Owner

fzyzcjy commented Jul 24, 2022

Awesome job! Looking forward to merging it! - Feel free to ping me (or click "request review" button) when it is ready, cannot wait for that :)

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jul 24, 2022

Btw I see some refactors in the PR, e.g. migrate to clap, use bail instead of assert, etc. What about making it a separate PR such that we can merge it firstly? That will make this main PR smaller and more focused on one problem (the web support problem).

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jul 24, 2022

WASM shims for allo_isolate (merge upstream?)

May I know what to merge? Not see PR in allo_isolate yet. Anyway just bump cargo.toml in this pr is sufficient

@Desdaemon
Copy link
Contributor Author

WASM shims for allo_isolate (merge upstream?)

May I know what to merge? Not see PR in allo_isolate yet. Anyway just bump cargo.toml in this pr is sufficient

No, I'm still unsure if I should merge this code at all, it introduces a lot of dependencies to support just a single platform. Maybe if I could get some feedback later on the shims then I can consider merging the code.

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jul 25, 2022

it introduces a lot of dependencies to support just a single platform

Which dependencies? Maybe we can use cargo's conditional dependencies to make it optional

@Desdaemon
Copy link
Contributor Author

it introduces a lot of dependencies to support just a single platform

Which dependencies? Maybe we can use cargo's conditional dependencies to make it optional

Definitely, they are already target-gated to WASM. They are in frb_rust/Cargo.toml if you want to double check.

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jul 25, 2022

So, maybe I do a code review on this draft even before it is mark as done? Because this is a very nontrivial PR, and early feedback instead of feedbacks after everything is done can save time and work needed.

Copy link
Owner

@fzyzcjy fzyzcjy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is big work! I only reviewed part of it today and will look at it again later. As we all know code-review is critical to ensure code quality, so I will definitely review all, no worries ;) (And to avoid providing a ton of review to stress you...)

frb_codegen/src/commands.rs Outdated Show resolved Hide resolved
frb_codegen/src/config.rs Show resolved Hide resolved
frb_codegen/src/config.rs Show resolved Hide resolved
frb_codegen/src/lib.rs Outdated Show resolved Hide resolved
frb_codegen/src/lib.rs Outdated Show resolved Hide resolved
frb_codegen/src/lib.rs Outdated Show resolved Hide resolved
frb_codegen/src/lib.rs Outdated Show resolved Hide resolved
frb_rust/src/ffi.rs Outdated Show resolved Hide resolved
frb_rust/src/ffi.rs Outdated Show resolved Hide resolved
frb_rust/src/ffi.rs Outdated Show resolved Hide resolved
@fzyzcjy
Copy link
Owner

fzyzcjy commented Jul 25, 2022

Remark: No worries about the code review comments - I am just leaving them here to provide early feedback :)

Copy link
Owner

@fzyzcjy fzyzcjy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! Looked at it a bit deeper, and only find some minor points that may need a bit of discussions

frb_codegen/src/generator/dart/mod.rs Show resolved Hide resolved
frb_codegen/src/generator/dart/mod.rs Outdated Show resolved Hide resolved
frb_codegen/src/generator/dart/mod.rs Outdated Show resolved Hide resolved
frb_codegen/src/generator/dart/mod.rs Outdated Show resolved Hide resolved
frb_codegen/src/generator/dart/mod.rs Outdated Show resolved Hide resolved
frb_codegen/src/generator/dart/ty_optional.rs Outdated Show resolved Hide resolved
frb_codegen/src/ir/ty_enum.rs Outdated Show resolved Hide resolved
@Desdaemon
Copy link
Contributor Author

This PR is big work! I only reviewed part of it today and will look at it again later. As we all know code-review is critical to ensure code quality, so I will definitely review all, no worries ;) (And to avoid providing a ton of review to stress you...)

No I agree, it's a big one so I will also need to be more thorough than usual in my tests.

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jul 25, 2022

more thorough than usual in my tests.

Btw, we already have a ton of e2e tests (in the frb_dart .dart), so as long as we can run them in Web, we automatically get a lot of confidence :)

Copy link
Owner

@fzyzcjy fzyzcjy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little bit more review

frb_example/pure_dart/dart/lib/bridge_generated.web.dart Outdated Show resolved Hide resolved
frb_example/pure_dart/dart/lib/main.dart Outdated Show resolved Hide resolved
frb_example/with_flutter/rust/src/bridge_generated.web.rs Outdated Show resolved Hide resolved
frb_rust/src/handler.rs Outdated Show resolved Hide resolved
frb_rust/src/handler.rs Outdated Show resolved Hide resolved
frb_rust/src/handler.rs Outdated Show resolved Hide resolved
frb_codegen/src/ir/ty.rs Outdated Show resolved Hide resolved
frb_codegen/src/ir/ty_boxed.rs Outdated Show resolved Hide resolved
frb_codegen/src/ir/ty_delegate.rs Outdated Show resolved Hide resolved
frb_codegen/src/ir/ty_delegate.rs Outdated Show resolved Hide resolved
frb_rust/src/pool.rs Outdated Show resolved Hide resolved
@fzyzcjy
Copy link
Owner

fzyzcjy commented Jul 26, 2022

Btw I am thinking about posting the news (frb supports web) after this PR is merged in Rust & Dart subreddit, since this new feature strengthens the lib a lot :) What do you think?

@mz2
Copy link

mz2 commented Jul 27, 2022

Hopefully my comment is more helpful than unhelpful given the PR is still in the works and I'm aware that the documentation is not yet updated: I got curious and tried installing and running flutter_rust_bridge_codegen off the source branch of this PR (flutter_rust_bridge_codegen --wasm --rust-input frb_example/with_flutter/rust/src/api.rs --dart-output frb_example/with_flutter/lib/bridge_generated.dart --dart-decl-output frb_example/with_flutter/lib/bridge_definitions.dart), resulting in lib/bridge_definitions.dart, lib/bridge_generated.dart, lib/bridge_generated.web.dart that looked similar to what's on the branch already, with different formatting applied (presuming a Dart SDK version difference).

After this I successfully ran frb_example/with_flutter, on macOS and Linux (repeated the same steps on both)... but on both platforms, attempting cargo run -d chrome results in a bunch of errors:

lib/main.dart:2:8: Error: Not found: 'dart:ffi'
import 'dart:ffi';

lib/bridge_generated.dart:11:8: Error: Not found: 'dart:ffi'
import 'dart:ffi' as ffi;
       ^
lib/bridge_generated.dart:16:40: Error: Type 'ffi.DynamicLibrary' not found.
  factory FlutterRustBridgeExampleImpl(ffi.DynamicLibrary dylib) =>

... (goes on quite a while)...

lib/bridge_generated.dart:694:16: Error: Only JS interop members may be 'external'.
Try removing the 'external' keyword or adding a JS interop annotation.
  external int len;

Again, hopefully this is of use 😄

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jul 27, 2022

@mz2 Judging from the errors, seems that, when running the web platform, the example fail to use conditional import to completely avoid importing any files that uses native feature (i.e. dart:ffi).

@mz2
Copy link

mz2 commented Jul 28, 2022

(Continuing noting my observations, let me know if this is just too early / not even supposed to work and I should not bother yet!)

Judging from the errors, seems that, when running the web platform, the example fail to use conditional import to completely avoid importing any files that uses native feature (i.e. dart:ffi).

Yep, that was it, thanks. The example doesn't take care of conditional imports yet. I was able to get the example application to run with the following (not sure if there is a more idiomatic solution, or some import that would itself take care of this):

import 'package:flutter_rust_bridge_example/bridge_generated.dart'
    if (dart.library.html) 'package:flutter_rust_bridge_example/bridge_generated.web.dart';
import 'dart:ffi' if (dart.library.html) './dylib_stub.dart';

...where dylib_stub.dart is something I made just as a test just to see it build, with the stub fullfilling the relevant API of DynamicLibrary that is referenced in main.dart (the process, executable, open factory constructors -- stub just throws errors from each of these since it's just needed to make the compiler happy):

late final dylib = kIsWeb
    ? {} as WasmModule // just put this here to get it to build as a test, since I have not worked out how to generate the wasm module successfully for the example...
    : Platform.isIOS
        ? DynamicLibrary.process()
        : Platform.isMacOS
            ? DynamicLibrary.executable()
            : DynamicLibrary.open(path);
late final api = FlutterRustBridgeExampleImpl(dylib as dynamic);

Again, I am guessing there's an abstraction intended to solve this either already somewhere or coming up. Like this the code runs but of course doesn't work right yet, since I don't know how to get the wasm for the example built successfully. Is that meant to work? I tried the following, issued in frb_example/with_flutter/rust:

> wasm-pack build --target web
[INFO]: 🎯  Checking for the Wasm target...
[INFO]: 🌀  Compiling to Wasm...
   Compiling flutter_rust_bridge_example v0.1.0 (/Users/mz2/Developer/flutter_rust_bridge/frb_example/with_flutter/rust)
error[E0308]: mismatched types
   --> src/bridge_generated.web.rs:215:28
    |
215 |         let vec: Vec<u8> = self.wire2api();
    |                  -------   ^^^^^^^^^^^^^^^ expected struct `Vec`, found struct `String`
    |                  |
    |                  expected due to this
    |
    = note: expected struct `Vec<u8>`
               found struct `String`

error[E0308]: mismatched types
   --> src/bridge_generated.web.rs:233:51
    |
233 |             let wrap = support::box_from_leak_ptr(self);
    |                                                   ^^^^ expected *-ptr, found struct `Box`
    |
    = note: expected raw pointer `*mut _`
                    found struct `Box<[flutter_rust_bridge::JsValue]>`

error[E0609]: no field `ptr` on type `Box<_>`
   --> src/bridge_generated.web.rs:234:45
    |
234 |             support::vec_from_leak_ptr(wrap.ptr, wrap.len)
    |                                             ^^^ unknown field

...

error[E0609]: no field `ptr` on type `Box<_>`
   --> src/bridge_generated.web.rs:281:45
    |
281 |             support::vec_from_leak_ptr(wrap.ptr, wrap.len)
    |                                             ^^^ unknown field

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jul 28, 2022

if (dart.library.html)

That is what I expected

For other errors, I guess it is because the work is still WIP - the wire types have not been constructed fully yet. I am just guessing and @Desdaemon is the expert on this question, surely :)

@mz2
Copy link

mz2 commented Jul 28, 2022

Awesome, well thanks a lot for all your effort on this, I can't wait to give this a spin and I bet I'm not the only one :-)

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jul 28, 2022

I can't wait to give this a spin and I bet I'm not the only one :-)

You are right - I also can't wait to try it :)

@Desdaemon
Copy link
Contributor Author

Should be the last batch of changes to be reviewed by @fzyzcjy before merging.

@fzyzcjy
Copy link
Owner

fzyzcjy commented Sep 6, 2022

Excellent job! Now we are very close to the exciting moment of adding this big feature :) I will try to have as little issues as possible, and defer to future backlog as well.

  • Look at all (resolved) comments again since may miss some of them
  • Review the code briefly again

Copy link
Owner

@fzyzcjy fzyzcjy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe in you, so only roughly look at the code and do not check the details :)

frb_dart/test/io_typed_data.dart Outdated Show resolved Hide resolved
frb_rust/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Owner

@fzyzcjy fzyzcjy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All done!

Only 4 conversations (2 new, 2 reopened), with no more than file or function renaming or removing a comment line etc.

I will sleep now and check inbox after ~8 hours :)

Desdaemon and others added 3 commits September 6, 2022 20:28
- Rename `wasm2api_body` to `wire2api_jsvalue`
- Run library unit tests in CI
- Update Scoop manifest
@fzyzcjy
Copy link
Owner

fzyzcjy commented Sep 7, 2022

Ready to merge! (After #678 since otherwise cannot release a new version with CI passing)

@fzyzcjy
Copy link
Owner

fzyzcjy commented Sep 7, 2022

Ah if you are resolving conflicts then I do not do it

Anyway the code is OK and will be merged once master turns green

@fzyzcjy fzyzcjy merged commit c687acb into fzyzcjy:master Sep 7, 2022
@fzyzcjy
Copy link
Owner

fzyzcjy commented Sep 7, 2022

🎉

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tracking issue for web support
4 participants