@@ -21,7 +21,40 @@ macro_rules! rtdebug {
2121 std:: eprintln!( $( $f) * ) ;
2222 }
2323 }
24+ }
2425
26+ /// Helper macro to deduplicate foreign definitions of wasm functions.
27+ ///
28+ /// This automatically imports when on wasm targets and then defines a dummy
29+ /// panicking shim for native targets to support native compilation but fail at
30+ /// runtime.
31+ macro_rules! extern_wasm {
32+ (
33+ $( #[ $extern_attr: meta] ) *
34+ extern "C" {
35+ $(
36+ $( #[ $func_attr: meta] ) *
37+ $vis: vis fn $func_name: ident ( $( $args: tt) * ) $( -> $ret: ty) ?;
38+ ) *
39+ }
40+ ) => {
41+ $(
42+ #[ cfg( not( target_family = "wasm" ) ) ]
43+ #[ allow( unused) ]
44+ $vis unsafe fn $func_name( $( $args) * ) $( -> $ret) ? {
45+ unreachable!( ) ;
46+ }
47+ ) *
48+
49+ #[ cfg( target_family = "wasm" ) ]
50+ $( #[ $extern_attr] ) *
51+ extern "C" {
52+ $(
53+ $( #[ $func_attr] ) *
54+ $vis fn $func_name( $( $args) * ) $( -> $ret) ?;
55+ ) *
56+ }
57+ } ;
2558}
2659
2760mod abi_buffer;
@@ -447,17 +480,14 @@ pub fn block_on<T: 'static>(future: impl Future<Output = T>) -> T {
447480/// at this yield point. The caller should return back and exit from the task
448481/// ASAP in this situation.
449482pub fn yield_blocking ( ) -> bool {
450- #[ cfg( not( target_arch = "wasm32" ) ) ]
451- unsafe fn yield_ ( ) -> bool {
452- unreachable ! ( ) ;
483+ extern_wasm ! {
484+ #[ link( wasm_import_module = "$root" ) ]
485+ extern "C" {
486+ #[ link_name = "[thread-yield]" ]
487+ fn yield_( ) -> bool ;
488+ }
453489 }
454490
455- #[ cfg( target_arch = "wasm32" ) ]
456- #[ link( wasm_import_module = "$root" ) ]
457- extern "C" {
458- #[ link_name = "[thread-yield]" ]
459- fn yield_ ( ) -> bool ;
460- }
461491 // Note that the return value from the raw intrinsic is inverted, the
462492 // canonical ABI returns "did this task get cancelled" while this function
463493 // works as "should work continue going".
@@ -508,82 +538,62 @@ pub async fn yield_async() {
508538/// called again with `enabled` set to `false`).
509539#[ deprecated = "use backpressure_{inc,dec} instead" ]
510540pub fn backpressure_set ( enabled : bool ) {
511- #[ cfg( not( target_arch = "wasm32" ) ) ]
512- unsafe fn backpressure_set ( _: i32 ) {
513- unreachable ! ( ) ;
514- }
515-
516- #[ cfg( target_arch = "wasm32" ) ]
517- #[ link( wasm_import_module = "$root" ) ]
518- extern "C" {
519- #[ link_name = "[backpressure-set]" ]
520- fn backpressure_set ( _: i32 ) ;
541+ extern_wasm ! {
542+ #[ link( wasm_import_module = "$root" ) ]
543+ extern "C" {
544+ #[ link_name = "[backpressure-set]" ]
545+ fn backpressure_set( _: i32 ) ;
546+ }
521547 }
522548
523549 unsafe { backpressure_set ( if enabled { 1 } else { 0 } ) }
524550}
525551
526552/// Call the `backpressure.inc` canonical built-in function.
527553pub fn backpressure_inc ( ) {
528- #[ cfg( not( target_arch = "wasm32" ) ) ]
529- unsafe fn backpressure_inc ( ) {
530- unreachable ! ( ) ;
531- }
532-
533- #[ cfg( target_arch = "wasm32" ) ]
534- #[ link( wasm_import_module = "$root" ) ]
535- extern "C" {
536- #[ link_name = "[backpressure-inc]" ]
537- fn backpressure_inc ( ) ;
554+ extern_wasm ! {
555+ #[ link( wasm_import_module = "$root" ) ]
556+ extern "C" {
557+ #[ link_name = "[backpressure-inc]" ]
558+ fn backpressure_inc( ) ;
559+ }
538560 }
539561
540562 unsafe { backpressure_inc ( ) }
541563}
542564
543565/// Call the `backpressure.dec` canonical built-in function.
544566pub fn backpressure_dec ( ) {
545- #[ cfg( not( target_arch = "wasm32" ) ) ]
546- unsafe fn backpressure_dec ( ) {
547- unreachable ! ( ) ;
548- }
549-
550- #[ cfg( target_arch = "wasm32" ) ]
551- #[ link( wasm_import_module = "$root" ) ]
552- extern "C" {
553- #[ link_name = "[backpressure-dec]" ]
554- fn backpressure_dec ( ) ;
567+ extern_wasm ! {
568+ #[ link( wasm_import_module = "$root" ) ]
569+ extern "C" {
570+ #[ link_name = "[backpressure-dec]" ]
571+ fn backpressure_dec( ) ;
572+ }
555573 }
556574
557575 unsafe { backpressure_dec ( ) }
558576}
559577
560578fn context_get ( ) -> * mut u8 {
561- #[ cfg( not( target_arch = "wasm32" ) ) ]
562- unsafe fn get ( ) -> * mut u8 {
563- unreachable ! ( )
564- }
565-
566- #[ cfg( target_arch = "wasm32" ) ]
567- #[ link( wasm_import_module = "$root" ) ]
568- extern "C" {
569- #[ link_name = "[context-get-0]" ]
570- fn get ( ) -> * mut u8 ;
579+ extern_wasm ! {
580+ #[ link( wasm_import_module = "$root" ) ]
581+ extern "C" {
582+ #[ link_name = "[context-get-0]" ]
583+ fn get( ) -> * mut u8 ;
584+ }
571585 }
572586
573587 unsafe { get ( ) }
574588}
575589
576590unsafe fn context_set ( value : * mut u8 ) {
577- #[ cfg( not( target_arch = "wasm32" ) ) ]
578- unsafe fn set ( _: * mut u8 ) {
579- unreachable ! ( )
580- }
581-
582- #[ cfg( target_arch = "wasm32" ) ]
583- #[ link( wasm_import_module = "$root" ) ]
584- extern "C" {
585- #[ link_name = "[context-set-0]" ]
586- fn set ( value : * mut u8 ) ;
591+ extern_wasm ! {
592+ #[ link( wasm_import_module = "$root" ) ]
593+ extern "C" {
594+ #[ link_name = "[context-set-0]" ]
595+ fn set( value: * mut u8 ) ;
596+ }
587597 }
588598
589599 unsafe { set ( value) }
@@ -608,16 +618,12 @@ impl TaskCancelOnDrop {
608618
609619impl Drop for TaskCancelOnDrop {
610620 fn drop ( & mut self ) {
611- #[ cfg( not( target_arch = "wasm32" ) ) ]
612- unsafe fn cancel ( ) {
613- unreachable ! ( )
614- }
615-
616- #[ cfg( target_arch = "wasm32" ) ]
617- #[ link( wasm_import_module = "[export]$root" ) ]
618- extern "C" {
619- #[ link_name = "[task-cancel]" ]
620- fn cancel ( ) ;
621+ extern_wasm ! {
622+ #[ link( wasm_import_module = "[export]$root" ) ]
623+ extern "C" {
624+ #[ link_name = "[task-cancel]" ]
625+ fn cancel( ) ;
626+ }
621627 }
622628
623629 unsafe { cancel ( ) }
0 commit comments