1 use crate::component::func::{LiftContext, LowerContext};
2 use crate::component::matching::InstanceType;
3 use crate::component::{ComponentType, Lift, Lower, Val};
4 use crate::runtime::vm::VMStore;
5 use anyhow::{Result, anyhow, bail};
6 use core::convert::Infallible;
7 use core::future::Future;
8 use core::mem::MaybeUninit;
9 use core::pin::pin;
10 use core::task::{Context, Poll, Waker};
11 use wasmtime_environ::component::{CanonicalAbiInfo, InterfaceType, RuntimeComponentInstanceIndex};
12 
13 #[derive(Default)]
14 pub struct ConcurrentState;
15 
16 fn should_have_failed_validation<T>(what: &str) -> Result<T> {
17     // This should be unreachable; if we trap here, it indicates a
18     // bug in Wasmtime rather than in the guest.
19     Err(anyhow!(
20         "{what} should have failed validation \
21          when `component-model-async` feature disabled"
22     ))
23 }
24 
25 pub(crate) fn check_blocking(_: &mut dyn VMStore) -> Result<()> {
26     Ok(())
27 }
28 
29 pub(crate) fn poll_and_block<R: Send + Sync + 'static>(
30     _store: &mut dyn VMStore,
31     future: impl Future<Output = Result<R>> + Send + 'static,
32     _caller_instance: RuntimeComponentInstanceIndex,
33 ) -> Result<R> {
34     match pin!(future).poll(&mut Context::from_waker(Waker::noop())) {
35         Poll::Ready(result) => result,
36         Poll::Pending => should_have_failed_validation("async lowered import"),
37     }
38 }
39 
40 pub(crate) fn lower_error_context_to_index<U>(
41     _rep: u32,
42     _cx: &mut LowerContext<'_, U>,
43     _ty: InterfaceType,
44 ) -> Result<u32> {
45     should_have_failed_validation("use of `error-context`")
46 }
47 
48 pub struct ErrorContext(Infallible);
49 
50 impl ErrorContext {
51     pub(crate) fn into_val(self) -> Val {
52         match self.0 {}
53     }
54 
55     pub(crate) fn linear_lift_from_flat(
56         _cx: &mut LiftContext<'_>,
57         _ty: InterfaceType,
58         _src: &<u32 as ComponentType>::Lower,
59     ) -> Result<Self> {
60         should_have_failed_validation("use of `error-context`")
61     }
62 
63     pub(crate) fn linear_lift_from_memory(
64         _cx: &mut LiftContext<'_>,
65         _ty: InterfaceType,
66         _bytes: &[u8],
67     ) -> Result<Self> {
68         should_have_failed_validation("use of `error-context`")
69     }
70 }
71 
72 #[derive(PartialEq, Clone, Debug)]
73 pub struct FutureAny(Infallible);
74 
75 unsafe impl ComponentType for FutureAny {
76     type Lower = <u32 as ComponentType>::Lower;
77     const ABI: CanonicalAbiInfo = CanonicalAbiInfo::SCALAR4;
78 
79     fn typecheck(_ty: &InterfaceType, _types: &InstanceType<'_>) -> Result<()> {
80         bail!("support for component-model-async disabled at compile time")
81     }
82 }
83 
84 unsafe impl Lift for FutureAny {
85     fn linear_lift_from_flat(
86         _cx: &mut LiftContext<'_>,
87         _ty: InterfaceType,
88         _src: &Self::Lower,
89     ) -> Result<Self> {
90         bail!("support for component-model-async disabled at compile time")
91     }
92 
93     fn linear_lift_from_memory(
94         _cx: &mut LiftContext<'_>,
95         _ty: InterfaceType,
96         _bytes: &[u8],
97     ) -> Result<Self> {
98         bail!("support for component-model-async disabled at compile time")
99     }
100 }
101 
102 unsafe impl Lower for FutureAny {
103     fn linear_lower_to_flat<T>(
104         &self,
105         _cx: &mut LowerContext<'_, T>,
106         _ty: InterfaceType,
107         _dst: &mut MaybeUninit<Self::Lower>,
108     ) -> Result<()> {
109         match self.0 {}
110     }
111 
112     fn linear_lower_to_memory<T>(
113         &self,
114         _cx: &mut LowerContext<'_, T>,
115         _ty: InterfaceType,
116         _offset: usize,
117     ) -> Result<()> {
118         match self.0 {}
119     }
120 }
121 
122 #[derive(PartialEq, Clone, Debug)]
123 pub struct StreamAny(Infallible);
124 
125 unsafe impl ComponentType for StreamAny {
126     type Lower = <u32 as ComponentType>::Lower;
127     const ABI: CanonicalAbiInfo = CanonicalAbiInfo::SCALAR4;
128 
129     fn typecheck(_ty: &InterfaceType, _types: &InstanceType<'_>) -> Result<()> {
130         bail!("support for component-model-async disabled at compile time")
131     }
132 }
133 
134 unsafe impl Lift for StreamAny {
135     fn linear_lift_from_flat(
136         _cx: &mut LiftContext<'_>,
137         _ty: InterfaceType,
138         _src: &Self::Lower,
139     ) -> Result<Self> {
140         bail!("support for component-model-async disabled at compile time")
141     }
142 
143     fn linear_lift_from_memory(
144         _cx: &mut LiftContext<'_>,
145         _ty: InterfaceType,
146         _bytes: &[u8],
147     ) -> Result<Self> {
148         bail!("support for component-model-async disabled at compile time")
149     }
150 }
151 
152 unsafe impl Lower for StreamAny {
153     fn linear_lower_to_flat<T>(
154         &self,
155         _cx: &mut LowerContext<'_, T>,
156         _ty: InterfaceType,
157         _dst: &mut MaybeUninit<Self::Lower>,
158     ) -> Result<()> {
159         match self.0 {}
160     }
161 
162     fn linear_lower_to_memory<T>(
163         &self,
164         _cx: &mut LowerContext<'_, T>,
165         _ty: InterfaceType,
166         _offset: usize,
167     ) -> Result<()> {
168         match self.0 {}
169     }
170 }
171