Skip to content

Commit

Permalink
feat: support RuntimeImportIndex lookups by string paths
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net>
  • Loading branch information
rvolosatovs committed Jan 8, 2024
1 parent 7690c50 commit d7d96d1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
26 changes: 19 additions & 7 deletions crates/wasmtime/src/component/instance.rs
Expand Up @@ -558,15 +558,27 @@ impl<T> InstancePre<T> {
}
}

pub(crate) fn resource_import_index(
&self,
path: ResourceImportIndex,
) -> Option<RuntimeImportIndex> {
*self.resource_imports.get(path)?
/// Returns [`RuntimeImportIndex`] associated with this resource.
/// `idx` is the [`ResourceImportIndex`] returned by [`Linker::resource`].
///
/// [`Linker::resource`]: crate::component::LinkerInstance::resource
pub fn resource_import_index(&self, idx: ResourceImportIndex) -> Option<RuntimeImportIndex> {
*self.resource_imports.get(idx)?
}

/// Returns [`RuntimeImportIndex`] associated with this `path` within a `root`.
pub fn path_import_index(&self, root: &str, path: &[&str]) -> Option<RuntimeImportIndex> {
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 resource_import(&self, path: ResourceImportIndex) -> Option<&RuntimeImport> {
let idx = self.resource_import_index(path)?;
pub(crate) fn runtime_import(&self, idx: RuntimeImportIndex) -> Option<&RuntimeImport> {
self.imports.get(idx)
}

Expand Down
15 changes: 7 additions & 8 deletions 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};
Expand All @@ -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};

Expand Down Expand Up @@ -428,7 +429,7 @@ where
self,
store: impl AsContextMut,
instance: &InstancePre<U>,
idx: ResourceImportIndex,
idx: RuntimeImportIndex,
) -> Result<ResourceAny> {
ResourceAny::try_from_resource(self, store, instance, idx)
}
Expand Down Expand Up @@ -542,18 +543,16 @@ struct OwnState {

impl ResourceAny {
/// Attempts to convert an imported [`Resource`] into [`ResourceAny`].
/// `idx` is the [`ResourceImportIndex`] returned by [`Linker::resource`].
///
/// [`Linker::resource`]: crate::component::LinkerInstance::resource
/// `idx` is the [`RuntimeImportIndex`] associated with this resource.
pub fn try_from_resource<T: 'static, U>(
Resource { rep, state, .. }: Resource<T>,
mut store: impl AsContextMut,
instance_pre: &InstancePre<U>,
idx: ResourceImportIndex,
idx: RuntimeImportIndex,
) -> Result<Self> {
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, ..
Expand Down
2 changes: 2 additions & 0 deletions tests/all/component_model/resources.rs
Expand Up @@ -562,6 +562,8 @@ fn dynamic_val() -> Result<()> {
.root()
.resource("t1", ResourceType::host::<MyType>(), |_, _| Ok(()))?;
let i_pre = linker.instantiate_pre(&c)?;
let idx = i_pre.resource_import_index(idx).unwrap();
assert_eq!(i_pre.path_import_index("t1", &[]), Some(idx));
let i = i_pre.instantiate(&mut store)?;

let a = i.get_func(&mut store, "a").unwrap();
Expand Down

0 comments on commit d7d96d1

Please sign in to comment.