Skip to content

Commit f009ee0

Browse files
committed
Simplify extern definitions for intrinsics
Deduplicate host panicking shim definitions with the wasm definitions.
1 parent 87c646e commit f009ee0

File tree

5 files changed

+118
-162
lines changed

5 files changed

+118
-162
lines changed

crates/guest-rust/src/rt/async_support.rs

Lines changed: 70 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,35 @@ macro_rules! rtdebug {
2121
std::eprintln!($($f)*);
2222
}
2323
}
24+
}
25+
26+
macro_rules! extern_wasm {
27+
(
28+
$(#[$extern_attr:meta])*
29+
extern "C" {
30+
$(
31+
$(#[$func_attr:meta])*
32+
$vis:vis fn $func_name:ident ( $($args:tt)* ) $(-> $ret:ty)?;
33+
)*
34+
}
35+
) => {
36+
$(
37+
#[cfg(not(target_family = "wasm"))]
38+
#[allow(unused)]
39+
$vis unsafe fn $func_name($($args)*) $(-> $ret)? {
40+
unreachable!();
41+
}
42+
)*
2443

44+
#[cfg(target_family = "wasm")]
45+
$(#[$extern_attr])*
46+
extern "C" {
47+
$(
48+
$(#[$func_attr])*
49+
$vis fn $func_name($($args)*) $(-> $ret)?;
50+
)*
51+
}
52+
};
2553
}
2654

2755
mod abi_buffer;
@@ -447,17 +475,14 @@ pub fn block_on<T: 'static>(future: impl Future<Output = T>) -> T {
447475
/// at this yield point. The caller should return back and exit from the task
448476
/// ASAP in this situation.
449477
pub fn yield_blocking() -> bool {
450-
#[cfg(not(target_arch = "wasm32"))]
451-
unsafe fn yield_() -> bool {
452-
unreachable!();
478+
extern_wasm! {
479+
#[link(wasm_import_module = "$root")]
480+
extern "C" {
481+
#[link_name = "[thread-yield]"]
482+
fn yield_() -> bool;
483+
}
453484
}
454485

455-
#[cfg(target_arch = "wasm32")]
456-
#[link(wasm_import_module = "$root")]
457-
extern "C" {
458-
#[link_name = "[thread-yield]"]
459-
fn yield_() -> bool;
460-
}
461486
// Note that the return value from the raw intrinsic is inverted, the
462487
// canonical ABI returns "did this task get cancelled" while this function
463488
// works as "should work continue going".
@@ -508,82 +533,62 @@ pub async fn yield_async() {
508533
/// called again with `enabled` set to `false`).
509534
#[deprecated = "use backpressure_{inc,dec} instead"]
510535
pub 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);
536+
extern_wasm! {
537+
#[link(wasm_import_module = "$root")]
538+
extern "C" {
539+
#[link_name = "[backpressure-set]"]
540+
fn backpressure_set(_: i32);
541+
}
521542
}
522543

523544
unsafe { backpressure_set(if enabled { 1 } else { 0 }) }
524545
}
525546

526547
/// Call the `backpressure.inc` canonical built-in function.
527548
pub 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();
549+
extern_wasm! {
550+
#[link(wasm_import_module = "$root")]
551+
extern "C" {
552+
#[link_name = "[backpressure-inc]"]
553+
fn backpressure_inc();
554+
}
538555
}
539556

540557
unsafe { backpressure_inc() }
541558
}
542559

543560
/// Call the `backpressure.dec` canonical built-in function.
544561
pub 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();
562+
extern_wasm! {
563+
#[link(wasm_import_module = "$root")]
564+
extern "C" {
565+
#[link_name = "[backpressure-dec]"]
566+
fn backpressure_dec();
567+
}
555568
}
556569

557570
unsafe { backpressure_dec() }
558571
}
559572

560573
fn 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;
574+
extern_wasm! {
575+
#[link(wasm_import_module = "$root")]
576+
extern "C" {
577+
#[link_name = "[context-get-0]"]
578+
fn get() -> *mut u8;
579+
}
571580
}
572581

573582
unsafe { get() }
574583
}
575584

576585
unsafe 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);
586+
extern_wasm! {
587+
#[link(wasm_import_module = "$root")]
588+
extern "C" {
589+
#[link_name = "[context-set-0]"]
590+
fn set(value: *mut u8);
591+
}
587592
}
588593

589594
unsafe { set(value) }
@@ -608,16 +613,12 @@ impl TaskCancelOnDrop {
608613

609614
impl Drop for TaskCancelOnDrop {
610615
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();
616+
extern_wasm! {
617+
#[link(wasm_import_module = "[export]$root")]
618+
extern "C" {
619+
#[link_name = "[task-cancel]"]
620+
fn cancel();
621+
}
621622
}
622623

623624
unsafe { cancel() }

crates/guest-rust/src/rt/async_support/cabi.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,21 @@
4646
4747
use core::ffi::c_void;
4848

49-
#[cfg(target_family = "wasm")]
50-
extern "C" {
51-
/// Sets the global task pointer to `ptr` provided. Returns the previous
52-
/// value.
53-
///
54-
/// This function acts as a dual getter and a setter. To get the
55-
/// current task pointer a dummy `ptr` can be provided (e.g. NULL) and then
56-
/// it's passed back when you're done working with it. When setting the
57-
/// current task pointer it's recommended to call this and then call it
58-
/// again with the previous value when the tasks's work is done.
59-
///
60-
/// For executors they need to ensure that the `ptr` passed in lives for
61-
/// the entire lifetime of the component model task.
62-
pub fn wasip3_task_set(ptr: *mut wasip3_task) -> *mut wasip3_task;
63-
}
64-
65-
#[cfg(not(target_family = "wasm"))]
66-
pub unsafe extern "C" fn wasip3_task_set(ptr: *mut wasip3_task) -> *mut wasip3_task {
67-
let _ = ptr;
68-
unreachable!();
49+
extern_wasm! {
50+
extern "C" {
51+
/// Sets the global task pointer to `ptr` provided. Returns the previous
52+
/// value.
53+
///
54+
/// This function acts as a dual getter and a setter. To get the
55+
/// current task pointer a dummy `ptr` can be provided (e.g. NULL) and then
56+
/// it's passed back when you're done working with it. When setting the
57+
/// current task pointer it's recommended to call this and then call it
58+
/// again with the previous value when the tasks's work is done.
59+
///
60+
/// For executors they need to ensure that the `ptr` passed in lives for
61+
/// the entire lifetime of the component model task.
62+
pub fn wasip3_task_set(ptr: *mut wasip3_task) -> *mut wasip3_task;
63+
}
6964
}
7065

7166
/// The first version of `wasip3_task` which implies the existence of the

crates/guest-rust/src/rt/async_support/error_context.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ impl std::error::Error for ErrorContext {}
6060

6161
impl Drop for ErrorContext {
6262
fn drop(&mut self) {
63-
#[cfg(target_arch = "wasm32")]
64-
unsafe {
65-
drop(self.handle)
66-
}
63+
unsafe { drop(self.handle) }
6764
}
6865
}
6966

@@ -73,22 +70,14 @@ struct RetPtr {
7370
len: usize,
7471
}
7572

76-
#[cfg(not(target_arch = "wasm32"))]
77-
unsafe fn new(_: *const u8, _: usize) -> u32 {
78-
unreachable!()
79-
}
80-
#[cfg(not(target_arch = "wasm32"))]
81-
fn debug_message(_: u32, _: &mut RetPtr) {
82-
unreachable!()
83-
}
84-
85-
#[cfg(target_arch = "wasm32")]
86-
#[link(wasm_import_module = "$root")]
87-
extern "C" {
88-
#[link_name = "[error-context-new-utf8]"]
89-
fn new(_: *const u8, _: usize) -> u32;
90-
#[link_name = "[error-context-drop]"]
91-
fn drop(_: u32);
92-
#[link_name = "[error-context-debug-message-utf8]"]
93-
fn debug_message(_: u32, _: &mut RetPtr);
73+
extern_wasm! {
74+
#[link(wasm_import_module = "$root")]
75+
extern "C" {
76+
#[link_name = "[error-context-new-utf8]"]
77+
fn new(_: *const u8, _: usize) -> u32;
78+
#[link_name = "[error-context-drop]"]
79+
fn drop(_: u32);
80+
#[link_name = "[error-context-debug-message-utf8]"]
81+
fn debug_message(_: u32, _: &mut RetPtr);
82+
}
9483
}

crates/guest-rust/src/rt/async_support/subtask.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -273,21 +273,12 @@ impl<T: Subtask> InProgress<T> {
273273
}
274274
}
275275

276-
#[cfg(not(target_arch = "wasm32"))]
277-
unsafe fn drop(_: u32) {
278-
unreachable!()
279-
}
280-
281-
#[cfg(not(target_arch = "wasm32"))]
282-
unsafe fn cancel(_: u32) -> u32 {
283-
unreachable!()
284-
}
285-
286-
#[cfg(target_arch = "wasm32")]
287-
#[link(wasm_import_module = "$root")]
288-
extern "C" {
289-
#[link_name = "[subtask-cancel]"]
290-
fn cancel(handle: u32) -> u32;
291-
#[link_name = "[subtask-drop]"]
292-
fn drop(handle: u32);
276+
extern_wasm! {
277+
#[link(wasm_import_module = "$root")]
278+
extern "C" {
279+
#[link_name = "[subtask-cancel]"]
280+
fn cancel(handle: u32) -> u32;
281+
#[link_name = "[subtask-drop]"]
282+
fn drop(handle: u32);
283+
}
293284
}

crates/guest-rust/src/rt/async_support/waitable_set.rs

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -63,38 +63,18 @@ impl Drop for WaitableSet {
6363
}
6464
}
6565

66-
#[cfg(not(target_arch = "wasm32"))]
67-
unsafe fn new() -> u32 {
68-
unreachable!()
69-
}
70-
#[cfg(not(target_arch = "wasm32"))]
71-
unsafe fn drop(_: u32) {
72-
unreachable!()
73-
}
74-
#[cfg(not(target_arch = "wasm32"))]
75-
unsafe fn join(_: u32, _: u32) {
76-
unreachable!()
77-
}
78-
#[cfg(not(target_arch = "wasm32"))]
79-
unsafe fn wait(_: u32, _: *mut [u32; 2]) -> u32 {
80-
unreachable!();
81-
}
82-
#[cfg(not(target_arch = "wasm32"))]
83-
unsafe fn poll(_: u32, _: *mut [u32; 2]) -> u32 {
84-
unreachable!();
85-
}
86-
87-
#[cfg(target_arch = "wasm32")]
88-
#[link(wasm_import_module = "$root")]
89-
extern "C" {
90-
#[link_name = "[waitable-set-new]"]
91-
fn new() -> u32;
92-
#[link_name = "[waitable-set-drop]"]
93-
fn drop(set: u32);
94-
#[link_name = "[waitable-join]"]
95-
fn join(waitable: u32, set: u32);
96-
#[link_name = "[waitable-set-wait]"]
97-
fn wait(_: u32, _: *mut [u32; 2]) -> u32;
98-
#[link_name = "[waitable-set-poll]"]
99-
fn poll(_: u32, _: *mut [u32; 2]) -> u32;
66+
extern_wasm! {
67+
#[link(wasm_import_module = "$root")]
68+
extern "C" {
69+
#[link_name = "[waitable-set-new]"]
70+
fn new() -> u32;
71+
#[link_name = "[waitable-set-drop]"]
72+
fn drop(set: u32);
73+
#[link_name = "[waitable-join]"]
74+
fn join(waitable: u32, set: u32);
75+
#[link_name = "[waitable-set-wait]"]
76+
fn wait(_: u32, _: *mut [u32; 2]) -> u32;
77+
#[link_name = "[waitable-set-poll]"]
78+
fn poll(_: u32, _: *mut [u32; 2]) -> u32;
79+
}
10080
}

0 commit comments

Comments
 (0)