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 Dec 21, 2023
1 parent ab6d971 commit 28cb7fd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
21 changes: 15 additions & 6 deletions crates/wasmtime/src/component/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,15 +558,24 @@ impl<T> InstancePre<T> {
}
}

pub(crate) fn resource_import_index(
&self,
path: ResourceImportIndex,
) -> Option<RuntimeImportIndex> {
/// Returns [`RuntimeImportIndex`] associated with this [`ResourceImportIndex`]
pub fn resource_import_index(&self, path: ResourceImportIndex) -> Option<RuntimeImportIndex> {
*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<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 runtime_import(&self, idx: RuntimeImportIndex) -> Option<&RuntimeImport> {
self.imports.get(idx)
}

Expand Down
13 changes: 7 additions & 6 deletions crates/wasmtime/src/component/resources.rs
Original file line number Diff line number Diff line change
@@ -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 @@ -402,7 +403,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 @@ -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<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

0 comments on commit 28cb7fd

Please sign in to comment.