Struct wasmer_wasix::WasiEnvBuilder
source · pub struct WasiEnvBuilder {Show 22 fields
pub(super) entry_function: Option<String>,
pub(super) args: Vec<String>,
pub(super) envs: Vec<(String, Vec<u8>)>,
pub(super) preopens: Vec<PreopenedDir>,
vfs_preopens: Vec<String>,
pub(super) setup_fs_fn: Option<Box<dyn Fn(&WasiInodes, &mut WasiFs) -> Result<(), String> + Send>>,
pub(super) stdout: Option<Box<dyn VirtualFile + Send + Sync + 'static>>,
pub(super) stderr: Option<Box<dyn VirtualFile + Send + Sync + 'static>>,
pub(super) stdin: Option<Box<dyn VirtualFile + Send + Sync + 'static>>,
pub(super) fs: Option<WasiFsRoot>,
pub(super) runtime: Option<Arc<dyn Runtime + Send + Sync + 'static>>,
pub(super) current_dir: Option<PathBuf>,
pub(super) uses: Vec<BinaryPackage>,
pub(super) included_packages: HashSet<PackageId>,
pub(super) module_hash: Option<ModuleHash>,
pub(super) map_commands: HashMap<String, PathBuf>,
pub(super) capabilites: Capabilities,
pub(super) additional_imports: Imports,
pub(super) snapshot_on: Vec<SnapshotTrigger>,
pub(super) snapshot_interval: Option<Duration>,
pub(super) journals: Vec<Arc<DynJournal>>,
pub(super) attach_ctrl_c: bool,
}
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()?;
Fields§
§entry_function: Option<String>
Name of entry function. Defaults to running _start
if not specified.
args: Vec<String>
Command line arguments.
envs: Vec<(String, Vec<u8>)>
Environment variables.
preopens: Vec<PreopenedDir>
Pre-opened directories that will be accessible from WASI.
vfs_preopens: Vec<String>
Pre-opened virtual directories that will be accessible from WASI.
setup_fs_fn: Option<Box<dyn Fn(&WasiInodes, &mut WasiFs) -> Result<(), String> + Send>>
§stdout: Option<Box<dyn VirtualFile + Send + Sync + 'static>>
§stderr: Option<Box<dyn VirtualFile + Send + Sync + 'static>>
§stdin: Option<Box<dyn VirtualFile + Send + Sync + 'static>>
§fs: Option<WasiFsRoot>
§runtime: Option<Arc<dyn Runtime + Send + Sync + 'static>>
§current_dir: Option<PathBuf>
§uses: Vec<BinaryPackage>
List of webc dependencies to be injected.
included_packages: HashSet<PackageId>
§module_hash: Option<ModuleHash>
§map_commands: HashMap<String, PathBuf>
List of host commands to map into the WASI instance.
capabilites: Capabilities
§additional_imports: Imports
§snapshot_on: Vec<SnapshotTrigger>
§snapshot_interval: Option<Duration>
§journals: Vec<Arc<DynJournal>>
§attach_ctrl_c: bool
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
pub fn attach_ctrl_c(self) -> Self
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>
pub fn preopen_build<F>(self, inner: F) -> Result<Self, WasiStateCreationError>
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>
pub fn add_preopen_build<F>( &mut self, inner: F, ) -> Result<(), WasiStateCreationError>
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>)
pub fn add_journal(&mut self, journal: Arc<DynJournal>)
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 get_current_dir(&mut self) -> Option<PathBuf>
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)
pub fn with_snapshot_interval(&mut self, interval: Duration)
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§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> 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