@@ -1320,7 +1320,7 @@ impl<'a> EncodingState<'a> {
13201320 . collect :: < Vec < _ > > ( ) ,
13211321 )
13221322 } ;
1323- let type_index = self . payload_type_index ( info. ty ) ?;
1323+ let type_index = self . payload_type_index ( info. ty , info . imported ) ?;
13241324
13251325 match kind {
13261326 PayloadFuncKind :: FutureWrite => {
@@ -1422,11 +1422,27 @@ impl<'a> EncodingState<'a> {
14221422 Ok ( ( ) )
14231423 }
14241424
1425- fn payload_type_index ( & mut self , ty : TypeId ) -> Result < u32 > {
1425+ fn payload_type_index ( & mut self , ty : TypeId , imported : bool ) -> Result < u32 > {
1426+ fn owner ( resolve : & Resolve , ty : TypeId ) -> Option < InterfaceId > {
1427+ let def = & resolve. types [ ty] ;
1428+ match & def. kind {
1429+ TypeDefKind :: Future ( Some ( Type :: Id ( ty) ) ) => owner ( resolve, * ty) ,
1430+ TypeDefKind :: Stream ( Type :: Id ( ty) ) => owner ( resolve, * ty) ,
1431+ _ => match & def. owner {
1432+ TypeOwner :: World ( _) | TypeOwner :: None => None ,
1433+ TypeOwner :: Interface ( id) => Some ( * id) ,
1434+ } ,
1435+ }
1436+ }
1437+
14261438 let resolve = & self . info . encoder . metadata . resolve ;
1427- let ComponentValType :: Type ( type_index) = self
1428- . root_import_type_encoder ( None )
1429- . encode_valtype ( resolve, & Type :: Id ( ty) ) ?
1439+ let ComponentValType :: Type ( type_index) = if imported {
1440+ self . root_import_type_encoder ( None )
1441+ } else {
1442+ let owner = owner ( resolve, ty) ;
1443+ self . root_export_type_encoder ( owner)
1444+ }
1445+ . encode_valtype ( resolve, & Type :: Id ( ty) ) ?
14301446 else {
14311447 unreachable ! ( )
14321448 } ;
@@ -1733,7 +1749,7 @@ impl<'a> EncodingState<'a> {
17331749 return Ok ( ( ExportKind :: Func , index) ) ;
17341750 }
17351751 Import :: StreamNew ( info) => {
1736- let ty = self . payload_type_index ( info. ty ) ?;
1752+ let ty = self . payload_type_index ( info. ty , info . imported ) ?;
17371753 let index = self . component . stream_new ( ty) ;
17381754 return Ok ( ( ExportKind :: Func , index) ) ;
17391755 }
@@ -1749,28 +1765,36 @@ impl<'a> EncodingState<'a> {
17491765 payload_indirect ( self , * async_, info, PayloadFuncKind :: StreamWrite ) ,
17501766 ) ) ;
17511767 }
1752- Import :: StreamCancelRead { ty, async_ } => {
1753- let ty = self . payload_type_index ( * ty) ?;
1768+ Import :: StreamCancelRead {
1769+ ty,
1770+ imported,
1771+ async_,
1772+ } => {
1773+ let ty = self . payload_type_index ( * ty, * imported) ?;
17541774 let index = self . component . stream_cancel_read ( ty, * async_) ;
17551775 return Ok ( ( ExportKind :: Func , index) ) ;
17561776 }
1757- Import :: StreamCancelWrite { ty, async_ } => {
1758- let ty = self . payload_type_index ( * ty) ?;
1777+ Import :: StreamCancelWrite {
1778+ ty,
1779+ imported,
1780+ async_,
1781+ } => {
1782+ let ty = self . payload_type_index ( * ty, * imported) ?;
17591783 let index = self . component . stream_cancel_write ( ty, * async_) ;
17601784 return Ok ( ( ExportKind :: Func , index) ) ;
17611785 }
1762- Import :: StreamCloseReadable ( ty ) => {
1763- let type_index = self . payload_type_index ( * ty) ?;
1786+ Import :: StreamCloseReadable { ty , imported } => {
1787+ let type_index = self . payload_type_index ( * ty, * imported ) ?;
17641788 let index = self . component . stream_close_readable ( type_index) ;
17651789 return Ok ( ( ExportKind :: Func , index) ) ;
17661790 }
1767- Import :: StreamCloseWritable ( ty ) => {
1768- let type_index = self . payload_type_index ( * ty) ?;
1791+ Import :: StreamCloseWritable { ty , imported } => {
1792+ let type_index = self . payload_type_index ( * ty, * imported ) ?;
17691793 let index = self . component . stream_close_writable ( type_index) ;
17701794 return Ok ( ( ExportKind :: Func , index) ) ;
17711795 }
17721796 Import :: FutureNew ( info) => {
1773- let ty = self . payload_type_index ( info. ty ) ?;
1797+ let ty = self . payload_type_index ( info. ty , info . imported ) ?;
17741798 let index = self . component . future_new ( ty) ;
17751799 return Ok ( ( ExportKind :: Func , index) ) ;
17761800 }
@@ -1786,23 +1810,31 @@ impl<'a> EncodingState<'a> {
17861810 payload_indirect ( self , * async_, info, PayloadFuncKind :: FutureWrite ) ,
17871811 ) ) ;
17881812 }
1789- Import :: FutureCancelRead { ty, async_ } => {
1790- let ty = self . payload_type_index ( * ty) ?;
1813+ Import :: FutureCancelRead {
1814+ ty,
1815+ imported,
1816+ async_,
1817+ } => {
1818+ let ty = self . payload_type_index ( * ty, * imported) ?;
17911819 let index = self . component . future_cancel_read ( ty, * async_) ;
17921820 return Ok ( ( ExportKind :: Func , index) ) ;
17931821 }
1794- Import :: FutureCancelWrite { ty, async_ } => {
1795- let ty = self . payload_type_index ( * ty) ?;
1822+ Import :: FutureCancelWrite {
1823+ ty,
1824+ imported,
1825+ async_,
1826+ } => {
1827+ let ty = self . payload_type_index ( * ty, * imported) ?;
17961828 let index = self . component . future_cancel_write ( ty, * async_) ;
17971829 return Ok ( ( ExportKind :: Func , index) ) ;
17981830 }
1799- Import :: FutureCloseReadable ( ty ) => {
1800- let type_index = self . payload_type_index ( * ty) ?;
1831+ Import :: FutureCloseReadable { ty , imported } => {
1832+ let type_index = self . payload_type_index ( * ty, * imported ) ?;
18011833 let index = self . component . future_close_readable ( type_index) ;
18021834 return Ok ( ( ExportKind :: Func , index) ) ;
18031835 }
1804- Import :: FutureCloseWritable ( ty ) => {
1805- let type_index = self . payload_type_index ( * ty) ?;
1836+ Import :: FutureCloseWritable { ty , imported } => {
1837+ let type_index = self . payload_type_index ( * ty, * imported ) ?;
18061838 let index = self . component . future_close_writable ( type_index) ;
18071839 return Ok ( ( ExportKind :: Func , index) ) ;
18081840 }
@@ -2160,12 +2192,12 @@ impl<'a> Shims<'a> {
21602192 | Import :: StreamNew ( ..)
21612193 | Import :: FutureCancelRead { .. }
21622194 | Import :: FutureCancelWrite { .. }
2163- | Import :: FutureCloseWritable ( .. )
2164- | Import :: FutureCloseReadable ( .. )
2195+ | Import :: FutureCloseWritable { .. }
2196+ | Import :: FutureCloseReadable { .. }
21652197 | Import :: StreamCancelRead { .. }
21662198 | Import :: StreamCancelWrite { .. }
2167- | Import :: StreamCloseWritable ( .. )
2168- | Import :: StreamCloseReadable ( .. ) => continue ,
2199+ | Import :: StreamCloseWritable { .. }
2200+ | Import :: StreamCloseReadable { .. } => continue ,
21692201
21702202 Import :: FutureWrite { async_, info } => {
21712203 payload_push (
0 commit comments