Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Support wasm32 environments that are not related to browsers #2532

Open
oblique opened this issue Aug 2, 2023 · 0 comments
Open

Support wasm32 environments that are not related to browsers #2532

oblique opened this issue Aug 2, 2023 · 0 comments

Comments

@oblique
Copy link
Contributor

oblique commented Aug 2, 2023

Why?

I'm working in a project that ethers will be used within Internet Computer environment, which is using WebAssembly.

The issue

What I'm going to describe is an issue in various projects in Rust ecosystem and ethers happens to have it too. At the end I will propose a solution (which I will implement), however it is not going to be backward compatible in the case of wasm32 targets.

wasm32 is an architecture but it does not describe the environment that is going to be used. Most projects assume that the environment is always a browser. Within browser environment it is valid to use wasm-bindgen, js-sys, and web-sys, but this is not valid in any other environments. To be more correct wasm-bindgen and js-sys can be used only in environments with JavaScript and web-sys in browser environments.

getrandom solved this issue by requiring from the user to enable the js feature flag manually. Because of this a user needs to add the feature in the top-level crate otherwise they will get a compilation error.

Proposal

In my opinion something similar needs to be done in ethers too. I'm proposing the following:

  • Introduce two new feature gates: web-sys and ic (for Internet Computer).
  • Both feature gates must be disabled by default.
  • In case of wasm32-unknown-unknown only one of the flags must be enabled. If none of them or both of them are enabled then we trigger compile_error!.
  • All wasm32 dependencies must be optional and feature flags will enable what is needed.
  • reqwest and futures-timer crates have the same issue so we need to do the following:
    • In case of wasm32 we have them as optional and we enable them with web-sys flag.
    • In case of not(wasm32) we have them as required.
    • In case of wasm32 and ic flag:
      • reqwest will be replaced with ic-cdk.
      • futures-timer will be replaced with ic-cdk-timers.
      • wasm-bindgen-futures will be replaced with ic-cdk.
  • Internet Computer does not support WebSockets, so implementation will not be provided if ic is enabled.

Other notes

  • Should I create a crate for this compatibility layer? If yes, do you have any name in mind?
  • web-sys can also be named as browser. The js is not a good option since web-sys is used by reqwest.
  • All the above can be solved within reqwest and futures-timer, but requires much longer effort, which I plan to do in the future.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant