diff --git a/crates/wasmtime/src/component/instance.rs b/crates/wasmtime/src/component/instance.rs index 9bf145b12de2..46801ea33b82 100644 --- a/crates/wasmtime/src/component/instance.rs +++ b/crates/wasmtime/src/component/instance.rs @@ -558,15 +558,24 @@ impl InstancePre { } } - pub(crate) fn resource_import_index( - &self, - path: ResourceImportIndex, - ) -> Option { + /// Returns [`RuntimeImportIndex`] associated with this [`ResourceImportIndex`] + pub fn resource_import_index(&self, path: ResourceImportIndex) -> Option { *self.resource_imports.get(*path)? } - pub(crate) fn resource_import(&self, path: ResourceImportIndex) -> Option<&RuntimeImport> { - let idx = self.resource_import_index(path)?; + /// Returns [`RuntimeImportIndex`] associated with this `path` within a `root`. + pub fn path_import_index(&self, root: &str, path: &[&str]) -> Option { + let env_component = self.component().env_component(); + env_component + .imports + .iter() + .find_map(|(idx, (import, import_path))| { + let (root_name, _) = env_component.import_types.get(*import)?; + (root_name == root && import_path == path).then_some(idx) + }) + } + + pub(crate) fn runtime_import(&self, idx: RuntimeImportIndex) -> Option<&RuntimeImport> { self.imports.get(idx) } diff --git a/crates/wasmtime/src/component/resources.rs b/crates/wasmtime/src/component/resources.rs index 400583d3525d..175ba656e289 100644 --- a/crates/wasmtime/src/component/resources.rs +++ b/crates/wasmtime/src/component/resources.rs @@ -1,6 +1,5 @@ use crate::component::func::{bad_type_info, desc, LiftContext, LowerContext}; use crate::component::instance::RuntimeImport; -use crate::component::linker::ResourceImportIndex; use crate::component::matching::InstanceType; use crate::component::{ComponentType, InstancePre, Lift, Lower}; use crate::store::{StoreId, StoreOpaque}; @@ -11,7 +10,9 @@ use std::fmt; use std::marker; use std::mem::MaybeUninit; use std::sync::atomic::{AtomicU32, Ordering::Relaxed}; -use wasmtime_environ::component::{CanonicalAbiInfo, DefinedResourceIndex, InterfaceType}; +use wasmtime_environ::component::{ + CanonicalAbiInfo, DefinedResourceIndex, InterfaceType, RuntimeImportIndex, +}; use wasmtime_runtime::component::{ComponentInstance, InstanceFlags, ResourceTables}; use wasmtime_runtime::{SendSyncPtr, VMFuncRef, ValRaw}; @@ -402,7 +403,7 @@ where self, store: impl AsContextMut, instance: &InstancePre, - idx: ResourceImportIndex, + idx: RuntimeImportIndex, ) -> Result { ResourceAny::try_from_resource(self, store, instance, idx) } @@ -516,16 +517,16 @@ struct OwnState { impl ResourceAny { /// Attempts to convert an imported [`Resource`] into [`ResourceAny`]. - /// `idx` is the [`ResourceImportIndex`] returned by [`Linker::resource`]. + /// `idx` is the [`RuntimeImportIndex`] associated with this resource. pub fn try_from_resource( Resource { rep, state, .. }: Resource, mut store: impl AsContextMut, instance_pre: &InstancePre, - idx: ResourceImportIndex, + idx: RuntimeImportIndex, ) -> Result { let store = store.as_context_mut(); let import = instance_pre - .resource_import(idx) + .runtime_import(idx) .context("import not found")?; let RuntimeImport::Resource { ty, dtor_funcref, ..