Stream: wasm

Topic: ✔ wasmlink import export difference


view this post on Zulip Scott Waye (Aug 05 2021 at 16:05):

The example for wasmlink with the renderer and the markdown. In the markdown module the export is

 (export "render" (func $render.command_export))

with a type of

(func $render.command_export (type 7) (param i32 i32) (result i32)

In the renderer the import is

(import "markdown" "render" (func $renderer::markdown::render::witx_import::h556dc1094c69443f (type 8)))

with a type of

 (type (;8;) (func (param i32 i32 i32)))

Why does the import take the result from a param and not a result?. Is the result a pointer to a block of 2 i64s?

view this post on Zulip Scott Waye (Aug 05 2021 at 16:06):

I mean the last parameter is that a pointer to 2 i64s that are populated by the markdown module, or is it a pointer to a pointer to 2 i64s and the pointer is supplied by the markdown/linker?

view this post on Zulip Scott Waye (Aug 05 2021 at 16:14):

Forgot the online tool which is super useful, thanks for that. I see its the first option

      #[link(wasm_import_module = "input")]
      extern "C" {
        #[cfg_attr(target_arch = "wasm32", link_name = "hello")]
        #[cfg_attr(not(target_arch = "wasm32"), link_name = "input_hello")]
        fn witx_import(_: i32, _: i32, _: i32, );
      }
      witx_import(ptr0, len0, ptr1);
      let len2 = *((ptr1 + 8) as *const i32) as usize;
      String::from_utf8(Vec::from_raw_parts(*((ptr1 + 0) as *const i32) as *mut _, len2, len2)).unwrap()
    }
  }
  static mut RET_AREA: [i64; 2] = [0; 2];

view this post on Zulip Peter Huene (Aug 05 2021 at 17:03):

Hi @Scott Waye. wasmlink is expecting the canonical ABI that witx-bindgen uses for its imports and exports. For functions that return "multiple values" (i.e. anything that can't fit in a single wasm value type, which a list is considered to be because it returns a pointer-length pair), the importer (caller) passes in an additional argument that is a pointer to an array of 64-bit values and the exporter (callee) returns a pointer to an array of 64-bit values of the same length. This facilitates the adapter code that sits between the caller and callee as it does not need to explicitly allocate any space in either linear memory; the code generated by witx-bindgen handles allocating the return value space and the code generated by wasmlink ensures that what's returned by the callee is marshaled correctly to the caller.

view this post on Zulip Scott Waye (Aug 05 2021 at 17:06):

and the utf8 array, is that memcpy'ed between the 2?

view this post on Zulip Scott Waye (Aug 05 2021 at 17:06):

no wait, scrap that.

view this post on Zulip Peter Huene (Aug 05 2021 at 17:06):

yes, the adapter code that wasmlink generates calls into each module to allocate space from their linear memories and uses memory.copy instructions to memcpy the memory between the two linear memories

view this post on Zulip Scott Waye (Aug 05 2021 at 17:07):

ah, ok, that's why it needs canonical_abi_realloc (which I'll need to create on the c# side)

view this post on Zulip Peter Huene (Aug 05 2021 at 17:07):

that's right

view this post on Zulip Scott Waye (Aug 05 2021 at 17:07):

thanks

view this post on Zulip Notification Bot (Aug 05 2021 at 17:08):

Scott Waye has marked this topic as resolved.

view this post on Zulip Till Schneidereit (Aug 05 2021 at 17:44):

one thing to note is that there'll be a bit more churn on the ABI before this is all fully stabilized. For example, we already know that the way short reads/writes are facilitated will change a bit, from the push/pull buffers right now to a streams based interface. We're aiming to wrap this phase of churn up as quickly as we can, though

view this post on Zulip Scott Waye (Aug 05 2021 at 17:45):

thanks, no problem, I'm just playing. wasmlink is completing without errors so made some progress.

view this post on Zulip Scott Waye (Aug 05 2021 at 23:09):

Thanks for the help. I've successfully started with a dotnet string, sent it to rust, reversed it, and passed it back. Ship it!
image.png

view this post on Zulip Peter Huene (Aug 05 2021 at 23:10):

super cool work! :)

view this post on Zulip Peter Huene (Aug 05 2021 at 23:11):

is this using mono to target wasm?

view this post on Zulip Scott Waye (Aug 05 2021 at 23:12):

No, this is NativeAOT-LLVM

view this post on Zulip Peter Huene (Aug 05 2021 at 23:12):

ah, nice

view this post on Zulip Scott Waye (Aug 05 2021 at 23:13):

Its an experimental backend to https://github.com/dotnet/runtimelab/tree/feature/NativeAOT

This repo is for experimentation and exploring new ideas that may or may not make it into the main dotnet/runtime repo. - GitHub - dotnet/runtimelab at feature/NativeAOT

view this post on Zulip Peter Huene (Aug 05 2021 at 23:15):

as a fan of C# myself, it's really exciting to see work in this space


Last updated: Dec 23 2024 at 12:05 UTC