Skip to content

Commit 1d8cbb9

Browse files
alexcrichtondicej
andauthored
Update support for p3 to match the latest wasm-tools (#1198)
* Update wasm-tools dependencies * Fixes from wasm-tools update to p3 support Fix the name and signature of various intrinsics in the Rust bindings generator for p3 support. This is detected by updating wasm-tools in the wasip3-prototyping repository which requires changes here. * minimal stub of waitable-set support This is temporary (and relies on the host to ignore the waitable-set parameter) until we have proper waitable-set support, which I'll work on soon. Signed-off-by: Joel Dice <joel.dice@fermyon.com> --------- Signed-off-by: Joel Dice <joel.dice@fermyon.com> Co-authored-by: Joel Dice <joel.dice@fermyon.com>
1 parent b4d62a4 commit 1d8cbb9

File tree

5 files changed

+38
-37
lines changed

5 files changed

+38
-37
lines changed

crates/core/src/abi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
985985

986986
assert_eq!(self.stack.len(), 2);
987987
self.emit(&Instruction::AsyncCallWasm {
988-
name: &format!("[async]{}", func.name),
988+
name: &format!("[async-lower]{}", func.name),
989989
size: params_size.size_wasm32(),
990990
align: params_align.align_wasm32(),
991991
});

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl ErrorContext {
388388
{
389389
#[link(wasm_import_module = "$root")]
390390
extern "C" {
391-
#[link_name = "[error-context-new;encoding=utf8]"]
391+
#[link_name = "[error-context-new-utf8]"]
392392
fn context_new(_: *const u8, _: usize) -> i32;
393393
}
394394

@@ -427,7 +427,7 @@ impl ErrorContext {
427427
}
428428
#[link(wasm_import_module = "$root")]
429429
extern "C" {
430-
#[link_name = "[error-context-debug-message;encoding=utf8;realloc=cabi_realloc]"]
430+
#[link_name = "[error-context-debug-message-utf8]"]
431431
fn error_context_debug_message(_: u32, _: &mut RetPtr);
432432
}
433433

@@ -484,7 +484,7 @@ pub fn spawn(future: impl Future<Output = ()> + 'static) {
484484
unsafe { SPAWNED.push(Box::pin(future)) }
485485
}
486486

487-
fn task_wait(state: &mut FutureState) {
487+
fn waitable_set_wait(state: &mut FutureState) {
488488
#[cfg(not(target_arch = "wasm32"))]
489489
{
490490
_ = state;
@@ -495,21 +495,22 @@ fn task_wait(state: &mut FutureState) {
495495
{
496496
#[link(wasm_import_module = "$root")]
497497
extern "C" {
498-
#[link_name = "[task-wait]"]
499-
fn wait(_: *mut i32) -> i32;
498+
#[link_name = "[waitable-set-wait]"]
499+
fn wait(_: u32, _: *mut i32) -> i32;
500500
}
501501
let mut payload = [0i32; 2];
502502
unsafe {
503-
let event0 = wait(payload.as_mut_ptr());
503+
// TODO: provide a real waitable-set here:
504+
let event0 = wait(0, payload.as_mut_ptr());
504505
callback(state as *mut _ as _, event0, payload[0], payload[1]);
505506
}
506507
}
507508
}
508509

509510
/// Run the specified future to completion, returning the result.
510511
///
511-
/// This uses `task.wait` to poll for progress on any in-progress calls to
512-
/// async-lowered imports as necessary.
512+
/// This uses `waitable-set.wait` to poll for progress on any in-progress calls
513+
/// to async-lowered imports as necessary.
513514
// TODO: refactor so `'static` bounds aren't necessary
514515
pub fn block_on<T: 'static>(future: impl Future<Output = T> + 'static) -> T {
515516
let (tx, mut rx) = oneshot::channel();
@@ -524,12 +525,12 @@ pub fn block_on<T: 'static>(future: impl Future<Output = T> + 'static) -> T {
524525
loop {
525526
match unsafe { poll(state) } {
526527
Poll::Ready(()) => break rx.try_recv().unwrap().unwrap(),
527-
Poll::Pending => task_wait(state),
528+
Poll::Pending => waitable_set_wait(state),
528529
}
529530
}
530531
}
531532

532-
/// Call the `task.yield` canonical built-in function.
533+
/// Call the `yield` canonical built-in function.
533534
///
534535
/// This yields control to the host temporarily, allowing other tasks to make
535536
/// progress. It's a good idea to call this inside a busy loop which does not
@@ -544,7 +545,7 @@ pub fn task_yield() {
544545
{
545546
#[link(wasm_import_module = "$root")]
546547
extern "C" {
547-
#[link_name = "[task-yield]"]
548+
#[link_name = "[yield]"]
548549
fn yield_();
549550
}
550551
unsafe {
@@ -553,12 +554,12 @@ pub fn task_yield() {
553554
}
554555
}
555556

556-
/// Call the `task.backpressure` canonical built-in function.
557+
/// Call the `backpressure.set` canonical built-in function.
557558
///
558559
/// When `enabled` is `true`, this tells the host to defer any new calls to this
559-
/// component instance until further notice (i.e. until `task.backpressure` is
560+
/// component instance until further notice (i.e. until `backpressure.set` is
560561
/// called again with `enabled` set to `false`).
561-
pub fn task_backpressure(enabled: bool) {
562+
pub fn backpressure_set(enabled: bool) {
562563
#[cfg(not(target_arch = "wasm32"))]
563564
{
564565
_ = enabled;
@@ -569,11 +570,11 @@ pub fn task_backpressure(enabled: bool) {
569570
{
570571
#[link(wasm_import_module = "$root")]
571572
extern "C" {
572-
#[link_name = "[task-backpressure]"]
573-
fn backpressure(_: i32);
573+
#[link_name = "[backpressure-set]"]
574+
fn backpressure_set(_: i32);
574575
}
575576
unsafe {
576-
backpressure(if enabled { 1 } else { 0 });
577+
backpressure_set(if enabled { 1 } else { 0 });
577578
}
578579
}
579580
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct FutureVtable<T> {
2525
pub cancel_write: unsafe extern "C" fn(future: u32) -> u32,
2626
pub cancel_read: unsafe extern "C" fn(future: u32) -> u32,
2727
pub close_writable: unsafe extern "C" fn(future: u32, err_ctx: u32),
28-
pub close_readable: unsafe extern "C" fn(future: u32),
28+
pub close_readable: unsafe extern "C" fn(future: u32, err_ctx: u32),
2929
pub new: unsafe extern "C" fn() -> u32,
3030
}
3131

@@ -418,7 +418,9 @@ impl<T> Drop for FutureReader<T> {
418418
}
419419
Handle::Read | Handle::LocalClosed => unsafe {
420420
entry.remove();
421-
(self.vtable.close_readable)(handle);
421+
// TODO: expose `0` here as an error context in the
422+
// API (or auto-fill-in? unsure).
423+
(self.vtable.close_readable)(handle, 0);
422424
},
423425
Handle::Write | Handle::WriteClosedErr(_) => unreachable!(),
424426
},

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub struct StreamVtable<T> {
3838
pub cancel_write: unsafe extern "C" fn(future: u32) -> u32,
3939
pub cancel_read: unsafe extern "C" fn(future: u32) -> u32,
4040
pub close_writable: unsafe extern "C" fn(future: u32, err_ctx: u32),
41-
pub close_readable: unsafe extern "C" fn(future: u32),
41+
pub close_readable: unsafe extern "C" fn(future: u32, err_ctx: u32),
4242
pub new: unsafe extern "C" fn() -> u32,
4343
}
4444

@@ -487,7 +487,9 @@ impl<T> Drop for StreamReader<T> {
487487
}
488488
Handle::Read | Handle::LocalClosed => unsafe {
489489
entry.remove();
490-
(self.vtable.close_readable)(handle);
490+
// TODO: expose `0` here as an error context in the
491+
// API (or auto-fill-in? unsure).
492+
(self.vtable.close_readable)(handle, 0);
491493
},
492494
Handle::Write | Handle::WriteClosedErr(_) => unreachable!(),
493495
},

crates/rust/src/interface.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ pub mod vtable{ordinal} {{
578578
#[cfg(not(target_arch = "wasm32"))]
579579
unsafe extern "C" fn close_writable(_: u32, _: u32) {{ unreachable!() }}
580580
#[cfg(not(target_arch = "wasm32"))]
581-
unsafe extern "C" fn close_readable(_: u32) {{ unreachable!() }}
581+
unsafe extern "C" fn close_readable(_: u32, _: u32) {{ unreachable!() }}
582582
#[cfg(not(target_arch = "wasm32"))]
583583
unsafe extern "C" fn new() -> u32 {{ unreachable!() }}
584584
#[cfg(not(target_arch = "wasm32"))]
@@ -598,12 +598,10 @@ pub mod vtable{ordinal} {{
598598
#[link_name = "[future-close-writable-{index}]{func_name}"]
599599
fn close_writable(_: u32, _: u32);
600600
#[link_name = "[future-close-readable-{index}]{func_name}"]
601-
fn close_readable(_: u32);
602-
#[link_name = "[future-new-{index}]{func_name}"]
603-
fn new() -> u32;
604-
#[link_name = "[async][future-read-{index}]{func_name}"]
601+
fn close_readable(_: u32, _: u32);
602+
#[link_name = "[async-lower][future-read-{index}]{func_name}"]
605603
fn start_read(_: u32, _: *mut u8) -> u32;
606-
#[link_name = "[async][future-write-{index}]{func_name}"]
604+
#[link_name = "[async-lower][future-write-{index}]{func_name}"]
607605
fn start_write(_: u32, _: *mut u8) -> u32;
608606
}}
609607
@@ -762,7 +760,7 @@ pub mod vtable{ordinal} {{
762760
#[cfg(not(target_arch = "wasm32"))]
763761
unsafe extern "C" fn close_writable(_: u32, _: u32) {{ unreachable!() }}
764762
#[cfg(not(target_arch = "wasm32"))]
765-
unsafe extern "C" fn close_readable(_: u32) {{ unreachable!() }}
763+
unsafe extern "C" fn close_readable(_: u32, _: u32) {{ unreachable!() }}
766764
#[cfg(not(target_arch = "wasm32"))]
767765
unsafe extern "C" fn new() -> u32 {{ unreachable!() }}
768766
#[cfg(not(target_arch = "wasm32"))]
@@ -782,12 +780,10 @@ pub mod vtable{ordinal} {{
782780
#[link_name = "[stream-close-writable-{index}]{func_name}"]
783781
fn close_writable(_: u32, _: u32);
784782
#[link_name = "[stream-close-readable-{index}]{func_name}"]
785-
fn close_readable(_: u32);
786-
#[link_name = "[stream-new-{index}]{func_name}"]
787-
fn new() -> u32;
788-
#[link_name = "[async][stream-read-{index}]{func_name}"]
783+
fn close_readable(_: u32, _: u32);
784+
#[link_name = "[async-lower][stream-read-{index}]{func_name}"]
789785
fn start_read(_: u32, _: *mut u8, _: u32) -> u32;
790-
#[link_name = "[async][stream-write-{index}]{func_name}"]
786+
#[link_name = "[async-lower][stream-write-{index}]{func_name}"]
791787
fn start_write(_: u32, _: *mut u8, _: u32) -> u32;
792788
}}
793789
@@ -817,7 +813,7 @@ pub mod vtable{ordinal} {{
817813
return;
818814
}
819815

820-
self.generate_payloads("[import-payload]", func, interface);
816+
self.generate_payloads("", func, interface);
821817

822818
let async_ = match &self.gen.opts.async_ {
823819
AsyncConfig::None => false,
@@ -924,7 +920,7 @@ pub mod vtable{ordinal} {{
924920
) {
925921
let name_snake = func.name.to_snake_case().replace('.', "_");
926922

927-
self.generate_payloads("[export-payload]", func, interface);
923+
self.generate_payloads("[export]", func, interface);
928924

929925
uwrite!(
930926
self.src,
@@ -1051,7 +1047,7 @@ pub mod vtable{ordinal} {{
10511047
let export_prefix = self.gen.opts.export_prefix.as_deref().unwrap_or("");
10521048
let export_name = func.legacy_core_export_name(wasm_module_export_name.as_deref());
10531049
let export_name = if async_ {
1054-
format!("[async]{export_name}")
1050+
format!("[async-lift]{export_name}")
10551051
} else {
10561052
export_name.to_string()
10571053
};

0 commit comments

Comments
 (0)