This repository was archived by the owner on Sep 8, 2025. It is now read-only.
Commit e4ffbf1
authored
refactor
I had a few goals with this PR:
1. Improve the ergonomics of concurrent import bindings by supporting
`async`/`await` sugar and allowing store access to be arbitrarily interspersed
between `await` points -- while still preventing references to the store
across `await` points.
2. Get rid of the `Data` associated types for `Host` traits and support
`add_to_linker_get_host` where the `Host` impl is not the same as the `T` in
`Store<T>`.
3. Allow creating, reading from, writing to, etc. streams and futures without
exposing `StoreContextMut` directly.
Unfortunately, after a day of intense type tetris I failed to achieve items 2 or
3, so this only covers item 1.
Regarding item 1: I've introduced a new `Accessor` type which wraps a `*mut dyn
VMStore` and provides access to it only via a `with` method that accepts a
synchronous closure which takes a `StoreContextMut<T>` parameter. The closure
can do what it likes and return an arbitrary value as long as that result has a
`'static` lifetime (i.e. does not borrow from the store). This ensures that the
host function is able to access the store only between `await`s and not across
them; we prohibit the latter because it would prevent other async-lowered
imports from running concurrently. Finally, since host function takes a `&mut
Accessor<T>`, it is not possible for the reference to outlive the future
returned by the host function, and since the `with` method takes `&mut self` it
cannot be used recursively.
Regarding items 2 and 3: In order to read from or write to streams/futures, we
need to be able to efficiently lift and lower their payload types, which
requires that both the payload type (of which there could be several for a given
world) and the `T` in `Store<T>` be in scope. I was unable to find a way to
thread those types through the various layers of closures, futures, and
generated code without adding unwanted `'static` bounds and/or breaking the
blanket `impl`s used for forwarding calls from `&mut X` to `X`. Also, the usual
tricks of using dyn objects or vtables could only be used ergonomically to erase
one of the two types but not both. I'd love to revisit this later with the help
of a Rust type wizard to see if there's a strategy I missed.
Signed-off-by: Joel Dice <joel.dice@fermyon.com>concurrent_imports support in wasmtime-wit-bindgen (#6)1 parent 469ef15 commit e4ffbf1
10 files changed
Lines changed: 298 additions & 430 deletions
File tree
- crates
- misc/component-async-tests
- http/src
- src
- wasmtime/src/runtime
- component
- func
- store
- wit-bindgen/src
- tests/all/component_model
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
33 | 33 | | |
34 | | - | |
| 34 | + | |
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
| 58 | + | |
59 | 59 | | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
| 60 | + | |
70 | 61 | | |
71 | 62 | | |
72 | 63 | | |
| |||
77 | 68 | | |
78 | 69 | | |
79 | 70 | | |
80 | | - | |
| 71 | + | |
81 | 72 | | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
93 | 76 | | |
94 | 77 | | |
95 | 78 | | |
| |||
103 | 86 | | |
104 | 87 | | |
105 | 88 | | |
106 | | - | |
| 89 | + | |
107 | 90 | | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
119 | 94 | | |
120 | 95 | | |
121 | 96 | | |
| |||
255 | 230 | | |
256 | 231 | | |
257 | 232 | | |
258 | | - | |
259 | | - | |
| 233 | + | |
| 234 | + | |
260 | 235 | | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
| 236 | + | |
| 237 | + | |
271 | 238 | | |
272 | 239 | | |
273 | 240 | | |
274 | 241 | | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
| 242 | + | |
282 | 243 | | |
283 | | - | |
284 | | - | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
285 | 249 | | |
286 | 250 | | |
287 | 251 | | |
| |||
530 | 494 | | |
531 | 495 | | |
532 | 496 | | |
533 | | - | |
534 | | - | |
| 497 | + | |
| 498 | + | |
535 | 499 | | |
536 | | - | |
537 | | - | |
538 | | - | |
539 | | - | |
540 | | - | |
541 | | - | |
542 | | - | |
543 | | - | |
544 | | - | |
545 | | - | |
546 | | - | |
| 500 | + | |
| 501 | + | |
547 | 502 | | |
548 | 503 | | |
549 | 504 | | |
| |||
0 commit comments