I have Rust code which I am compiling to wasm, and running that wasm file from wasmtime
, wasmtime_wasi
and wit_bindgen_wasmtime::import
.
I built the wasm file just fine with cargo build --target wasm32-wasi --release
, though when trying to run my wasm in a Rust host, I get the following error:
Error: unknown import: `canonical_abi::resource_new_aggregate` has not been defined
My wit file contains:
variant error {
command(list<u8>),
deserialize-event,
deserialize-command,
}
resource aggregate {
static new: function(id: string) -> aggregate
apply-events: function(events: list<list<u8>>) -> expected<unit, error>
handle-command: function(command: list<u8>) -> expected<list<list<u8>>, error>
}
And I am attempting to call the functions with wit_bindgen_wasmtime::import!("./domain.wit");
and:
let (domain, _instance) =
Domain::instantiate(&mut store, &module, &mut linker, move |ctx| &mut ctx.domain)?;
let state = domain.aggregate_new(&mut store, "john-doe")?;
let events = domain.aggregate_handle_command(
&mut store,
&state,
br#"{"command":"open_account","params":{"initial_balance":10.0}}"#,
)??;
domain.aggregate_apply_events(
&mut store,
&state,
&events
.iter()
.map(|event| event.as_ref())
.collect::<Vec<_>>(),
)??;
Strangely I had this working some months ago, but when coming back to it, it doesn't seem to work. I've also had to rename domain.new_instance
to domain.aggregate_new
, domain.handle_command
to domain.aggregate_handle_command
and domain.apply_events
to domain.aggregate_apply_events
.
I had posted a code snippet for my future self here: https://github.com/thalo-rs/esdl/tree/main/examples/bank-account-wasm
But like I said, I've had to rename those new_instance
, handle_command
and apply_events
from the code snippet.
I tried using wit from a locked commit d5c8b3960d1bc3a7b7f312991e5923b7fa2a3495
, though that didn't change anything.
I think the renaming part I mentioned should be disregarded. That's just because I switched to use a wit resource.
Though the initial error of canonical_abi::resource_new_aggregate has not been defined
is still an issue
It seems I had this exact issue before and posted the solution already :face_palm:
https://github.com/bytecodealliance/wit-bindgen/issues/179
Ari Seyhun has marked this topic as resolved.
Last updated: Dec 23 2024 at 12:05 UTC