1use {
2 crate::{
3 AsContextMut, ValRaw,
4 store::StoreInner,
5 vm::{VMFuncRef, VMMemoryDefinition, component::ComponentInstance},
6 },
7 anyhow::Result,
8 futures::{FutureExt, stream::FuturesUnordered},
9 std::{boxed::Box, future::Future, mem::MaybeUninit, pin::Pin},
10 wasmtime_environ::component::{
11 RuntimeComponentInstanceIndex, TypeComponentLocalErrorContextTableIndex,
12 TypeFutureTableIndex, TypeStreamTableIndex, TypeTupleIndex,
13 },
14};
15
16pub(crate) use futures_and_streams::ResourcePair;
17pub use futures_and_streams::{ErrorContext, FutureReader, StreamReader};
18
19mod futures_and_streams;
20
21pub struct Promise<T>(Pin<Box<dyn Future<Output = T> + Send + Sync + 'static>>);
28
29impl<T: 'static> Promise<T> {
30 pub fn map<U>(self, fun: impl FnOnce(T) -> U + Send + Sync + 'static) -> Promise<U> {
32 Promise(Box::pin(self.0.map(fun)))
33 }
34
35 pub async fn get<U: Send>(self, store: impl AsContextMut<Data = U>) -> Result<T> {
42 _ = store;
43 todo!()
44 }
45
46 pub fn into_future(self) -> Pin<Box<dyn Future<Output = T> + Send + Sync + 'static>> {
55 self.0
56 }
57}
58
59pub struct PromisesUnordered<T>(
64 FuturesUnordered<Pin<Box<dyn Future<Output = T> + Send + Sync + 'static>>>,
65);
66
67impl<T: 'static> PromisesUnordered<T> {
68 pub fn new() -> Self {
70 Self(FuturesUnordered::new())
71 }
72
73 pub fn push(&mut self, promise: Promise<T>) {
75 self.0.push(promise.0)
76 }
77
78 pub async fn next<U: Send>(&mut self, store: impl AsContextMut<Data = U>) -> Result<Option<T>> {
80 _ = store;
81 todo!()
82 }
83}
84
85pub unsafe trait VMComponentAsyncStore {
88 fn backpressure_set(
90 &mut self,
91 caller_instance: RuntimeComponentInstanceIndex,
92 enabled: u32,
93 ) -> Result<()>;
94
95 fn task_return(
97 &mut self,
98 instance: &mut ComponentInstance,
99 ty: TypeTupleIndex,
100 storage: *mut ValRaw,
101 storage_len: usize,
102 ) -> Result<()>;
103
104 fn waitable_set_new(
106 &mut self,
107 instance: &mut ComponentInstance,
108 caller_instance: RuntimeComponentInstanceIndex,
109 ) -> Result<u32>;
110
111 fn waitable_set_wait(
113 &mut self,
114 instance: &mut ComponentInstance,
115 caller_instance: RuntimeComponentInstanceIndex,
116 set: u32,
117 async_: bool,
118 memory: *mut VMMemoryDefinition,
119 payload: u32,
120 ) -> Result<u32>;
121
122 fn waitable_set_poll(
124 &mut self,
125 instance: &mut ComponentInstance,
126 caller_instance: RuntimeComponentInstanceIndex,
127 set: u32,
128 async_: bool,
129 memory: *mut VMMemoryDefinition,
130 payload: u32,
131 ) -> Result<u32>;
132
133 fn waitable_set_drop(
135 &mut self,
136 instance: &mut ComponentInstance,
137 caller_instance: RuntimeComponentInstanceIndex,
138 set: u32,
139 ) -> Result<()>;
140
141 fn waitable_join(
143 &mut self,
144 instance: &mut ComponentInstance,
145 caller_instance: RuntimeComponentInstanceIndex,
146 set: u32,
147 waitable: u32,
148 ) -> Result<()>;
149
150 fn yield_(&mut self, instance: &mut ComponentInstance, async_: bool) -> Result<()>;
152
153 fn subtask_drop(
155 &mut self,
156 instance: &mut ComponentInstance,
157 caller_instance: RuntimeComponentInstanceIndex,
158 task_id: u32,
159 ) -> Result<()>;
160
161 fn sync_enter(
164 &mut self,
165 start: *mut VMFuncRef,
166 return_: *mut VMFuncRef,
167 caller_instance: RuntimeComponentInstanceIndex,
168 task_return_type: TypeTupleIndex,
169 result_count: u32,
170 storage: *mut ValRaw,
171 storage_len: usize,
172 ) -> Result<()>;
173
174 fn sync_exit(
177 &mut self,
178 instance: &mut ComponentInstance,
179 callback: *mut VMFuncRef,
180 caller_instance: RuntimeComponentInstanceIndex,
181 callee: *mut VMFuncRef,
182 callee_instance: RuntimeComponentInstanceIndex,
183 param_count: u32,
184 storage: *mut MaybeUninit<ValRaw>,
185 storage_len: usize,
186 ) -> Result<()>;
187
188 fn async_enter(
191 &mut self,
192 start: *mut VMFuncRef,
193 return_: *mut VMFuncRef,
194 caller_instance: RuntimeComponentInstanceIndex,
195 task_return_type: TypeTupleIndex,
196 params: u32,
197 results: u32,
198 ) -> Result<()>;
199
200 fn async_exit(
203 &mut self,
204 instance: &mut ComponentInstance,
205 callback: *mut VMFuncRef,
206 post_return: *mut VMFuncRef,
207 caller_instance: RuntimeComponentInstanceIndex,
208 callee: *mut VMFuncRef,
209 callee_instance: RuntimeComponentInstanceIndex,
210 param_count: u32,
211 result_count: u32,
212 flags: u32,
213 ) -> Result<u32>;
214
215 fn future_new(
217 &mut self,
218 instance: &mut ComponentInstance,
219 ty: TypeFutureTableIndex,
220 ) -> Result<u32>;
221
222 fn future_write(
224 &mut self,
225 instance: &mut ComponentInstance,
226 memory: *mut VMMemoryDefinition,
227 realloc: *mut VMFuncRef,
228 string_encoding: u8,
229 ty: TypeFutureTableIndex,
230 future: u32,
231 address: u32,
232 ) -> Result<u32>;
233
234 fn future_read(
236 &mut self,
237 instance: &mut ComponentInstance,
238 memory: *mut VMMemoryDefinition,
239 realloc: *mut VMFuncRef,
240 string_encoding: u8,
241 ty: TypeFutureTableIndex,
242 future: u32,
243 address: u32,
244 ) -> Result<u32>;
245
246 fn future_cancel_write(
248 &mut self,
249 instance: &mut ComponentInstance,
250 ty: TypeFutureTableIndex,
251 async_: bool,
252 writer: u32,
253 ) -> Result<u32>;
254
255 fn future_cancel_read(
257 &mut self,
258 instance: &mut ComponentInstance,
259 ty: TypeFutureTableIndex,
260 async_: bool,
261 reader: u32,
262 ) -> Result<u32>;
263
264 fn future_close_writable(
266 &mut self,
267 instance: &mut ComponentInstance,
268 ty: TypeFutureTableIndex,
269 writer: u32,
270 ) -> Result<()>;
271
272 fn future_close_readable(
274 &mut self,
275 instance: &mut ComponentInstance,
276 ty: TypeFutureTableIndex,
277 reader: u32,
278 ) -> Result<()>;
279
280 fn stream_new(
282 &mut self,
283 instance: &mut ComponentInstance,
284 ty: TypeStreamTableIndex,
285 ) -> Result<u32>;
286
287 fn stream_write(
289 &mut self,
290 instance: &mut ComponentInstance,
291 memory: *mut VMMemoryDefinition,
292 realloc: *mut VMFuncRef,
293 string_encoding: u8,
294 ty: TypeStreamTableIndex,
295 stream: u32,
296 address: u32,
297 count: u32,
298 ) -> Result<u32>;
299
300 fn stream_read(
302 &mut self,
303 instance: &mut ComponentInstance,
304 memory: *mut VMMemoryDefinition,
305 realloc: *mut VMFuncRef,
306 string_encoding: u8,
307 ty: TypeStreamTableIndex,
308 stream: u32,
309 address: u32,
310 count: u32,
311 ) -> Result<u32>;
312
313 fn stream_cancel_write(
315 &mut self,
316 instance: &mut ComponentInstance,
317 ty: TypeStreamTableIndex,
318 async_: bool,
319 writer: u32,
320 ) -> Result<u32>;
321
322 fn stream_cancel_read(
324 &mut self,
325 instance: &mut ComponentInstance,
326 ty: TypeStreamTableIndex,
327 async_: bool,
328 reader: u32,
329 ) -> Result<u32>;
330
331 fn stream_close_writable(
333 &mut self,
334 instance: &mut ComponentInstance,
335 ty: TypeStreamTableIndex,
336 writer: u32,
337 ) -> Result<()>;
338
339 fn stream_close_readable(
341 &mut self,
342 instance: &mut ComponentInstance,
343 ty: TypeStreamTableIndex,
344 reader: u32,
345 ) -> Result<()>;
346
347 fn flat_stream_write(
350 &mut self,
351 instance: &mut ComponentInstance,
352 memory: *mut VMMemoryDefinition,
353 realloc: *mut VMFuncRef,
354 ty: TypeStreamTableIndex,
355 payload_size: u32,
356 payload_align: u32,
357 stream: u32,
358 address: u32,
359 count: u32,
360 ) -> Result<u32>;
361
362 fn flat_stream_read(
365 &mut self,
366 instance: &mut ComponentInstance,
367 memory: *mut VMMemoryDefinition,
368 realloc: *mut VMFuncRef,
369 ty: TypeStreamTableIndex,
370 payload_size: u32,
371 payload_align: u32,
372 stream: u32,
373 address: u32,
374 count: u32,
375 ) -> Result<u32>;
376
377 fn error_context_new(
379 &mut self,
380 instance: &mut ComponentInstance,
381 memory: *mut VMMemoryDefinition,
382 realloc: *mut VMFuncRef,
383 string_encoding: u8,
384 ty: TypeComponentLocalErrorContextTableIndex,
385 debug_msg_address: u32,
386 debug_msg_len: u32,
387 ) -> Result<u32>;
388
389 fn error_context_debug_message(
391 &mut self,
392 instance: &mut ComponentInstance,
393 memory: *mut VMMemoryDefinition,
394 realloc: *mut VMFuncRef,
395 string_encoding: u8,
396 ty: TypeComponentLocalErrorContextTableIndex,
397 err_ctx_handle: u32,
398 debug_msg_address: u32,
399 ) -> Result<()>;
400
401 fn error_context_drop(
403 &mut self,
404 instance: &mut ComponentInstance,
405 ty: TypeComponentLocalErrorContextTableIndex,
406 err_ctx_handle: u32,
407 ) -> Result<()>;
408}
409
410unsafe impl<T> VMComponentAsyncStore for StoreInner<T> {
411 fn backpressure_set(
412 &mut self,
413 caller_instance: RuntimeComponentInstanceIndex,
414 enabled: u32,
415 ) -> Result<()> {
416 _ = (caller_instance, enabled);
417 todo!()
418 }
419
420 fn task_return(
421 &mut self,
422 instance: &mut ComponentInstance,
423 ty: TypeTupleIndex,
424 storage: *mut ValRaw,
425 storage_len: usize,
426 ) -> Result<()> {
427 _ = (instance, ty, storage, storage_len);
428 todo!()
429 }
430
431 fn waitable_set_new(
432 &mut self,
433 instance: &mut ComponentInstance,
434 caller_instance: RuntimeComponentInstanceIndex,
435 ) -> Result<u32> {
436 _ = (instance, caller_instance);
437 todo!();
438 }
439
440 fn waitable_set_wait(
441 &mut self,
442 instance: &mut ComponentInstance,
443 caller_instance: RuntimeComponentInstanceIndex,
444 set: u32,
445 async_: bool,
446 memory: *mut VMMemoryDefinition,
447 payload: u32,
448 ) -> Result<u32> {
449 _ = (instance, caller_instance, set, async_, memory, payload);
450 todo!();
451 }
452
453 fn waitable_set_poll(
454 &mut self,
455 instance: &mut ComponentInstance,
456 caller_instance: RuntimeComponentInstanceIndex,
457 set: u32,
458 async_: bool,
459 memory: *mut VMMemoryDefinition,
460 payload: u32,
461 ) -> Result<u32> {
462 _ = (instance, caller_instance, set, async_, memory, payload);
463 todo!();
464 }
465
466 fn waitable_set_drop(
467 &mut self,
468 instance: &mut ComponentInstance,
469 caller_instance: RuntimeComponentInstanceIndex,
470 set: u32,
471 ) -> Result<()> {
472 _ = (instance, caller_instance, set);
473 todo!();
474 }
475
476 fn waitable_join(
477 &mut self,
478 instance: &mut ComponentInstance,
479 caller_instance: RuntimeComponentInstanceIndex,
480 set: u32,
481 waitable: u32,
482 ) -> Result<()> {
483 _ = (instance, caller_instance, set, waitable);
484 todo!();
485 }
486
487 fn yield_(&mut self, instance: &mut ComponentInstance, async_: bool) -> Result<()> {
488 _ = (instance, async_);
489 todo!()
490 }
491
492 fn subtask_drop(
493 &mut self,
494 instance: &mut ComponentInstance,
495 caller_instance: RuntimeComponentInstanceIndex,
496 task_id: u32,
497 ) -> Result<()> {
498 _ = (instance, caller_instance, task_id);
499 todo!()
500 }
501
502 fn sync_enter(
503 &mut self,
504 start: *mut VMFuncRef,
505 return_: *mut VMFuncRef,
506 caller_instance: RuntimeComponentInstanceIndex,
507 task_return_type: TypeTupleIndex,
508 result_count: u32,
509 storage: *mut ValRaw,
510 storage_len: usize,
511 ) -> Result<()> {
512 _ = (
513 start,
514 return_,
515 caller_instance,
516 task_return_type,
517 result_count,
518 storage,
519 storage_len,
520 );
521 todo!()
522 }
523
524 fn sync_exit(
525 &mut self,
526 instance: &mut ComponentInstance,
527 callback: *mut VMFuncRef,
528 caller_instance: RuntimeComponentInstanceIndex,
529 callee: *mut VMFuncRef,
530 callee_instance: RuntimeComponentInstanceIndex,
531 param_count: u32,
532 storage: *mut MaybeUninit<ValRaw>,
533 storage_len: usize,
534 ) -> Result<()> {
535 _ = (
536 instance,
537 callback,
538 caller_instance,
539 callee,
540 callee_instance,
541 param_count,
542 storage,
543 storage_len,
544 );
545 todo!()
546 }
547
548 fn async_enter(
549 &mut self,
550 start: *mut VMFuncRef,
551 return_: *mut VMFuncRef,
552 caller_instance: RuntimeComponentInstanceIndex,
553 task_return_type: TypeTupleIndex,
554 params: u32,
555 results: u32,
556 ) -> Result<()> {
557 _ = (
558 start,
559 return_,
560 caller_instance,
561 task_return_type,
562 params,
563 results,
564 );
565 todo!()
566 }
567
568 fn async_exit(
569 &mut self,
570 instance: &mut ComponentInstance,
571 callback: *mut VMFuncRef,
572 post_return: *mut VMFuncRef,
573 caller_instance: RuntimeComponentInstanceIndex,
574 callee: *mut VMFuncRef,
575 callee_instance: RuntimeComponentInstanceIndex,
576 param_count: u32,
577 result_count: u32,
578 flags: u32,
579 ) -> Result<u32> {
580 _ = (
581 instance,
582 callback,
583 post_return,
584 caller_instance,
585 callee,
586 callee_instance,
587 param_count,
588 result_count,
589 flags,
590 );
591 todo!()
592 }
593
594 fn future_new(
595 &mut self,
596 instance: &mut ComponentInstance,
597 ty: TypeFutureTableIndex,
598 ) -> Result<u32> {
599 _ = (instance, ty);
600 todo!()
601 }
602
603 fn future_write(
604 &mut self,
605 instance: &mut ComponentInstance,
606 memory: *mut VMMemoryDefinition,
607 realloc: *mut VMFuncRef,
608 string_encoding: u8,
609 ty: TypeFutureTableIndex,
610 future: u32,
611 address: u32,
612 ) -> Result<u32> {
613 _ = (
614 instance,
615 memory,
616 realloc,
617 string_encoding,
618 ty,
619 future,
620 address,
621 );
622 todo!()
623 }
624
625 fn future_read(
626 &mut self,
627 instance: &mut ComponentInstance,
628 memory: *mut VMMemoryDefinition,
629 realloc: *mut VMFuncRef,
630 string_encoding: u8,
631 ty: TypeFutureTableIndex,
632 future: u32,
633 address: u32,
634 ) -> Result<u32> {
635 _ = (
636 instance,
637 memory,
638 realloc,
639 string_encoding,
640 ty,
641 future,
642 address,
643 );
644 todo!()
645 }
646
647 fn future_cancel_write(
648 &mut self,
649 instance: &mut ComponentInstance,
650 ty: TypeFutureTableIndex,
651 async_: bool,
652 writer: u32,
653 ) -> Result<u32> {
654 _ = (instance, ty, async_, writer);
655 todo!()
656 }
657
658 fn future_cancel_read(
659 &mut self,
660 instance: &mut ComponentInstance,
661 ty: TypeFutureTableIndex,
662 async_: bool,
663 reader: u32,
664 ) -> Result<u32> {
665 _ = (instance, ty, async_, reader);
666 todo!()
667 }
668
669 fn future_close_writable(
670 &mut self,
671 instance: &mut ComponentInstance,
672 ty: TypeFutureTableIndex,
673 writer: u32,
674 ) -> Result<()> {
675 _ = (instance, ty, writer);
676 todo!()
677 }
678
679 fn future_close_readable(
680 &mut self,
681 instance: &mut ComponentInstance,
682 ty: TypeFutureTableIndex,
683 reader: u32,
684 ) -> Result<()> {
685 _ = (instance, ty, reader);
686 todo!()
687 }
688
689 fn stream_new(
690 &mut self,
691 instance: &mut ComponentInstance,
692 ty: TypeStreamTableIndex,
693 ) -> Result<u32> {
694 _ = (instance, ty);
695 todo!()
696 }
697
698 fn stream_write(
699 &mut self,
700 instance: &mut ComponentInstance,
701 memory: *mut VMMemoryDefinition,
702 realloc: *mut VMFuncRef,
703 string_encoding: u8,
704 ty: TypeStreamTableIndex,
705 stream: u32,
706 address: u32,
707 count: u32,
708 ) -> Result<u32> {
709 _ = (
710 instance,
711 memory,
712 realloc,
713 string_encoding,
714 ty,
715 stream,
716 address,
717 count,
718 );
719 todo!()
720 }
721
722 fn stream_read(
723 &mut self,
724 instance: &mut ComponentInstance,
725 memory: *mut VMMemoryDefinition,
726 realloc: *mut VMFuncRef,
727 string_encoding: u8,
728 ty: TypeStreamTableIndex,
729 stream: u32,
730 address: u32,
731 count: u32,
732 ) -> Result<u32> {
733 _ = (
734 instance,
735 memory,
736 realloc,
737 string_encoding,
738 ty,
739 stream,
740 address,
741 count,
742 );
743 todo!()
744 }
745
746 fn stream_cancel_write(
747 &mut self,
748 instance: &mut ComponentInstance,
749 ty: TypeStreamTableIndex,
750 async_: bool,
751 writer: u32,
752 ) -> Result<u32> {
753 _ = (instance, ty, async_, writer);
754 todo!()
755 }
756
757 fn stream_cancel_read(
758 &mut self,
759 instance: &mut ComponentInstance,
760 ty: TypeStreamTableIndex,
761 async_: bool,
762 reader: u32,
763 ) -> Result<u32> {
764 _ = (instance, ty, async_, reader);
765 todo!()
766 }
767
768 fn stream_close_writable(
769 &mut self,
770 instance: &mut ComponentInstance,
771 ty: TypeStreamTableIndex,
772 writer: u32,
773 ) -> Result<()> {
774 _ = (instance, ty, writer);
775 todo!()
776 }
777
778 fn stream_close_readable(
779 &mut self,
780 instance: &mut ComponentInstance,
781 ty: TypeStreamTableIndex,
782 reader: u32,
783 ) -> Result<()> {
784 _ = (instance, ty, reader);
785 todo!()
786 }
787
788 fn flat_stream_write(
789 &mut self,
790 instance: &mut ComponentInstance,
791 memory: *mut VMMemoryDefinition,
792 realloc: *mut VMFuncRef,
793 ty: TypeStreamTableIndex,
794 payload_size: u32,
795 payload_align: u32,
796 stream: u32,
797 address: u32,
798 count: u32,
799 ) -> Result<u32> {
800 _ = (
801 instance,
802 memory,
803 realloc,
804 ty,
805 payload_size,
806 payload_align,
807 stream,
808 address,
809 count,
810 );
811 todo!()
812 }
813
814 fn flat_stream_read(
815 &mut self,
816 instance: &mut ComponentInstance,
817 memory: *mut VMMemoryDefinition,
818 realloc: *mut VMFuncRef,
819 ty: TypeStreamTableIndex,
820 payload_size: u32,
821 payload_align: u32,
822 stream: u32,
823 address: u32,
824 count: u32,
825 ) -> Result<u32> {
826 _ = (
827 instance,
828 memory,
829 realloc,
830 ty,
831 payload_size,
832 payload_align,
833 stream,
834 address,
835 count,
836 );
837 todo!()
838 }
839
840 fn error_context_new(
841 &mut self,
842 instance: &mut ComponentInstance,
843 memory: *mut VMMemoryDefinition,
844 realloc: *mut VMFuncRef,
845 string_encoding: u8,
846 ty: TypeComponentLocalErrorContextTableIndex,
847 debug_msg_address: u32,
848 debug_msg_len: u32,
849 ) -> Result<u32> {
850 _ = (
851 instance,
852 memory,
853 realloc,
854 string_encoding,
855 ty,
856 debug_msg_address,
857 debug_msg_len,
858 );
859 todo!()
860 }
861
862 fn error_context_debug_message(
863 &mut self,
864 instance: &mut ComponentInstance,
865 memory: *mut VMMemoryDefinition,
866 realloc: *mut VMFuncRef,
867 string_encoding: u8,
868 ty: TypeComponentLocalErrorContextTableIndex,
869 err_ctx_handle: u32,
870 debug_msg_address: u32,
871 ) -> Result<()> {
872 _ = (
873 instance,
874 memory,
875 realloc,
876 string_encoding,
877 ty,
878 err_ctx_handle,
879 debug_msg_address,
880 );
881 todo!()
882 }
883
884 fn error_context_drop(
885 &mut self,
886 instance: &mut ComponentInstance,
887 ty: TypeComponentLocalErrorContextTableIndex,
888 err_ctx_handle: u32,
889 ) -> Result<()> {
890 _ = (instance, ty, err_ctx_handle);
891 todo!()
892 }
893}