async_wasi/snapshots/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
pub mod common;
pub mod env;
pub mod preview_1;

use common::error::Errno;

use self::env::{vfs::WasiFileSys, VFS};

#[derive(Debug)]
pub struct WasiCtx {
    pub args: Vec<String>,
    envs: Vec<String>,
    pub(crate) vfs: VFS,
    pub exit_code: u32,
}
impl Default for WasiCtx {
    fn default() -> Self {
        Self::new()
    }
}
impl WasiCtx {
    pub fn new() -> Self {
        WasiCtx {
            args: vec![],
            envs: vec![],
            vfs: VFS::new(),
            exit_code: 0,
        }
    }

    pub fn create_with_vfs(vfs: VFS) -> Self {
        Self {
            args: vec![],
            envs: vec![],
            vfs,
            exit_code: 0,
        }
    }

    pub fn mount_file_sys(
        &mut self,
        guest_path: &str,
        file_sys: Box<dyn WasiFileSys<Index = usize> + Send + Sync>,
    ) {
        self.vfs.mount_file_sys(guest_path, file_sys)
    }

    pub fn push_arg(&mut self, arg: String) {
        self.args.push(arg);
    }

    pub fn push_args(&mut self, args: Vec<String>) {
        self.args.extend(args);
    }

    /// The format of the `env` argument should be "KEY=VALUE"
    pub fn push_env(&mut self, env: String) {
        self.envs.push(env);
    }

    pub fn push_envs(&mut self, envs: Vec<String>) {
        self.envs.extend(envs);
    }
}

// unsafe impl Send for WasiCtx {}
// unsafe impl Sync for WasiCtx {}