@@ -270,11 +270,14 @@ where
270270}
271271
272272macro_rules! vm_function_impl {
273- ( [ $( $f: tt) * ] $( $args: ident) ,* ) => {
273+ ( [ $( $f: tt) * ] $( $args: ident) ,* -> $ret : ident , $ret_ty : ty ) => {
274274
275- impl <' vm, $( $args, ) * R > VmFunction <' vm> for $( $f) * ( $( $args) ,* ) -> R
275+ impl <' vm, $( $args, ) * $ret > VmFunction <' vm> for $( $f) * ( $( $args) ,* ) -> $ret_ty
276276where $( $args: Getable <' vm, ' vm> + ' vm, ) *
277- R : AsyncPushable <' vm> + VmType + ' vm
277+ $ret: AsyncPushable <' vm> + VmType + ' vm,
278+ $ret_ty: AsyncPushable <' vm> + VmType + ' vm,
279+ $ret:: Type : Sized ,
280+ <$ret_ty as VmType >:: Type : Sized ,
278281{
279282 #[ allow( non_snake_case, unused_mut, unused_assignments, unused_variables, unused_unsafe) ]
280283 fn unpack_and_call( & self , vm: & ' vm Thread ) -> Status {
@@ -341,49 +344,76 @@ where
341344}
342345
343346macro_rules! make_vm_function {
344- ( $( $args: ident) ,* ) => (
345- impl <$( $args: VmType , ) * R : VmType > VmType for fn ( $( $args) ,* ) -> R {
347+ ( $( $args: ident) ,* ) => {
348+ make_vm_function_inner!( $( $args) ,* -> R , R ) ;
349+ }
350+ }
351+ macro_rules! make_vm_function_inner {
352+ ( $( $args: ident) ,* -> $ret: ident, $ret_ty: ty) => (
353+ impl <$( $args: VmType , ) * $ret: VmType > VmType for fn ( $( $args) ,* ) -> $ret_ty
354+ where
355+ <$ret_ty as VmType >:: Type : Sized ,
356+ <$ret as VmType >:: Type : Sized ,
357+ {
346358 #[ allow( non_snake_case) ]
347- type Type = fn ( $( $args:: Type ) ,* ) -> R :: Type ;
359+ type Type = fn ( $( $args:: Type ) ,* ) -> <$ret_ty as VmType > :: Type ;
348360
349361 #[ allow( non_snake_case) ]
350362 fn make_type( vm: & Thread ) -> ArcType {
351363 let args = vec![ $( make_type:: <$args>( vm) ) ,* ] ;
352- vm. global_env( ) . type_cache( ) . function( args, make_type:: <R >( vm) )
364+ vm. global_env( ) . type_cache( ) . function( args, make_type:: <$ret_ty >( vm) )
353365 }
354366}
355367
356- vm_function_impl!( [ fn ] $( $args) ,* ) ;
357- vm_function_impl!( [ dyn Fn ] $( $args) ,* ) ;
368+ vm_function_impl!( [ fn ] $( $args) ,* -> $ret , $ret_ty ) ;
369+ vm_function_impl!( [ dyn Fn ] $( $args) ,* -> $ret , $ret_ty ) ;
358370
359- impl <' vm, $( $args, ) * R : VmType > FunctionType for fn ( $( $args) ,* ) -> R {
371+ impl <' vm, $( $args, ) * $ret: VmType > FunctionType for fn ( $( $args) ,* ) -> $ret_ty
372+ where
373+ $ret_ty: VmType ,
374+ <$ret_ty as VmType >:: Type : Sized ,
375+ $ret:: Type : Sized ,
376+ {
360377 fn arguments( ) -> VmIndex {
361- count!( $( $args) ,* ) + R :: EXTRA_ARGS
378+ count!( $( $args) ,* ) + <$ret_ty as VmType > :: EXTRA_ARGS
362379 }
363380}
364381
365- impl <' s, $( $args, ) * R : VmType > FunctionType for dyn Fn ( $( $args) ,* ) -> R + ' s {
382+ impl <' s, $( $args, ) * $ret: VmType > FunctionType for dyn Fn ( $( $args) ,* ) -> $ret_ty + ' s
383+ where
384+ $ret_ty: VmType ,
385+ <$ret_ty as VmType >:: Type : Sized ,
386+ $ret:: Type : Sized ,
387+ {
366388 fn arguments( ) -> VmIndex {
367- count!( $( $args) ,* ) + R :: EXTRA_ARGS
389+ count!( $( $args) ,* ) + <$ret_ty as VmType > :: EXTRA_ARGS
368390 }
369391}
370392
371- impl <' s, $( $args: VmType , ) * R : VmType > VmType for dyn Fn ( $( $args) ,* ) -> R + ' s {
372- type Type = fn ( $( $args:: Type ) ,* ) -> R :: Type ;
393+ impl <' s, $( $args: VmType , ) * $ret: VmType > VmType for dyn Fn ( $( $args) ,* ) -> $ret_ty + ' s
394+ where
395+ $ret_ty: VmType ,
396+ <$ret_ty as VmType >:: Type : Sized ,
397+ $ret:: Type : Sized ,
398+ {
399+ type Type = fn ( $( $args:: Type ) ,* ) -> <$ret_ty as VmType >:: Type ;
373400
374401 #[ allow( non_snake_case) ]
375402 fn make_type( vm: & Thread ) -> ArcType {
376- <fn ( $( $args) ,* ) -> R >:: make_type( vm)
403+ <fn ( $( $args) ,* ) -> $ret_ty >:: make_type( vm)
377404 }
378405}
379406
380- impl <T , $( $args, ) * R > Function <T , fn ( $( $args) ,* ) -> R >
381- where $( $args: for <' vm> Pushable <' vm>, ) *
382- T : VmRootInternal ,
383- R : VmType + for <' x, ' value> Getable <' x, ' value>,
407+ impl <T , $( $args, ) * $ret> Function <T , fn ( $( $args) ,* ) -> $ret_ty>
408+ where
409+ $( $args: for <' vm> Pushable <' vm>, ) *
410+ T : VmRootInternal ,
411+ $ret: VmType + for <' x, ' value> Getable <' x, ' value>,
412+ <$ret_ty as VmType >:: Type : Sized ,
413+ $ret:: Type : Sized ,
384414{
385415 #[ allow( non_snake_case) ]
386- pub fn call( & mut self $( , $args: $args) * ) -> Result <R > {
416+ pub fn call( & mut self $( , $args: $args) * ) -> Result <$ret_ty > {
387417 block_on_sync( future:: lazy( |cx| {
388418 match self . call_first( cx, $( $args) ,* ) {
389419 Poll :: Ready ( r) => r,
@@ -393,17 +423,17 @@ impl<T, $($args,)* R> Function<T, fn($($args),*) -> R>
393423 }
394424
395425 #[ allow( non_snake_case) ]
396- fn call_first( & self , cx: & mut task:: Context <' _> $( , $args: $args) * ) -> Poll <Result <R >> {
426+ fn call_first( & self , cx: & mut task:: Context <' _> $( , $args: $args) * ) -> Poll <Result <$ret_ty >> {
397427 let vm = self . value. vm( ) ;
398428 let mut context = vm. current_context( ) ;
399429 context. push( self . value. get_variant( ) ) ;
400430 $(
401431 $args. vm_push( & mut context) ?;
402432 ) *
403- for _ in 0 ..R :: EXTRA_ARGS {
433+ for _ in 0 ..<$ret_ty as VmType > :: EXTRA_ARGS {
404434 0 . vm_push( & mut context) . unwrap( ) ;
405435 }
406- let args = count!( $( $args) ,* ) + R :: EXTRA_ARGS ;
436+ let args = count!( $( $args) ,* ) + <$ret_ty as VmType > :: EXTRA_ARGS ;
407437 let context = ready!( vm. call_function( cx, context. into_owned( ) , args) ) ?;
408438 let mut context = context. unwrap( ) ;
409439 let result = {
@@ -414,21 +444,24 @@ impl<T, $($args,)* R> Function<T, fn($($args),*) -> R>
414444 Poll :: Ready ( result)
415445 }
416446
417- fn return_value( vm: & Thread , value: Variants ) -> Result <R > {
418- Ok ( R :: from_value( vm, value) )
447+ fn return_value( vm: & Thread , value: Variants ) -> Result <$ret_ty > {
448+ Ok ( <$ret_ty> :: from_value( vm, value) )
419449 }
420450}
421451
422- impl <T , $( $args, ) * R > Function <T , fn ( $( $args) ,* ) -> R >
423- where $( $args: for <' vm> Pushable <' vm>, ) *
424- T : VmRootInternal + Clone + Send ,
425- R : VmType + for <' x, ' value> Getable <' x, ' value> + Send + Sync + ' static ,
452+ impl <T , $( $args, ) * $ret> Function <T , fn ( $( $args) ,* ) -> $ret_ty>
453+ where
454+ $( $args: for <' vm> Pushable <' vm>, ) *
455+ T : VmRootInternal + Clone + Send ,
456+ $ret: VmType + for <' x, ' value> Getable <' x, ' value> + Send + Sync + ' static ,
457+ <$ret_ty as VmType >:: Type : Sized ,
458+ $ret:: Type : Sized ,
426459{
427460 #[ allow( non_snake_case) ]
428461 pub async fn call_async(
429462 & mut self
430463 $( , $args: $args) *
431- ) -> Result <R >
464+ ) -> Result <$ret_ty >
432465 {
433466 use crate :: thread:: Execute ;
434467 match future:: lazy( |cx| self . call_first( cx, $( $args) ,* ) ) . await {
@@ -444,7 +477,7 @@ impl<T, $($args,)* R> Function<T, fn($($args),*) -> R>
444477 )
445478}
446479
447- make_vm_function ! ( ) ;
480+ make_vm_function_inner ! ( -> R , crate :: api :: IO < R > ) ;
448481make_vm_function ! ( A ) ;
449482make_vm_function ! ( A , B ) ;
450483make_vm_function ! ( A , B , C ) ;
0 commit comments