Skip to content

Commit 05a711f

Browse files
authored
Deduplicate static/dynamic host function code paths (#12146)
* Deduplicate static/dynamic host function code paths This commit refactors the `component/func/host.rs` file to deduplicate the paths between static/dynamic host functions. Previously there was a significant amount of duplication between the two which has been exacerbated through time. This commit refactors the state of affairs to ensure that all the shared logic between the two is in one location and the only difference is what they're already doing different (e.g. lifting/lowering guts). The high-level goal here was to see if this was possible, but in the end this feels like a much cleaner state of affairs than prior as far fewer details are duplicated across a few locations. The host function behavior is slightly more "dynamic" than before in the sense that statically-known signature has a few more type lookups than before, for example, but this can be fixed in due time if necessary. * Fix compile warnings/issues
1 parent 99ef35d commit 05a711f

File tree

5 files changed

+494
-798
lines changed

5 files changed

+494
-798
lines changed

crates/wasmtime/src/runtime/component/concurrent.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ use std::vec::Vec;
8282
use table::{TableDebug, TableId};
8383
use wasmtime_environ::Trap;
8484
use wasmtime_environ::component::{
85-
CanonicalOptions, CanonicalOptionsDataModel, ExportIndex, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS,
86-
OptionsIndex, PREPARE_ASYNC_NO_RESULT, PREPARE_ASYNC_WITH_RESULT,
85+
CanonicalAbiInfo, CanonicalOptions, CanonicalOptionsDataModel, ExportIndex, MAX_FLAT_PARAMS,
86+
MAX_FLAT_RESULTS, OptionsIndex, PREPARE_ASYNC_NO_RESULT, PREPARE_ASYNC_WITH_RESULT,
8787
RuntimeComponentInstanceIndex, RuntimeTableIndex, StringEncoding,
8888
TypeComponentGlobalErrorContextTableIndex, TypeComponentLocalErrorContextTableIndex,
8989
TypeFuncIndex, TypeFutureTableIndex, TypeStreamTableIndex, TypeTupleIndex,
@@ -2519,7 +2519,7 @@ impl Instance {
25192519
/// stack and linear memory unless the task has been cancelled.
25202520
pub(crate) fn first_poll<T: 'static, R: Send + 'static>(
25212521
self,
2522-
mut store: StoreContextMut<T>,
2522+
mut store: StoreContextMut<'_, T>,
25232523
future: impl Future<Output = Result<R>> + Send + 'static,
25242524
caller_instance: RuntimeComponentInstanceIndex,
25252525
lower: impl FnOnce(StoreContextMut<T>, R) -> Result<()> + Send + 'static,
@@ -3279,8 +3279,11 @@ impl Instance {
32793279
}
32803280
};
32813281
let memory = self.options_memory_mut(store, params.options);
3282-
let ptr =
3283-
func::validate_inbounds::<(u32, u32)>(memory, &ValRaw::u32(params.payload))?;
3282+
let ptr = func::validate_inbounds_dynamic(
3283+
&CanonicalAbiInfo::POINTER_PAIR,
3284+
memory,
3285+
&ValRaw::u32(params.payload),
3286+
)?;
32843287
memory[ptr + 0..][..4].copy_from_slice(&handle.to_le_bytes());
32853288
memory[ptr + 4..][..4].copy_from_slice(&result.to_le_bytes());
32863289
Ok(ordinal)

crates/wasmtime/src/runtime/component/concurrent_disabled.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ use crate::component::{ComponentType, Lift, Lower, Val};
44
use crate::runtime::vm::VMStore;
55
use anyhow::{Result, anyhow, bail};
66
use core::convert::Infallible;
7-
use core::future::Future;
87
use core::mem::MaybeUninit;
9-
use core::pin::pin;
10-
use core::task::{Context, Poll, Waker};
11-
use wasmtime_environ::component::{CanonicalAbiInfo, InterfaceType, RuntimeComponentInstanceIndex};
8+
use wasmtime_environ::component::{CanonicalAbiInfo, InterfaceType};
129

1310
#[derive(Default)]
1411
pub struct ConcurrentState;
@@ -26,17 +23,6 @@ pub(crate) fn check_blocking(_: &mut dyn VMStore) -> Result<()> {
2623
Ok(())
2724
}
2825

29-
pub(crate) fn poll_and_block<R: Send + Sync + 'static>(
30-
_store: &mut dyn VMStore,
31-
future: impl Future<Output = Result<R>> + Send + 'static,
32-
_caller_instance: RuntimeComponentInstanceIndex,
33-
) -> Result<R> {
34-
match pin!(future).poll(&mut Context::from_waker(Waker::noop())) {
35-
Poll::Ready(result) => result,
36-
Poll::Pending => should_have_failed_validation("async lowered import"),
37-
}
38-
}
39-
4026
pub(crate) fn lower_error_context_to_index<U>(
4127
_rep: u32,
4228
_cx: &mut LowerContext<'_, U>,

0 commit comments

Comments
 (0)