Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Move `ResourceTables` constructors to `StoreOpaque` (#12546)

Alex Crichton984996538c64
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index 8d3c2fd517..da5f36c7dd 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -57,7 +57,7 @@ use crate::component::{
use crate::fiber::{self, StoreFiber, StoreFiberYield};
use crate::prelude::*;
use crate::store::{Store, StoreId, StoreInner, StoreOpaque, StoreToken};
-use crate::vm::component::{CallContext, ComponentInstance, InstanceState, ResourceTables};
+use crate::vm::component::{CallContext, ComponentInstance, InstanceState};
use crate::vm::{AlwaysMut, SendSyncPtr, VMFuncRef, VMMemoryDefinition, VMStore};
use crate::{
AsContext, AsContextMut, FuncType, Result, StoreContext, StoreContextMut, ValRaw, ValType,
@@ -2952,13 +2952,7 @@ impl Instance {
result: Box<dyn Any + Send + Sync>,
status: Status,
) -> Result<()> {
- let (calls, host_table, _, instance) = store.component_resource_state_with_instance(self);
- ResourceTables {
- calls,
- host_table: Some(host_table),
- guest: Some(instance.instance_states()),
- }
- .exit_call()?;
+ store.component_resource_tables(Some(self)).exit_call()?;
let state = store.concurrent_state_mut();
let task = state.get_mut(guest_task)?;
diff --git a/crates/wasmtime/src/runtime/component/func.rs b/crates/wasmtime/src/runtime/component/func.rs
index 481e0de644..0ed643b5fe 100644
--- a/crates/wasmtime/src/runtime/component/func.rs
+++ b/crates/wasmtime/src/runtime/component/func.rs
@@ -5,7 +5,7 @@ use crate::component::storage::storage_as_slice;
use crate::component::types::ComponentFunc;
use crate::component::values::Val;
use crate::prelude::*;
-use crate::runtime::vm::component::{ComponentInstance, InstanceFlags, ResourceTables};
+use crate::runtime::vm::component::{ComponentInstance, InstanceFlags};
use crate::runtime::vm::{Export, VMFuncRef};
use crate::store::StoreOpaque;
use crate::{AsContext, AsContextMut, StoreContextMut, ValRaw};
@@ -713,15 +713,10 @@ impl Func {
unsafe {
call_post_return(&mut store, post_return, arg, flags)?;
- let (calls, host_table, _, instance) = store
+ store
.0
- .component_resource_state_with_instance(self.instance);
- ResourceTables {
- host_table: Some(host_table),
- calls,
- guest: Some(instance.instance_states()),
- }
- .exit_call()?;
+ .component_resource_tables(Some(self.instance))
+ .exit_call()?;
if store.0.concurrency_support() {
store.0.exit_sync_call(false)?;
diff --git a/crates/wasmtime/src/runtime/component/func/options.rs b/crates/wasmtime/src/runtime/component/func/options.rs
index aefa1471e8..61788584f4 100644
--- a/crates/wasmtime/src/runtime/component/func/options.rs
+++ b/crates/wasmtime/src/runtime/component/func/options.rs
@@ -276,18 +276,11 @@ impl<'a, T: 'static> LowerContext<'a, T> {
}
fn resource_tables(&mut self) -> HostResourceTables<'_> {
- let (calls, host_table, host_resource_data, instance) = self
+ let (tables, data) = self
.store
.0
- .component_resource_state_with_instance(self.instance);
- HostResourceTables::from_parts(
- ResourceTables {
- host_table: Some(host_table),
- calls,
- guest: Some(instance.instance_states()),
- },
- host_resource_data,
- )
+ .component_resource_tables_and_host_resource_data(Some(self.instance));
+ HostResourceTables::from_parts(tables, data)
}
/// See [`HostResourceTables::enter_call`].
@@ -469,8 +462,6 @@ impl<'a> LiftContext<'a> {
ResourceTables {
host_table: Some(self.host_table),
calls: self.calls,
- // Note that the unsafety here should be valid given the contract of
- // `LiftContext::new`.
guest: Some(self.instance.as_mut().instance_states()),
},
self.host_resource_data,
diff --git a/crates/wasmtime/src/runtime/component/instance.rs b/crates/wasmtime/src/runtime/component/instance.rs
index 3c3cdb1131..dd512ddcb7 100644
--- a/crates/wasmtime/src/runtime/component/instance.rs
+++ b/crates/wasmtime/src/runtime/component/instance.rs
@@ -9,9 +9,7 @@ use crate::component::{
use crate::instance::OwnedImports;
use crate::linker::DefinitionType;
use crate::prelude::*;
-use crate::runtime::vm::component::{
- CallContexts, ComponentInstance, ResourceTables, TypedResource, TypedResourceIndex,
-};
+use crate::runtime::vm::component::{ComponentInstance, TypedResource, TypedResourceIndex};
use crate::runtime::vm::{self, VMFuncRef};
use crate::store::{AsStoreOpaque, Asyncness, StoreOpaque};
use crate::{AsContext, AsContextMut, Engine, Module, StoreContextMut};
@@ -382,8 +380,9 @@ impl Instance {
ty: TypeResourceTableIndex,
rep: u32,
) -> Result<u32> {
- let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
- resource_tables(calls, instance).resource_new(TypedResource::Component { ty, rep })
+ store
+ .component_resource_tables(Some(self))
+ .resource_new(TypedResource::Component { ty, rep })
}
/// Implementation of the `resource.rep` intrinsic for `i32`
@@ -394,8 +393,9 @@ impl Instance {
ty: TypeResourceTableIndex,
index: u32,
) -> Result<u32> {
- let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
- resource_tables(calls, instance).resource_rep(TypedResourceIndex::Component { ty, index })
+ store
+ .component_resource_tables(Some(self))
+ .resource_rep(TypedResourceIndex::Component { ty, index })
}
/// Implementation of the `resource.drop` intrinsic.
@@ -405,8 +405,9 @@ impl Instance {
ty: TypeResourceTableIndex,
index: u32,
) -> Result<Option<u32>> {
- let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
- resource_tables(calls, instance).resource_drop(TypedResourceIndex::Component { ty, index })
+ store
+ .component_resource_tables(Some(self))
+ .resource_drop(TypedResourceIndex::Component { ty, index })
}
pub(crate) fn resource_transfer_own(
@@ -416,8 +417,7 @@ impl Instance {
src: TypeResourceTableIndex,
dst: TypeResourceTableIndex,
) -> Result<u32> {
- let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
- let mut tables = resource_tables(calls, instance);
+ let mut tables = store.component_resource_tables(Some(self));
let rep = tables.resource_lift_own(TypedResourceIndex::Component { ty: src, index })?;
tables.resource_lower_own(TypedResource::Component { ty: dst, rep })
}
@@ -430,8 +430,7 @@ impl Instance {
dst: TypeResourceTableIndex,
) -> Result<u32> {
let dst_owns_resource = self.id().get(store).resource_owned_by_own_instance(dst);
- let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
- let mut tables = resource_tables(calls, instance);
+ let mut tables = store.component_resource_tables(Some(self));
let rep = tables.resource_lift_borrow(TypedResourceIndex::Component { ty: src, index })?;
// Implement `lower_borrow`'s special case here where if a borrow's
// resource type is owned by `dst` then the destination receives the
@@ -448,13 +447,11 @@ impl Instance {
}
pub(crate) fn resource_enter_call(self, store: &mut StoreOpaque) {
- let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
- resource_tables(calls, instance).enter_call()
+ store.component_resource_tables(Some(self)).enter_call()
}
pub(crate) fn resource_exit_call(self, store: &mut StoreOpaque) -> Result<()> {
- let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
- resource_tables(calls, instance).exit_call()
+ store.component_resource_tables(Some(self)).exit_call()
}
pub(crate) fn lookup_vmdef(&self, store: &mut StoreOpaque, def: &CoreDef) -> vm::Export {
@@ -650,17 +647,6 @@ where
unsafe { instance.get_export_by_index_mut(registry, store_id, idx) }
}
-fn resource_tables<'a>(
- calls: &'a mut CallContexts,
- instance: Pin<&'a mut ComponentInstance>,
-) -> ResourceTables<'a> {
- ResourceTables {
- host_table: None,
- calls,
- guest: Some(instance.instance_states()),
- }
-}
-
/// Trait used to lookup the export of a component instance.
///
/// This trait is used as an implementation detail of [`Instance::get_func`]
diff --git a/crates/wasmtime/src/runtime/component/resources/host_tables.rs b/crates/wasmtime/src/runtime/component/resources/host_tables.rs
index 3ef7253897..a6f70e46b9 100644
--- a/crates/wasmtime/src/runtime/component/resources/host_tables.rs
+++ b/crates/wasmtime/src/runtime/component/resources/host_tables.rs
@@ -97,15 +97,8 @@ impl HostResourceIndex {
impl<'a> HostResourceTables<'a> {
pub fn new_host(store: &'a mut StoreOpaque) -> HostResourceTables<'a> {
- let (calls, host_table, host_resource_data) = store.component_resource_state();
- HostResourceTables::from_parts(
- ResourceTables {
- host_table: Some(host_table),
- calls,
- guest: None,
- },
- host_resource_data,
- )
+ let (tables, data) = store.component_resource_tables_and_host_resource_data(None);
+ HostResourceTables::from_parts(tables, data)
}
pub fn from_parts(
diff --git a/crates/wasmtime/src/runtime/store.rs b/crates/wasmtime/src/runtime/store.rs
index 699afef3b2..d680479573 100644
--- a/crates/wasmtime/src/runtime/store.rs
+++ b/crates/wasmtime/src/runtime/store.rs
@@ -2549,7 +2549,7 @@ at https://bytecodealliance.org/security.
}
#[inline]
- #[cfg(feature = "component-model")]
+ #[cfg(feature = "component-model-async")]
pub(crate) fn component_resource_state(
&mut self,
) -> (
@@ -2574,9 +2574,8 @@ at https://bytecodealliance.org/security.
self.num_component_instances += 1;
}
- #[inline]
#[cfg(feature = "component-model")]
- pub(crate) fn component_resource_state_with_instance(
+ pub(crate) fn component_resource_state_with_instance_and_concurrent_state(
&mut self,
instance: crate::component::Instance,
) -> (
@@ -2584,32 +2583,47 @@ at https://bytecodealliance.org/security.
&mut vm::component::HandleTable,
&mut crate::component::HostResourceData,
Pin<&mut vm::component::ComponentInstance>,
+ Option<&mut concurrent::ConcurrentState>,
) {
(
&mut self.component_calls,
&mut self.component_host_table,
&mut self.host_resource_data,
instance.id().from_data_get_mut(&mut self.store_data),
+ self.concurrent_state.as_mut(),
)
}
#[cfg(feature = "component-model")]
- pub(crate) fn component_resource_state_with_instance_and_concurrent_state(
+ pub(crate) fn component_resource_tables(
&mut self,
- instance: crate::component::Instance,
+ instance: Option<crate::component::Instance>,
+ ) -> vm::component::ResourceTables<'_> {
+ self.component_resource_tables_and_host_resource_data(instance)
+ .0
+ }
+
+ #[cfg(feature = "component-model")]
+ pub(crate) fn component_resource_tables_and_host_resource_data(
+ &mut self,
+ instance: Option<crate::component::Instance>,
) -> (
- &mut vm::component::CallContexts,
- &mut vm::component::HandleTable,
+ vm::component::ResourceTables<'_>,
&mut crate::component::HostResourceData,
- Pin<&mut vm::component::ComponentInstance>,
- Option<&mut concurrent::ConcurrentState>,
) {
+ let guest = instance.map(|i| {
+ i.id()
+ .from_data_get_mut(&mut self.store_data)
+ .instance_states()
+ });
+
(
- &mut self.component_calls,
- &mut self.component_host_table,
+ vm::component::ResourceTables {
+ host_table: Some(&mut self.component_host_table),
+ calls: &mut self.component_calls,
+ guest,
+ },
&mut self.host_resource_data,
- instance.id().from_data_get_mut(&mut self.store_data),
- self.concurrent_state.as_mut(),
)
}