3

We are exploring how to wrap part of the runtime logic into a re-usable WASM blob that can be wrapped in a JS library. To do that, we are investigating using wasm-pack and wasm-bindgen. Unfortunately, wasm-bindgen does not support compiling for no_std environment, hence we are in the need of using the std feature while targeting the wasm32-unknown-unknown architecture.

As I see, this does not currently seem to be possible because of the assumption that compiling for wasm32-unknown-unknown means compiling the runtime, which in this case is not true. Is there a way to do this? Or is bugging wasm-bindgen people to support no_std the best shot we have?

error: the wasm32-unknown-unknown target is not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support
   --> /home/dev/.cargo/registry/src/index.crates.io-6f17d22bba15001f/getrandom-0.2.8/src/lib.rs:263:9
    |
263 | /         compile_error!("the wasm32-unknown-unknown target is not supported ...
264 | |                         default, you may need to enable the \"js\" feature...
265 | |                         For more information see: \
266 | |                         https://docs.rs/getrandom/#webassembly-support...
    | |________________________________________________________________________^

error[E0433]: failed to resolve: use of undeclared crate or module `imp`
   --> /home/dev/.cargo/registry/src/index.crates.io-6f17d22bba15001f/getrandom-0.2.8/src/lib.rs:290:5
    |
290 |     imp::getrandom_inner(dest)
    |     ^^^ use of undeclared crate or module `imp`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `getrandom` (lib) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
4
  • I have the same question. substrate.stackexchange.com/questions/7710/… Apr 18, 2023 at 13:04
  • Hey, I see your question is trying to compile the runtime without the default features. I am trying to compiling with the std feature, so it's not really the same question... Apr 19, 2023 at 14:03
  • The runtime is already a WASM blob, it is located in target/release/wbuild/polkadot-runtime/polkadot_runtime.wasm. How is your use-case different? Apr 19, 2023 at 17:57
  • wasm-bindgen requires std, hence the only way to expose the runtime blob to JS is by building a second WASM blob with std support that wraps around the runtime blob. So I was wondering wether the runtime itself can already by compiled with std support and is hence supported in wasm-bindgen. Apr 20, 2023 at 6:08

1 Answer 1

2

I think you should extract that logic out.

For example,

pallet-a -> pallet-a + primitives-a/impls-a. Get rid of the pallet/runtime framework. Then compile the primitives-a/impls-a to WASM.

4
  • Yes, that is definitely something to do 😄 But compiling even just a simple crate that only depends on codec, scale-info and sp-std fails if the combination of --features std and target wasm32-unknown-unknown is used, so I assume by design it's not possible to target WASM with std feature on, at least not in a way that I would be able to understand. Apr 21, 2023 at 6:31
  • You can only enable a few std things in the WASM, and you need to tweak it yourself. Because it is WASM, it runs in the browser in the sandbox. For example, there is no std::io at all in that envoiroment. So, you can't just enable the std directly. Apr 21, 2023 at 6:37
  • My understanding is that you can decide how much "sandboxed" you want your WASM to be. There could be cases where it makes sense to have std capabilities in a WASM executable. But as far as I understand Parity does not support that case in any way. Apr 21, 2023 at 12:52
  • So, you'd better ask this in the Rust WASM community? It looks like it's a general question. Sorry, I have no more idea about this. Apr 21, 2023 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.