No std wasm, various options?

what are the options for no_std wasm? how do they compare?

we know wasm32-unknown-unknown is incompatible with clang, but also we don't wanna have to deal with wasi bindings for our next project. it would be nice to not have to deal with them. (we do want alloc tho.)

We have wasm32-unknown-unknown, wasm32-wasi and wasm32-unknown-emscripten. if you need anything else you will need to define your own target using a target spec json file.

sure.

okay, let's say we wanna run a wasm parser inside wasm, but not have to deal with bringing our own wasi bindings. what are our options?

The practical options at the time being are wasm32-wasi, std's APIs, and use your wasm runtime's wasi implementation; or wasm32-unknown-unknown plus wasm_bindgen glue. Anything else is essentially entirely manual.

If you're into the extreme bleeding edge, you might try wit-bindgen (wasmtime) or wai-bindgen (wasmer), but these are still bindgen-likes.

(we don't have wasi bindings. the wasm runtime is custom.)

I'm missing something. If this is the end goal, where does the "no std" requirement in the title come from ?

I don't recall which crate exactly ( possibly https://crates.io/crates/wasmi ), but there are definitely "wasm parsers that can build on wasm32-unknown-unknown" crates around.

last we checked the only still-maintained wasmparser requires std. which makes it inconvenient for the runtime we're targeting, because we do not have wasi (and we don't wanna write our own wasi).

we're not familiar with no_std wasm32-wasi vs wasm32-unknown-unknown, which is why we're asking. tho we do know wasm32-unknown-unknown has a broken abi.

If the library you want to use requires std, your options are

  • Find or write a different library which doesn't require std.
  • Provide std.
    • On the wasm32-unknown-unknown target.
      • extern "C" doesn't agree with other languages' conventions when you define APIs not directly translatable to wasm (i.e. use non-primitive types).
      • std is largely subbed out, so just don't do those things.
    • On the wasm32-wasi target.
      • If code is stripped from the compilation for being dead, the imports aren't necessary; you only need to provide the wasi imports which actually get used.
      • Patching the wasm to delete functions which you know aren't used but the compiler didn't notice (e.g. to eliminate unwanted imports) is fairly common and simple to do with the binaryen tools.

Ultimately, it all depends on what interface you're trying to export from your wasm module. Wasm is still very "bring your own batteries," especially if you're trying to avoid the batteries other people are offering (e.g. wasi).

If you want a wasm target without any environment access, that's wasm32-unknown-unknown, and just dealing with not using extern "C" bridges between Rust and another language which disagree. Any communication to the host is already custom by necessity.

If you don't use std in wasm32-wasi, no wasi interfaces are imported. (The allocator is internal to the wasm module.) If you do use std, well, there's no magic way to just not use std.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.