Stream: wit-bindgen

Topic: question re. wit, wasi, --adapt


view this post on Zulip Hazel Rowell (Sep 06 2023 at 16:20):

Hello! I'm trying to better understand what --adapt is doing in wasm-tools component.

My current, self assembled, potentially completely incorrect understanding is..


When compiling the guest program to the wasm32-wasi target; a bunch of wasi specific imports are included

$ wasm-tools print function.wasm | grep '(import'

  (import "wasi_snapshot_preview1" "fd_write" (func $_ZN4wasi13lib_generated22wasi_snapshot_preview18fd_write17h03249cfaa96a4902E (;0;) (type 5)))
  (import "wasi_snapshot_preview1" "environ_get" (func $__imported_wasi_snapshot_preview1_environ_get (;1;) (type 2)))
  (import "wasi_snapshot_preview1" "environ_sizes_get" (func $__imported_wasi_snapshot_preview1_environ_sizes_get (;2;) (type 2)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (;3;) (type 0)))

Running the adapt command injects a bunch of i32 refs in the guest program table which the host provides at those addresses.

gist of longer output - https://gist.github.com/SuddenlyHazel/bf0ce95f5753c70fd72cc0937066e569

(import "" "1" (func (;1;) (type 1)))
  (import (interface "wasi:io/streams") (instance (;1;) (type 1)))

Thank you!

GitHub Gist: instantly share code, notes, and snippets.

view this post on Zulip Alex Crichton (Sep 06 2023 at 16:39):

Sort of, but also sort of not. The premise of --adapt is that there's difficult-to-update code, such as a language standard library or a preexisting framework or preexisting binary, which is using preview1 functions. These preview1 functions do not map to the component model, but at the same time the preview2 definitions are able to be used to implement the preview1 definitions. This is where the adapter comes in.

The adapter is an implementation of preview1 in terms of preview2 within wasm itself. The imports of preview1 functions are then hooked up to the exports of the adapter and then the adapter only imports preview2 things. In the end only component-model things are imported and a component is created.

There's a whole bunch of details about how exactly the adapter works with indirect calls, instantiation ordering, how memory works, etc. That's not super relevant for the high-level picture though which is how this is basically all about implementing preview1 in terms of preview2 in-content

view this post on Zulip Hazel Rowell (Sep 06 2023 at 17:04):

Thank you! I think I have a better understanding now..

So, at the highest level, the adaptor is being used to provide interoperability?

I ended up going down this rabbit-hole when I received an

import  wasi:io/streams  has the wrong type

error. I believe it's because I downloaded the dev version of the adaptor and not the v12.0.1 release; the version my wasmtime and wasmtime-wasi deps use. just trying to figure out "why" its not working

view this post on Zulip Alex Crichton (Sep 06 2023 at 17:08):

Correct yeah, it's interop between preview1 and preview2. What you'll need to do though in your situation is ensure the version of the adapter matches the version of wasmtime to get the types to line up

view this post on Zulip Hazel Rowell (Sep 06 2023 at 17:21):

Yup! It took me awhile but I eventually figured out the mismatch there. I've been compiling these little "gotchas" together in a document. Might be useful to share once I have a better understanding of the holistic picture. I'm going to dive into resources and handles next :partying_face: , before discovering wit I had reached a similar stage with my own weird custom integration pattern..

Thank you for your time, I expect to be back here again soon :sweat_smile: ..

view this post on Zulip Alex Crichton (Sep 06 2023 at 17:25):

Sounds good, and collecting gotchas is much appreciated! We definitely like to keep a list of those around to know what to burn down.

view this post on Zulip Alex Crichton (Sep 06 2023 at 17:26):

One thing I'll say on resources is that they're still relatively new in terms of an implementation so there may be some rough corners, but all the basics and ideas should be there

view this post on Zulip Hazel Rowell (Sep 06 2023 at 22:42):

got a working example of resources
https://github.com/MyceliaNetwork/mycelia/pull/2

The above is using a static representation id.

Haven't dug in more just yet.. But, I'm guessing if I wanted something a bit better I want to at-least check the table for conflicts?

Building up some examples context for us..

Last updated: Nov 22 2024 at 16:03 UTC