I'm trying to compile my Rust code to wasi (using wit-bindgen-wasmtime::export!
) with the command:
cargo build --target wasm32-wasi --release
But I receive some errors from the region-2.2.0
package:
30 | os::lock(
| ^^^^ not found in `os`
60 | os::unlock(
| ^^^^^^ not found in `os`
16 | INIT.call_once(|| PAGE_SIZE = os::page_size());
| ^^^^^^^^^ not found in `os`
48 | os::set_protection(
| ^^^^^^^^^^^^^^ not found in `os`
133 | os::get_region(page::floor(address as usize) as *const u8)
| ^^^^^^^^^^ not found in `os`
Running cargo tree --invert region
I get:
region v2.2.0
├── wasmtime v0.34.1
│ └── wit-bindgen-wasmtime v0.1.0 (https://github.com/bytecodealliance/wit-bindgen#2f46ce4c)
│ └── thalo-bank-account v0.1.0 (/private/tmp/thalo-bank-account)
│ [build-dependencies]
│ └── thalo-bank-account v0.1.0 (/private/tmp/thalo-bank-account)
├── wasmtime-jit v0.34.1
│ └── wasmtime v0.34.1 (*)
└── wasmtime-runtime v0.34.1
├── wasmtime v0.34.1 (*)
└── wasmtime-jit v0.34.1 (*)
wit-bindgen-wasmtime is for host code, which is typically compiled natively for the host. You likely want to use wit-bindgen-rust instead, which is for wasm code.
I'm planning to compile to wasi and and execute it from rust again rather than the browser. If I'm compiling from Rust and running in Rust too, it should be wit-bindgen-wasmtime right?
wit-bindgen-wasmtime is for building host implementations meant to be linked into wasmtime. Are you using wasmtime?
Yes I was planning to run the wasm file with wasmtime, similar to the tutorial here: <https://docs.wasmtime.dev/examples-rust-wasi.html>
Ah. So, wasmtime itself is not something that can be compiled to WASI. It uses JIT compilation and other platform features which aren't supported in WASI.
If you want to run code in the browser, it's better to look for ways to be able to use the browser's wasm engine. If you need WASI support, something like https://github.com/bjorn3/browser_wasi_shim might help.
Hmm I don't need to run wasi in the browser. Essentially I am using wit_bindgen_rust::export("file.wit");
in one Rust library, and in another Rust binary I want to run it with wit_bindgen_rust::import("file.wit");
Ah, ok. To do that, you'll need wasmlink
Oh interesting. I remember trying to do the same thing I'm trying now a few months and I don't recall needing wasmlink.
My main goal is to let users write wasi modules which I can then run from my Rust code (which I thought would be done with wasmtime).
Is there a difference between wasm and wasi? I thought wasm is for running in the browser, and wasi is for running outside the browser
Roughly speaking, wasm is the platform ISA, while wasi is an OS API.
In browsers, you have wasm, but browsers don't provide the WASI APIs
Ah okay I realised my issue.
I was using wit-bindgen-gen-wasmtime
for the library, when I should've been using wit-bindgen-gen-rust-wasm
.
Ari Seyhun has marked this topic as resolved.
Ari Seyhun has marked this topic as unresolved.
When running cargo build --target wasm32-wasi --release
, I cannot see any wasm or wasi file in target/wasm32-wasi/release...
The only files are:
// some folders...
// These files:
my_lib.d
my_lib.rlib
Should I be compiling to wasm32-unknown-unknown instead? What is the point of compiling to wasm32-wasi?
It seems compining to wasm32-unknown-unknown also doesn't create a wasm file
I think I may be missing a [[lib]] section in my Cargo toml but I can't recall what I need to set it to.
Got it working with crate-type = ['cdylib']
Ari Seyhun has marked this topic as resolved.
Last updated: Dec 23 2024 at 12:05 UTC