1 use crate::Uninhabited; 2 use crate::component::func::{ComponentType, LiftContext, LowerContext}; 3 use crate::component::{Instance, Val}; 4 use crate::runtime::vm::VMStore; 5 use anyhow::{Result, anyhow}; 6 use core::future::Future; 7 use core::marker::PhantomData; 8 use core::pin::pin; 9 use core::task::{Context, Poll, Waker}; 10 use wasmtime_environ::component::{InterfaceType, RuntimeComponentInstanceIndex}; 11 12 fn should_have_failed_validation<T>(what: &str) -> Result<T> { 13 // This should be unreachable; if we trap here, it indicates a 14 // bug in Wasmtime rather than in the guest. 15 Err(anyhow!( 16 "{what} should have failed validation \ 17 when `component-model-async` feature disabled" 18 )) 19 } 20 21 impl Instance { 22 pub(crate) fn poll_and_block<R: Send + Sync + 'static>( 23 self, 24 _store: &mut dyn VMStore, 25 future: impl Future<Output = Result<R>> + Send + 'static, 26 _caller_instance: RuntimeComponentInstanceIndex, 27 ) -> Result<R> { 28 match pin!(future).poll(&mut Context::from_waker(Waker::noop())) { 29 Poll::Ready(result) => result, 30 Poll::Pending => should_have_failed_validation("async lowered import"), 31 } 32 } 33 } 34 35 pub(crate) fn lower_future_to_index<U>( 36 _rep: u32, 37 _cx: &mut LowerContext<'_, U>, 38 _ty: InterfaceType, 39 ) -> Result<u32> { 40 should_have_failed_validation("use of `future`") 41 } 42 43 pub(crate) fn lower_stream_to_index<U>( 44 _rep: u32, 45 _cx: &mut LowerContext<'_, U>, 46 _ty: InterfaceType, 47 ) -> Result<u32> { 48 should_have_failed_validation("use of `stream`") 49 } 50 51 pub(crate) fn lower_error_context_to_index<U>( 52 _rep: u32, 53 _cx: &mut LowerContext<'_, U>, 54 _ty: InterfaceType, 55 ) -> Result<u32> { 56 should_have_failed_validation("use of `error-context`") 57 } 58 59 pub struct ErrorContext(Uninhabited); 60 61 impl ErrorContext { 62 pub(crate) fn into_val(self) -> Val { 63 match self.0 {} 64 } 65 66 pub(crate) fn linear_lift_from_flat( 67 _cx: &mut LiftContext<'_>, 68 _ty: InterfaceType, 69 _src: &<u32 as ComponentType>::Lower, 70 ) -> Result<Self> { 71 should_have_failed_validation("use of `error-context`") 72 } 73 74 pub(crate) fn linear_lift_from_memory( 75 _cx: &mut LiftContext<'_>, 76 _ty: InterfaceType, 77 _bytes: &[u8], 78 ) -> Result<Self> { 79 should_have_failed_validation("use of `error-context`") 80 } 81 } 82 83 pub struct StreamReader<P> { 84 uninhabited: Uninhabited, 85 _phantom: PhantomData<P>, 86 } 87 88 impl<P> StreamReader<P> { 89 pub(crate) fn into_val(self) -> Val { 90 match self.uninhabited {} 91 } 92 93 pub(crate) fn linear_lift_from_flat( 94 _cx: &mut LiftContext<'_>, 95 _ty: InterfaceType, 96 _src: &<u32 as ComponentType>::Lower, 97 ) -> Result<Self> { 98 should_have_failed_validation("use of `stream`") 99 } 100 101 pub(crate) fn linear_lift_from_memory( 102 _cx: &mut LiftContext<'_>, 103 _ty: InterfaceType, 104 _bytes: &[u8], 105 ) -> Result<Self> { 106 should_have_failed_validation("use of `stream`") 107 } 108 } 109 110 pub struct FutureReader<P> { 111 uninhabited: Uninhabited, 112 _phantom: PhantomData<P>, 113 } 114 115 impl<P> FutureReader<P> { 116 pub(crate) fn into_val(self) -> Val { 117 match self.uninhabited {} 118 } 119 120 pub(crate) fn linear_lift_from_flat( 121 _cx: &mut LiftContext<'_>, 122 _ty: InterfaceType, 123 _src: &<u32 as ComponentType>::Lower, 124 ) -> Result<Self> { 125 should_have_failed_validation("use of `future`") 126 } 127 128 pub(crate) fn linear_lift_from_memory( 129 _cx: &mut LiftContext<'_>, 130 _ty: InterfaceType, 131 _bytes: &[u8], 132 ) -> Result<Self> { 133 should_have_failed_validation("use of `future`") 134 } 135 } 136