wit-bindgen icon indicating copy to clipboard operation
wit-bindgen copied to clipboard

Associated resource functions not generated if they are imported?

Open Hoverbear opened this issue 3 years ago • 2 comments
trafficstars

I've been exploring wit-bindgen lately and was trying to use resources.

Given some demo.wit:

resource doggo {
    static new: function(name: string) -> doggo
    bark: function() -> string
}

I can call wit-bindgen wasmtime -i demo.wit and get:

// ...
    pub fn doggo_new(&self, mut caller: impl wasmtime::AsContextMut<Data = T>,name: & str,)-> Result<Doggo, wasmtime::Trap> {
      let func_canonical_abi_realloc = &self.canonical_abi_realloc;
      let memory = &self.memory;
      let vec0 = name;
      let ptr0 = func_canonical_abi_realloc.call(&mut caller, (0, 0, 1, (vec0.len() as i32) * 1))?;
      memory.data_mut(&mut caller).store_many(ptr0, vec0.as_ref())?;
      let (result1_0,) = self.doggo_new.call(&mut caller, (ptr0, vec0.len() as i32, ))?;
      let handle2 = (self.get_state)(caller.as_context_mut().data_mut()).index_slab0.remove(result1_0 as u32)?;
      Ok(Doggo(handle2))
    }
    pub fn doggo_bark(&self, mut caller: impl wasmtime::AsContextMut<Data = T>,self_: & Doggo,)-> Result<String, wasmtime::Trap> {
      let func_canonical_abi_free = &self.canonical_abi_free;
      let memory = &self.memory;
      
      let obj0 = self_;
      (self.get_state)(caller.as_context_mut().data_mut()).resource_slab0.clone(obj0.0)?;
      let handle0 = (self.get_state)(caller.as_context_mut().data_mut()).index_slab0.insert(obj0.0);
      let (result1_0,) = self.doggo_bark.call(&mut caller, (handle0 as i32, ))?;
      let load2 = memory.data_mut(&mut caller).load::<i32>(result1_0 + 0)?;
      let load3 = memory.data_mut(&mut caller).load::<i32>(result1_0 + 8)?;
      let ptr4 = load2;
      let len4 = load3;
      
      let data4 = copy_slice(
      &mut caller,
      memory,
      ptr4, len4, 1
      )?;
      func_canonical_abi_free.call(&mut caller, (ptr4, len4 * 1, 1))?;
      Ok(String::from_utf8(data4)
      .map_err(|_| wasmtime::Trap::new("invalid utf-8"))?)
    }
// ...

An outline of the generated code looks like:

image

However if I create a demo-import.wit and then use the resource in that:

use { doggo } from demo

demo: function(d: doggo) -> doggo

It does not generate the functions above (when I run wit-bindgen wasmtime -i demo-import.wit ). An outline of the generated code looks like:

image

Further, I cannot use wit-bindgen with both as imports:

ana@autonoma:~/git/zombodb/plrust$ RUST_BACKTRACE=1 wit-bindgen wasmtime -i demo.wit -i demo-import.wit
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `11`,
 right: `1`', crates/gen-wasmtime/src/lib.rs:1166:28
stack backtrace:
   0: rust_begin_unwind
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c/library/core/src/panicking.rs:143:14
   2: core::panicking::assert_failed_inner
   3: core::panicking::assert_failed
   4: <wit_bindgen_gen_wasmtime::Wasmtime as wit_bindgen_gen_core::Generator>::finish_one
   5: wit_bindgen_gen_core::Generator::generate_all
   6: wit_bindgen::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Am I perhaps using it wrong?

Hoverbear avatar May 02 '22 17:05 Hoverbear

Yes unfortunately the use feature in *.wit files is pretty underbaked, especially around resources, to the point that it probably doesn't really work at all. I was actually just in the process of writing up current issues with wit-bindgen and the use feature is definitely on that list of things that need more work and figuring out.

alexcrichton avatar May 03 '22 14:05 alexcrichton

I noticed that use statements seemed to behave a bit odd! Thanks Alex.

Hoverbear avatar May 03 '22 16:05 Hoverbear

I'm going to close this in favor of https://github.com/bytecodealliance/wit-bindgen/issues/266 to consolidate "use doesn't work as expected" in one place.

alexcrichton avatar Sep 06 '22 18:09 alexcrichton