Stream: general

Topic: optional imports


view this post on Zulip ovf (Jun 27 2023 at 16:38):

hi, what's the current state of optional imports? i have a wasi preview1 application that i want to give an additional syscall or two in the browser, but still run the module in wasmtime etc.

view this post on Zulip Lann Martin (Jun 27 2023 at 17:03):

There is no first-class concept of optional imports yet. Wasmtime has options to stub out unknown imports to return "default values"; that's about as close as you can get today.

view this post on Zulip ovf (Jun 27 2023 at 17:05):

thanks! that's a bit unfortunate. do people generally not need this, or am i facing an x/y problem?

view this post on Zulip Lann Martin (Jun 27 2023 at 17:07):

Optional imports are definitely on the collective design radar but haven't taken priority yet. There are also more workarounds available if you are building your own wasmtime embedding rather than using wasmtime run.

view this post on Zulip ovf (Jun 27 2023 at 17:09):

thanks. my goal is to redistribute that module, so custom wasmtime embeddings won't help.

view this post on Zulip Salim Afiune Maya (Jul 07 2023 at 04:58):

@Lann Martin could you point me to that workaround you mentioned? Please

view this post on Zulip Lann Martin (Jul 07 2023 at 12:49):

The general strategy would be to iterate over a module's imports and - for any unknown imports that you want to stub out - define a stub implementation that either traps or returns some default value. If you want to do this unconditionally for all unknown imports there are two functions that implement the above approach, corresponding to wasmtime run --trap-unknown-imports and --default-values-unknown-imports

view this post on Zulip Lann Martin (Jul 07 2023 at 12:50):

The source of those two functions is also a great reference for how to do this yourself.

view this post on Zulip Karel Hrkal (kajacx) (Jul 07 2023 at 13:26):

With wit-bindgen, you could define the imported functions in a resource and then return an option<resource> from an imported function.

Not sure if that will help if yo don's want to / can't use wit bindgen.

view this post on Zulip Lann Martin (Jul 07 2023 at 13:34):

If the host (in this case) knows about the import ahead of time then you have lots of alternatives. I think one of the ideas with optional imports would be that only the importer needs to know about the optional import and can detect at runtime whether the import was actually provided.

view this post on Zulip Salim Afiune Maya (Jul 08 2023 at 10:32):

Nice! Thank you. I’m assuming this works transparently in the component model? 🤔

view this post on Zulip Salim Afiune Maya (Aug 13 2023 at 13:15):

I finally got time to come back to this topic. I am really interested about what you said @Karel Hrkal (kajacx) since my use case is very straightforward, it is essentially what @Lann Martin mentioned so I am trying to have a quick example but not only about optional imports, but also optional exports. I think that the later might not be supported just yet https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md#a-note-on-subtyping but I might be wrong.

What I want for the optional exports is to sort-of define a default behavior in the Host that, if a component is loaded and has the exported function, then it will call that function instead of the default.

I think this is really helpful since we could implement new functionality that, components might not really need to define which means that they can be loaded by the runtime without issues but, when consumers want to modify that default behavior, they can do it without breaking the “contract”. This essentially allows the system to add features without the components knowing.

Is there something I can read about optional exports? I really appreciate all of your time.

view this post on Zulip Karel Hrkal (kajacx) (Aug 13 2023 at 16:34):

In "normal" wasm files, all exports are optional by default. That's why instance.get_typed_funcreturns an result, because the function might not exists.

With wit bindgen / the component model, I'm not sure how well optional exports are supported. My approach would be to create a wrapper library around the "raw" wit "contract", which would be better to use anyway, and it gives you additional benefits such as handling default/optional exports.

For example, you have a look at this trait. It "wraps" the trait coming from the wit bindgen, but it's using custom types. This is so that I can define convenience methods on the types that will be used by the end user who will write the guest library, which is more convenient then requiring them to use the wit generated types directly.

Adding an optional exported function with default behaviour is super easy now: just add a method to the trait with a default implementation. It would not even break existing guest libraries, as long as then use the updated version of the "api" library.

view this post on Zulip Salim Afiune Maya (Aug 14 2023 at 12:58):

@Karel Hrkal (kajacx) this is exactly what I need. Thank you so much for sharing this with me.

view this post on Zulip Salim Afiune Maya (Aug 14 2023 at 13:00):

One more question, I noticed that your project is still using wit-bindgen = { version = "0.8.0" } - I am doing the same - Any reason not to switching to use wasmtime directly now that the component model is in there? (trying to look for any gotchas before trying to switch my project :wink:)

view this post on Zulip Karel Hrkal (kajacx) (Aug 14 2023 at 13:46):

AFAIK, the wasmtime crate is for the runtime host, this is for the guest. I guess I could update to use cargo-component-bindings, which is what cargo component will generate for you, but I haven't gotten to do the update yet.

A Cargo subcommand for creating WebAssembly components based on the component model proposal. - GitHub - bytecodealliance/cargo-component: A Cargo subcommand for creating WebAssembly components bas...

view this post on Zulip Mossaka (Joe) (Aug 14 2023 at 21:18):

@Luke Wagner might have some thoughts on optional imports/exports

view this post on Zulip Luke Wagner (Aug 14 2023 at 21:44):

optional imports/exports are still something I'd like to include in the C-M and Wit so that this optionality can be given first-class tooling and virtualization support and also enable certain kinds of deployment-time optimizations, but it's definitely post-Preview-2, so i'm glad to hear we have stopgap workarounds in the meantime.


Last updated: Oct 23 2024 at 20:03 UTC