How to make rust crate with C lib dependency "wasm32-unknown-unknown compatible"

Hi, I have problem about C, Rust and WASM. There is a C lib called libspot, which implement an algorithm with pure C(without c stdlib). I wrap it by Rust ffi, and built a lib crate named with libspot too(GitHub - shenxiangzhuang/libspot-rs: A Rust wrapper for the libspot). To be clear, let's call the wrapper crate with libspot-rs(just as repo name). Based on the libspot-rs, I built another lib crate, let's just call it X.

The problem is that I can not add wasm binding for the X crate, because it dependents on libspot-rs, and libspot-rs use C lib libspot by ffi.

All I want is that use libspot-rs as a regular pure rust crate and can add wasm binding by wasm-bindgen normally. And I want to know how to do this. Any suggestion would be greatly appreciated!

For references: the orignal libspot c lib already has a wasm binding and has beed published to npm.

1 Like

Just a silly idea: maybe you could convert libpost to Rust with C2Rust?

I'm very curious what the answer to this will be.

Or maybe, another solution would be to use the existing libspot WASM binding, and calling it from your generated WASM code? Not sure how that could be possible. Can a WASM program call another WASM blob? If so, that might be the solution :thinking:

Provided that you have a C compiler capable of compiling for wasm (eg clang), it should be possible to just use C code from wasm32-unknown-unknown like you would for native platforms starting from rustc 1.89 (which is currently in beta). Before that there were ABI incompatibilities: Remove wasm legacy abi by bjorn3 · Pull Request #133952 · rust-lang/rust · GitHub

2 Likes

Thanks for your suggestion. Could you please provide some tutorial about this? I build with rust nightly and it build well, but the generated js file first line with import * as __wbg_star0 from 'env';, which lead to the fellowing error:

Uncaught TypeError: Failed to resolve module specifier "env". Relative references must start with either "/", "./", or "../".

It seems switch to nightly doesn't change the error. Is there any suggestion about this error?

Ps: the wasm-bindgen version is v0.2.100, I don't know if the rustc 1.89 version feature need wasm-bindgen's support or not.

I think the original C implementation is pretty solid, so I won't re-impl it if there has other choice:)

1 Like

Yes, this should be a solution in a way, but I don't find related resources about this.

1 Like

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.