pub struct WasiEnvBuilder { /* private fields */ }
Expand description
Builder API for configuring a WasiEnv
environment needed to run WASI modules.
Usage:
let mut state_builder = WasiEnv::builder("wasi-prog-name");
state_builder
.env("ENV_VAR", "ENV_VAL")
.arg("--verbose")
.preopen_dir("src")?
.map_dir("name_wasi_sees", "path/on/host/fs")?
.build_init()?;
Implementations§
Source§impl WasiEnvBuilder
impl WasiEnvBuilder
Sourcepub fn new(program_name: impl Into<String>) -> Self
pub fn new(program_name: impl Into<String>) -> Self
Creates an empty WasiEnvBuilder
.
Sourcepub fn env<Key, Value>(self, key: Key, value: Value) -> Self
pub fn env<Key, Value>(self, key: Key, value: Value) -> Self
Add an environment variable pair.
Both the key and value of an environment variable must not
contain a nul byte (0x0
), and the key must not contain the
=
byte (0x3d
).
Sourcepub fn attach_ctrl_c(self) -> Self
Available on crate feature ctrlc
only.
pub fn attach_ctrl_c(self) -> Self
ctrlc
only.Attaches a ctrl-c handler which will send signals to the process rather than immediately termiante it
Sourcepub fn add_env<Key, Value>(&mut self, key: Key, value: Value)
pub fn add_env<Key, Value>(&mut self, key: Key, value: Value)
Add an environment variable pair.
Both the key and value of an environment variable must not
contain a nul byte (0x0
), and the key must not contain the
=
byte (0x3d
).
Sourcepub fn envs<I, Key, Value>(self, env_pairs: I) -> Self
pub fn envs<I, Key, Value>(self, env_pairs: I) -> Self
Add multiple environment variable pairs.
Both the key and value of the environment variables must not
contain a nul byte (0x0
), and the key must not contain the
=
byte (0x3d
).
Sourcepub fn add_envs<I, Key, Value>(&mut self, env_pairs: I)
pub fn add_envs<I, Key, Value>(&mut self, env_pairs: I)
Add multiple environment variable pairs.
Both the key and value of the environment variables must not
contain a nul byte (0x0
), and the key must not contain the
=
byte (0x3d
).
Sourcepub fn get_env(&self) -> &[(String, Vec<u8>)]
pub fn get_env(&self) -> &[(String, Vec<u8>)]
Get a reference to the configured environment variables.
Sourcepub fn get_env_mut(&mut self) -> &mut Vec<(String, Vec<u8>)>
pub fn get_env_mut(&mut self) -> &mut Vec<(String, Vec<u8>)>
Get a mutable reference to the configured environment variables.
pub fn entry_function<S>(self, entry_function: S) -> Self
pub fn set_entry_function<S>(&mut self, entry_function: S)
Sourcepub fn arg<V>(self, arg: V) -> Self
pub fn arg<V>(self, arg: V) -> Self
Add an argument.
Arguments must not contain the nul (0x0) byte
Sourcepub fn add_arg<V>(&mut self, arg: V)
pub fn add_arg<V>(&mut self, arg: V)
Add an argument.
Arguments must not contain the nul (0x0) byte.
Sourcepub fn args<I, Arg>(self, args: I) -> Self
pub fn args<I, Arg>(self, args: I) -> Self
Add multiple arguments.
Arguments must not contain the nul (0x0) byte
Sourcepub fn add_args<I, Arg>(&mut self, args: I)
pub fn add_args<I, Arg>(&mut self, args: I)
Add multiple arguments.
Arguments must not contain the nul (0x0) byte
Sourcepub fn get_args_mut(&mut self) -> &mut Vec<String>
pub fn get_args_mut(&mut self) -> &mut Vec<String>
Get a mutable reference to the configured arguments.
Sourcepub fn use_webc(self, pkg: BinaryPackage) -> Self
pub fn use_webc(self, pkg: BinaryPackage) -> Self
Adds a container this module inherits from.
This will make all of the container’s files and commands available to the resulting WASI instance.
Sourcepub fn set_module_hash(&mut self, hash: ModuleHash) -> &mut Self
pub fn set_module_hash(&mut self, hash: ModuleHash) -> &mut Self
Sets the module hash for the running process. This ensures that the journal can restore the records for the right module. If no module hash is supplied then the process will start with a random module hash.
Sourcepub fn add_webc(&mut self, pkg: BinaryPackage) -> &mut Self
pub fn add_webc(&mut self, pkg: BinaryPackage) -> &mut Self
Adds a container this module inherits from.
This will make all of the container’s files and commands available to the resulting WASI instance.
Sourcepub fn include_package(&mut self, pkg_id: PackageId) -> &mut Self
pub fn include_package(&mut self, pkg_id: PackageId) -> &mut Self
Adds a package that is already included in the WasiEnvBuilder
filesystem.
These packages will not be merged to the final filesystem since they are already included.
Sourcepub fn include_packages(
&mut self,
pkg_ids: impl IntoIterator<Item = PackageId>,
) -> &mut Self
pub fn include_packages( &mut self, pkg_ids: impl IntoIterator<Item = PackageId>, ) -> &mut Self
Adds packages that is already included in the WasiEnvBuilder
filesystem.
These packages will not be merged to the final filesystem since they are already included.
Sourcepub fn uses<I>(self, uses: I) -> Selfwhere
I: IntoIterator<Item = BinaryPackage>,
pub fn uses<I>(self, uses: I) -> Selfwhere
I: IntoIterator<Item = BinaryPackage>,
Adds a list of other containers this module inherits from.
This will make all of the container’s files and commands available to the resulting WASI instance.
Sourcepub fn map_command<Name, Target>(self, name: Name, target: Target) -> Self
pub fn map_command<Name, Target>(self, name: Name, target: Target) -> Self
Map an atom to a local binary
Sourcepub fn add_mapped_command<Name, Target>(&mut self, name: Name, target: Target)
pub fn add_mapped_command<Name, Target>(&mut self, name: Name, target: Target)
Map an atom to a local binary
Sourcepub fn map_commands<I, Name, Target>(self, map_commands: I) -> Self
pub fn map_commands<I, Name, Target>(self, map_commands: I) -> Self
Maps a series of atoms to the local binaries
Sourcepub fn add_mapped_commands<I, Name, Target>(&mut self, map_commands: I)
pub fn add_mapped_commands<I, Name, Target>(&mut self, map_commands: I)
Maps a series of atoms to local binaries.
Sourcepub fn preopen_dir<P>(self, po_dir: P) -> Result<Self, WasiStateCreationError>
pub fn preopen_dir<P>(self, po_dir: P) -> Result<Self, WasiStateCreationError>
Preopen a directory
This opens the given directory at the virtual root, /
, and allows
the WASI module to read and write to the given directory.
Sourcepub fn add_preopen_dir<P>(
&mut self,
po_dir: P,
) -> Result<(), WasiStateCreationError>
pub fn add_preopen_dir<P>( &mut self, po_dir: P, ) -> Result<(), WasiStateCreationError>
Adds a preopen a directory
This opens the given directory at the virtual root, /
, and allows
the WASI module to read and write to the given directory.
Sourcepub fn preopen_dirs<I, P>(self, dirs: I) -> Result<Self, WasiStateCreationError>
pub fn preopen_dirs<I, P>(self, dirs: I) -> Result<Self, WasiStateCreationError>
Preopen multiple directories.
This opens the given directories at the virtual root, /
, and allows
the WASI module to read and write to the given directory.
Sourcepub fn preopen_build<F>(self, inner: F) -> Result<Self, WasiStateCreationError>where
F: Fn(&mut PreopenDirBuilder) -> &mut PreopenDirBuilder,
pub fn preopen_build<F>(self, inner: F) -> Result<Self, WasiStateCreationError>where
F: Fn(&mut PreopenDirBuilder) -> &mut PreopenDirBuilder,
Preopen a directory and configure it.
Usage:
WasiEnv::builder("program_name")
.preopen_build(|p| p.directory("src").read(true).write(true).create(true))?
.preopen_build(|p| p.directory(".").alias("dot").read(true))?
.build_init()?;
Sourcepub fn add_preopen_build<F>(
&mut self,
inner: F,
) -> Result<(), WasiStateCreationError>where
F: Fn(&mut PreopenDirBuilder) -> &mut PreopenDirBuilder,
pub fn add_preopen_build<F>(
&mut self,
inner: F,
) -> Result<(), WasiStateCreationError>where
F: Fn(&mut PreopenDirBuilder) -> &mut PreopenDirBuilder,
Preopen a directory and configure it.
Usage:
WasiEnv::builder("program_name")
.preopen_build(|p| p.directory("src").read(true).write(true).create(true))?
.preopen_build(|p| p.directory(".").alias("dot").read(true))?
.build_init()?;
Sourcepub fn preopen_vfs_dirs<I>(
&mut self,
po_dirs: I,
) -> Result<&mut Self, WasiStateCreationError>where
I: IntoIterator<Item = String>,
pub fn preopen_vfs_dirs<I>(
&mut self,
po_dirs: I,
) -> Result<&mut Self, WasiStateCreationError>where
I: IntoIterator<Item = String>,
Preopen the given directories from the Virtual FS.
Sourcepub fn map_dir<P>(
self,
alias: &str,
po_dir: P,
) -> Result<Self, WasiStateCreationError>
pub fn map_dir<P>( self, alias: &str, po_dir: P, ) -> Result<Self, WasiStateCreationError>
Preopen a directory with a different name exposed to the WASI.
Sourcepub fn add_map_dir<P>(
&mut self,
alias: &str,
po_dir: P,
) -> Result<(), WasiStateCreationError>
pub fn add_map_dir<P>( &mut self, alias: &str, po_dir: P, ) -> Result<(), WasiStateCreationError>
Preopen a directory with a different name exposed to the WASI.
Sourcepub fn map_dirs<I, P>(
self,
mapped_dirs: I,
) -> Result<Self, WasiStateCreationError>
pub fn map_dirs<I, P>( self, mapped_dirs: I, ) -> Result<Self, WasiStateCreationError>
Preopen directorys with a different names exposed to the WASI.
Sourcepub fn add_journal(&mut self, journal: Arc<DynJournal>)
Available on crate feature journal
only.
pub fn add_journal(&mut self, journal: Arc<DynJournal>)
journal
only.Specifies one or more journal files that Wasmer will use to restore the state of the WASM process.
The state of the WASM process and its sandbox will be reapplied use the journals in the order that you specify here.
The last journal file specified will be created if it does not exist and opened for read and write. New journal events will be written to this file
pub fn set_current_dir(&mut self, dir: impl Into<PathBuf>)
pub fn current_dir(self, dir: impl Into<PathBuf>) -> Self
Sourcepub fn stdout(
self,
new_file: Box<dyn VirtualFile + Send + Sync + 'static>,
) -> Self
pub fn stdout( self, new_file: Box<dyn VirtualFile + Send + Sync + 'static>, ) -> Self
Overwrite the default WASI stdout
, if you want to hold on to the
original stdout
use WasiFs::swap_file
after building.
Sourcepub fn set_stdout(
&mut self,
new_file: Box<dyn VirtualFile + Send + Sync + 'static>,
)
pub fn set_stdout( &mut self, new_file: Box<dyn VirtualFile + Send + Sync + 'static>, )
Overwrite the default WASI stdout
, if you want to hold on to the
original stdout
use WasiFs::swap_file
after building.
Sourcepub fn stderr(
self,
new_file: Box<dyn VirtualFile + Send + Sync + 'static>,
) -> Self
pub fn stderr( self, new_file: Box<dyn VirtualFile + Send + Sync + 'static>, ) -> Self
Overwrite the default WASI stderr
, if you want to hold on to the
original stderr
use WasiFs::swap_file
after building.
Sourcepub fn set_stderr(
&mut self,
new_file: Box<dyn VirtualFile + Send + Sync + 'static>,
)
pub fn set_stderr( &mut self, new_file: Box<dyn VirtualFile + Send + Sync + 'static>, )
Overwrite the default WASI stderr
, if you want to hold on to the
original stderr
use WasiFs::swap_file
after building.
Sourcepub fn stdin(
self,
new_file: Box<dyn VirtualFile + Send + Sync + 'static>,
) -> Self
pub fn stdin( self, new_file: Box<dyn VirtualFile + Send + Sync + 'static>, ) -> Self
Overwrite the default WASI stdin
, if you want to hold on to the
original stdin
use WasiFs::swap_file
after building.
Sourcepub fn set_stdin(
&mut self,
new_file: Box<dyn VirtualFile + Send + Sync + 'static>,
)
pub fn set_stdin( &mut self, new_file: Box<dyn VirtualFile + Send + Sync + 'static>, )
Overwrite the default WASI stdin
, if you want to hold on to the
original stdin
use WasiFs::swap_file
after building.
Sourcepub fn fs(self, fs: Box<dyn FileSystem + Send + Sync>) -> Self
pub fn fs(self, fs: Box<dyn FileSystem + Send + Sync>) -> Self
Sets the FileSystem to be used with this WASI instance.
This is usually used in case a custom virtual_fs::FileSystem
is needed.
pub fn set_fs(&mut self, fs: Box<dyn FileSystem + Send + Sync>)
Sourcepub fn sandbox_fs(self, fs: TmpFileSystem) -> Self
pub fn sandbox_fs(self, fs: TmpFileSystem) -> Self
Sets a new sandbox FileSystem to be used with this WASI instance.
This is usually used in case a custom virtual_fs::FileSystem
is needed.
Sourcepub fn setup_fs(
self,
setup_fs_fn: Box<dyn Fn(&WasiInodes, &mut WasiFs) -> Result<(), String> + Send>,
) -> Self
pub fn setup_fs( self, setup_fs_fn: Box<dyn Fn(&WasiInodes, &mut WasiFs) -> Result<(), String> + Send>, ) -> Self
Configure the WASI filesystem before running.
Sourcepub fn runtime(self, runtime: Arc<dyn Runtime + Send + Sync>) -> Self
pub fn runtime(self, runtime: Arc<dyn Runtime + Send + Sync>) -> Self
Sets the WASI runtime implementation and overrides the default implementation
pub fn set_runtime(&mut self, runtime: Arc<dyn Runtime + Send + Sync>)
pub fn capabilities(self, capabilities: Capabilities) -> Self
pub fn capabilities_mut(&mut self) -> &mut Capabilities
pub fn set_capabilities(&mut self, capabilities: Capabilities)
pub fn add_snapshot_trigger(&mut self, on: SnapshotTrigger)
journal
only.pub fn with_snapshot_interval(&mut self, interval: Duration)
journal
only.Sourcepub fn import(
self,
namespace: impl Into<String>,
name: impl Into<String>,
value: impl Into<Extern>,
) -> Self
pub fn import( self, namespace: impl Into<String>, name: impl Into<String>, value: impl Into<Extern>, ) -> Self
Add an item to the list of importable items provided to the instance.
Sourcepub fn add_import(
&mut self,
namespace: impl Into<String>,
name: impl Into<String>,
value: impl Into<Extern>,
)
pub fn add_import( &mut self, namespace: impl Into<String>, name: impl Into<String>, value: impl Into<Extern>, )
Add an item to the list of importable items provided to the instance.
pub fn add_imports<I, S1, S2, E>(&mut self, imports: I)
pub fn imports<I, S1, S2, E>(self, imports: I) -> Self
Sourcepub fn build_init(self) -> Result<WasiEnvInit, WasiStateCreationError>
pub fn build_init(self) -> Result<WasiEnvInit, WasiStateCreationError>
Consumes the WasiEnvBuilder
and produces a WasiEnvInit
, which
can be used to construct a new WasiEnv
.
Returns the error from WasiFs::new
if there’s an error
NOTE: You should prefer to not work directly with WasiEnvInit
.
Use WasiEnvBuilder::run
or WasiEnvBuilder::run_with_store
instead
to ensure proper invokation of WASI modules.
pub fn build(self) -> Result<WasiEnv, WasiRuntimeError>
Sourcepub fn instantiate(
self,
module: Module,
store: &mut impl AsStoreMut,
) -> Result<(Instance, WasiFunctionEnv), WasiRuntimeError>
pub fn instantiate( self, module: Module, store: &mut impl AsStoreMut, ) -> Result<(Instance, WasiFunctionEnv), WasiRuntimeError>
Consumes the WasiEnvBuilder
and produces a WasiEnvInit
, which
can be used to construct a new WasiEnv
.
Returns the error from WasiFs::new
if there’s an error
pub fn instantiate_ext( self, module: Module, module_hash: ModuleHash, store: &mut impl AsStoreMut, ) -> Result<(Instance, WasiFunctionEnv), WasiRuntimeError>
pub fn run(self, module: Module) -> Result<(), WasiRuntimeError>
pub fn run_ext( self, module: Module, module_hash: ModuleHash, ) -> Result<(), WasiRuntimeError>
pub fn run_with_store( self, module: Module, store: &mut Store, ) -> Result<(), WasiRuntimeError>
pub fn run_with_store_ext( self, module: Module, module_hash: ModuleHash, store: &mut Store, ) -> Result<(), WasiRuntimeError>
Sourcepub fn run_with_store_async(
self,
module: Module,
module_hash: ModuleHash,
store: Store,
) -> Result<(), WasiRuntimeError>
pub fn run_with_store_async( self, module: Module, module_hash: ModuleHash, store: Store, ) -> Result<(), WasiRuntimeError>
Start the WASI executable with async threads enabled.
Trait Implementations§
Source§impl Debug for WasiEnvBuilder
impl Debug for WasiEnvBuilder
Source§impl Default for WasiEnvBuilder
impl Default for WasiEnvBuilder
Source§fn default() -> WasiEnvBuilder
fn default() -> WasiEnvBuilder
Auto Trait Implementations§
impl Freeze for WasiEnvBuilder
impl !RefUnwindSafe for WasiEnvBuilder
impl Send for WasiEnvBuilder
impl !Sync for WasiEnvBuilder
impl Unpin for WasiEnvBuilder
impl !UnwindSafe for WasiEnvBuilder
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more