Skip to content

Commit

Permalink
feat: Added the ability to provide additional host functions to `Wasi…
Browse files Browse the repository at this point in the history
…Runner`
  • Loading branch information
Michael-F-Bryan committed Jan 16, 2024
1 parent cd1828a commit 84b2374
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/wasix/src/runners/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,30 @@ impl WasiRunner {
self
}

/// Add an item to the list of importable items provided to the instance.
pub fn with_import(
&mut self,
namespace: impl Into<String>,
name: impl Into<String>,
value: impl Into<Extern>,
) -> &mut Self {
self.with_imports([((namespace, name), value)])
}

pub fn with_imports<I, S1, S2, E>(&mut self, imports: I) -> &mut Self
where
I: IntoIterator<Item = ((S1, S2), E)>,
S1: Into<String>,
S2: Into<String>,
E: Into<Extern>,
{
let imports = imports
.into_iter()
.map(|((ns, n), e)| ((ns.into(), n.into()), e.into()));
self.wasi.additional_imports.extend(imports);
self
}

#[tracing::instrument(level = "debug", skip_all)]
pub(crate) fn prepare_webc_env(
&self,
Expand Down
4 changes: 4 additions & 0 deletions lib/wasix/src/runners/wasi_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use anyhow::{Context, Error};
use derivative::Derivative;
use futures::future::BoxFuture;
use virtual_fs::{FileSystem, FsError, OverlayFileSystem, RootFileSystemBuilder, TmpFileSystem};
use wasmer::Imports;
use webc::metadata::annotations::Wasi as WasiAnnotation;

use crate::{
Expand Down Expand Up @@ -40,6 +41,7 @@ pub(crate) struct CommonWasiOptions {
pub(crate) snapshot_on: Vec<SnapshotTrigger>,
pub(crate) snapshot_interval: Option<std::time::Duration>,
pub(crate) current_dir: Option<PathBuf>,
pub(crate) additional_imports: Imports,
}

impl CommonWasiOptions {
Expand Down Expand Up @@ -77,6 +79,8 @@ impl CommonWasiOptions {

*builder.capabilities_mut() = self.capabilities.clone();

builder.add_imports(&self.additional_imports);

Ok(())
}

Expand Down

0 comments on commit 84b2374

Please sign in to comment.