@@ -439,7 +439,7 @@ impl Func {
439439 MAX_FLAT_PARAMS ,
440440 false ,
441441 move |func, store, params_out| {
442- func. with_lower_context ( store, true , |cx, ty| {
442+ func. with_lower_context ( store, |cx, ty| {
443443 Self :: lower_args ( cx, & params, ty, params_out)
444444 } )
445445 } ,
@@ -490,7 +490,7 @@ impl Func {
490490 // safe in Rust, however, due to `ValRaw` being a `union`. The
491491 // contents should dynamically not be read due to the type of the
492492 // function used here matching the actual lift.
493- unsafe {
493+ let ( _ , post_return_arg ) = unsafe {
494494 self . call_raw (
495495 store. as_context_mut ( ) ,
496496 |cx, ty, dst : & mut MaybeUninit < [ MaybeUninit < ValRaw > ; MAX_FLAT_PARAMS ] > | {
@@ -510,9 +510,9 @@ impl Func {
510510 Ok ( ( ) )
511511 } ,
512512 ) ?
513- }
513+ } ;
514514
515- self . post_return_impl ( store)
515+ self . post_return_impl ( store, post_return_arg )
516516 }
517517
518518 pub ( crate ) fn lifted_core_func ( & self , store : & mut StoreOpaque ) -> NonNull < VMFuncRef > {
@@ -587,7 +587,7 @@ impl Func {
587587 & mut MaybeUninit < LowerParams > ,
588588 ) -> Result < ( ) > ,
589589 lift : impl FnOnce ( & mut LiftContext < ' _ > , InterfaceType , & LowerReturn ) -> Result < Return > ,
590- ) -> Result < Return >
590+ ) -> Result < ( Return , ValRaw ) >
591591 where
592592 LowerParams : Copy ,
593593 LowerReturn : Copy ,
@@ -631,7 +631,7 @@ impl Func {
631631 assert ! ( mem:: align_of_val( map_maybe_uninit!( space. params) ) == val_align) ;
632632 assert ! ( mem:: align_of_val( map_maybe_uninit!( space. ret) ) == val_align) ;
633633
634- self . with_lower_context ( store. as_context_mut ( ) , false , |cx, ty| {
634+ self . with_lower_context ( store. as_context_mut ( ) , |cx, ty| {
635635 cx. enter_call ( ) ;
636636 lower ( cx, ty, map_maybe_uninit ! ( space. params) )
637637 } ) ?;
@@ -671,23 +671,20 @@ impl Func {
671671 // is currently required to be 0 or 1 values according to the
672672 // canonical ABI, is saved within the `Store`'s `FuncData`. This'll
673673 // later get used in post-return.
674- // flags.set_needs_post_return(true);
675674 let val = self . with_lift_context ( store. 0 , |cx, ty| lift ( cx, ty, ret) ) ?;
676675
677676 // SAFETY: it's a contract of this function that `LowerReturn` is an
678677 // appropriate representation of the result of this function.
679678 let ret_slice = unsafe { storage_as_slice ( ret) } ;
680679
681- self . instance . id ( ) . get_mut ( store . 0 ) . post_return_arg_set (
682- self . index ,
680+ Ok ( (
681+ val ,
683682 match ret_slice. len ( ) {
684683 0 => ValRaw :: i32 ( 0 ) ,
685684 1 => ret_slice[ 0 ] ,
686685 _ => unreachable ! ( ) ,
687686 } ,
688- ) ;
689-
690- return Ok ( val) ;
687+ ) )
691688 }
692689
693690 #[ doc( hidden) ]
@@ -703,7 +700,7 @@ impl Func {
703700 Ok ( ( ) )
704701 }
705702
706- pub ( crate ) fn post_return_impl ( & self , mut store : impl AsContextMut ) -> Result < ( ) > {
703+ pub ( crate ) fn post_return_impl ( & self , mut store : impl AsContextMut , arg : ValRaw ) -> Result < ( ) > {
707704 let mut store = store. as_context_mut ( ) ;
708705
709706 let index = self . index ;
@@ -712,33 +709,9 @@ impl Func {
712709 let ( _ty, _def, options) = component. export_lifted_function ( index) ;
713710 let post_return = self . post_return_core_func ( store. 0 ) ;
714711 let flags = vminstance. instance_flags ( component. env_component ( ) . options [ options] . instance ) ;
715- let mut instance = self . instance . id ( ) . get_mut ( store. 0 ) ;
716- let post_return_arg = instance. as_mut ( ) . post_return_arg_take ( index) ;
717712
718713 unsafe {
719- // First assert that the instance is in a "needs post return" state.
720- // This will ensure that the previous action on the instance was a
721- // function call above. This flag is only set after a component
722- // function returns so this also can't be called (as expected)
723- // during a host import for example.
724- //
725- // Note, though, that this assert is not sufficient because it just
726- // means some function on this instance needs its post-return
727- // called. We need a precise post-return for a particular function
728- // which is the second assert here (the `.expect`). That will assert
729- // that this function itself needs to have its post-return called.
730- //
731- // The theory at least is that these two asserts ensure component
732- // model semantics are upheld where the host properly calls
733- // `post_return` on the right function despite the call being a
734- // separate step in the API.
735- assert ! (
736- flags. needs_post_return( ) ,
737- "post_return can only be called after a function has previously been called" ,
738- ) ;
739- let post_return_arg = post_return_arg. expect ( "calling post_return on wrong function" ) ;
740-
741- call_post_return ( & mut store, post_return, post_return_arg, flags) ?;
714+ call_post_return ( & mut store, post_return, arg, flags) ?;
742715
743716 let ( calls, host_table, _, instance) = store
744717 . 0
@@ -866,11 +839,9 @@ impl Func {
866839 fn with_lower_context < T > (
867840 self ,
868841 mut store : StoreContextMut < T > ,
869- call_post_return_automatically : bool ,
870842 lower : impl FnOnce ( & mut LowerContext < T > , InterfaceType ) -> Result < ( ) > ,
871843 ) -> Result < ( ) > {
872- let ( options_idx, mut flags, ty, options) = self . abi_info ( store. 0 ) ;
873- let async_ = options. async_ ;
844+ let ( options_idx, mut flags, ty, _) = self . abi_info ( store. 0 ) ;
874845
875846 // Perform the actual lowering, where while this is running the
876847 // component is forbidden from calling imports.
@@ -882,17 +853,7 @@ impl Func {
882853 let param_ty = InterfaceType :: Tuple ( cx. types [ ty] . params ) ;
883854 let result = lower ( & mut cx, param_ty) ;
884855 unsafe { flags. set_may_leave ( true ) } ;
885- result?;
886-
887- // If needed, flag a post-return call being required as we're about to
888- // enter wasm and afterwards need a post-return.
889- unsafe {
890- if !( call_post_return_automatically && async_) {
891- flags. set_needs_post_return ( true ) ;
892- }
893- }
894-
895- Ok ( ( ) )
856+ result
896857 }
897858
898859 /// Creates a `LiftContext` using the configuration values with this lifted
@@ -951,11 +912,6 @@ pub(crate) unsafe fn call_post_return(
951912 mut flags : InstanceFlags ,
952913) -> Result < ( ) > {
953914 unsafe {
954- // Unset the "needs post return" flag now that post-return is being
955- // processed. This will cause future invocations of this method to
956- // panic, even if the function call below traps.
957- flags. set_needs_post_return ( false ) ;
958-
959915 // Post return functions are forbidden from calling imports or
960916 // intrinsics.
961917 flags. set_may_leave ( false ) ;
@@ -966,7 +922,7 @@ pub(crate) unsafe fn call_post_return(
966922 crate :: Func :: call_unchecked_raw (
967923 & mut store. as_context_mut ( ) ,
968924 func,
969- std :: slice:: from_ref ( & arg) . into ( ) ,
925+ core :: slice:: from_ref ( & arg) . into ( ) ,
970926 ) ?;
971927 }
972928
0 commit comments