So I've been reading this blog post by @Dirk Bäumer and working through it. I am exploring a vscode extension and I wanted to define about 4 modules using WIT and then implement some of them in Rust and some in typescript. Unfortunately, I will need async functions, and I gather they are not supported by WIT yet.
I'm finding the relationship of wasm-bindgen, WIT, and wasm-pack fairly confusing. What would be a recommended setup for the above? Can I combine WIT + wasm-bindgen into one project?
@nikomatsakis Oh that blog post is fairly out of date. The recent series by Dirk (part 1, part 2) should be more up to date, and I would recommend reading instead.
nikomatsakis said:
I'm finding the relationship of wasm-bindgen, WIT, and wasm-pack fairly confusing. What would be a recommended setup for the above? Can I combine WIT + wasm-bindgen into one project?
wasm-bindgen
and wasm-pack
are tools which I believe even predate WASI 0.1. The equivalent of these for WASI 0.2 would be wit-bindgen
and cargo-component
. These are designed for WIT, Wasm Components, and WASI 0.2.
nikomatsakis said:
Unfortunately, I will need async functions, and I gather they are not supported by WIT yet.
The answer here is nuanced: async functions are supported by WIT. Just not yet as a first-class construct. Instead WASI APIs currently use an epoll-like system based on a Pollable
type which requires a little bit of effort to correctly design for.
However, that said - my understanding is that the VS Code extension API currently does not support async functionality.
@Yoshua Wuyts I think I gave the wrong link, I read the more recent ones too
My conclusion after doing a bit more digging was that I should just use sync functions in WIT
and setup "completion callbacks"
perhaps writing a future on the Rust side if I want to model it
I've read your blog post as well but I'll re-read
nikomatsakis said:
My conclusion after doing a bit more digging was that I should just use sync functions in WIT
basically it seemed like async in WIT was still a WIP but I wanted to be on the "new hotness" train, and my needs I think are relatively simple
@nikomatsakis using the technique outlined here you can do the following:
VS Code's component model implementation will take care of the necessary synchronization.
I don't follow that last part
What is "Async API provided by VS Code" and where can I learn more about it?
I think they are just referring to the host's call into wasm being async. I am not familiar with vscode's specific implementation but Wasmtime's async feature lets you call (async host) -> (sync guest) -> (async host) where the guest has "no idea" it is in an async stack.
...though it isn't clear to me that vscode has any way to inject your own host imports into the runtime, so I'm not sure how relevant that really is...
Anyway you are correct that you have to roll your own async/concurrency today.
VS Code's API in the extension host is usually async (see for example the FileSytem API). WASM imports can currently only be sync, however by running the WASM code in a worker and using SharedArrayBuffers and Atomics WASM imports can be implemented in an async fashion. We use this to implement the WASI preview1 for VS Code.
I tried to setup a callback in wasi but I did something wrong...
#general > how to manage callbacks
not sure what though :)
@Dirk Bäumer is the wasm-component-model-async the one that demonstrates how to use webworkers here?
or maybe I'm confused
I'm trying to remember what I was trying to do even :)
I don't know what you're trying to do either :), but I know that Jco's preview2-shim demonstrates how to call async JS functions via web workers. Other options are to use asyncify or wait for JSPI to be standardized.
I was hoping to build a VSCode extension that consists of a mix of typescript + rust -- but I'm more and more thinking i'll just stick to TypeScript.
I don't have a real need to use Rust, I just like it :)
The key thing is that I expect the Rust code will need to call into async typescript functions (and vice versa)
I was hoping I could use a webworker to hide that or something without too much pain
anyway, thanks! asyncify etc looks cool. Count me as an eager future user, I guess, of better async interop...
webworkers would definitely work, but no guarantees about the "without too much pain" part.
it seems like I'd have to write various low-level primtiives to do the blocking etc
that...doesn't seem worth it
Yeah, if you look at the preview2-shim
code, it's not simple
I guess vscode's worker support is meant to ease this
now that I'm re-reading that blog post, I see that the point is that they've done the hard work ...
...still. I think i'm going to stick with typescript for now.
Thanks again!
Last updated: Nov 22 2024 at 17:03 UTC