Skip to content

feat: Support compilation as wasm32-unknown-unknown - #312

Draft
dhdaines wants to merge 6 commits into
garvys-org:mainfrom
dhdaines:wasm_unknown_compilation
Draft

feat: Support compilation as wasm32-unknown-unknown#312
dhdaines wants to merge 6 commits into
garvys-org:mainfrom
dhdaines:wasm_unknown_compilation

Conversation

@dhdaines

@dhdaines dhdaines commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Currently in order to get rustfst to run in the browser it's necessary to build it as WASI and then stub out the imported WASI symbols (using https://github.com/typst-community/wasm-minimal-protocol/tree/main/crates/wasi-stub for instance)

This is suboptimal since it adds an extra build step and retains useless code in the binaries. But unfortunately it's not actually possible to build rustfst-ffi for the bare metal WebAssembly target wasm32-unknown-unknown for a couple of reasons:

  1. randgen requires getrandom which doesn't work on wasm32-unknown (there is a way around this but it's discouraged for libraries: https://docs.rs/getrandom/latest/getrandom/#webassembly-support)
  2. ffi-convert doesn't compile for wasm32-unknown-unknown because of a (useless) dependency on libc (see fix: remove dep on libc to make wasm unknown work sonos/ffi-convert-rs#68), and libc is also (uselessly) used in a lot of places in rustfst-ffi
  3. obviously there are a few file I/O operations for loading and saving symbol tables and FSTs and such!

(I realize using libc is probably leftover from when std::ffi and core::ffi weren't very good, as the now very dated "Rust FFI Omnibus" suggests to do)

This PR adds a std default feature which compiles these things that require stdio. So to build for wasm32-unknown-unknown you can omit the default features, the same pattern as we see elsewhere:

cargo build --package rustfst-ffi --no-default-features --target wasm32-unknown-unknown

(--features rustfst-state-label-u32 is unnecessary since wasm32 is 32)
(doesn't work for the top-level build because Reasons)

Because there isn't yet a version of ffi-convert that compiles, and when it will be released it will require Rust 2024, and I'm not sure if you want to require that here, I've simply vendored a fixed version of it since the license is totally compatible.

Note: changing libc::size_t to usize has revealed that ... why exactly are you using usize for all those things in rustfst-ffi? Possibly for some of them it should be the same type as state IDs which I think is either usize or u32 (of course these are the same thing on WebAssembly!)

@dhdaines
dhdaines marked this pull request as draft July 23, 2026 22:22
@dhdaines

Copy link
Copy Markdown
Contributor Author

Changed to draft! No longer necessary to vendor ffi-convert-rs as it has been updated, so I will update this as soon as possible: sonos/ffi-convert-rs#68 (comment)

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

Successfully merging this pull request may close these issues.

1 participant