wasmer_wasix/
capabilities.rs

1use std::time::Duration;
2
3use crate::http::HttpClientCapabilityV1;
4
5/// Defines capabilities for a Wasi environment.
6#[derive(Clone, Debug, PartialEq, Eq, Hash)]
7pub struct Capabilities {
8    pub insecure_allow_all: bool,
9    pub http_client: HttpClientCapabilityV1,
10    pub threading: CapabilityThreadingV1,
11}
12
13impl Capabilities {
14    pub fn new() -> Self {
15        Self {
16            insecure_allow_all: false,
17            http_client: Default::default(),
18            threading: Default::default(),
19        }
20    }
21
22    /// Merges another [`Capabilities`] object into this one, overwriting fields
23    /// if necessary.
24    pub fn update(&mut self, other: Capabilities) {
25        let Capabilities {
26            insecure_allow_all,
27            http_client,
28            threading,
29        } = other;
30        self.insecure_allow_all |= insecure_allow_all;
31        self.http_client.update(http_client);
32        self.threading.update(threading);
33    }
34}
35
36impl Default for Capabilities {
37    fn default() -> Self {
38        Self::new()
39    }
40}
41
42/// Defines threading related permissions.
43#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
44pub struct CapabilityThreadingV1 {
45    /// Maximum number of threads that can be spawned.
46    ///
47    /// [`None`] means no limit.
48    pub max_threads: Option<usize>,
49
50    /// Flag that indicates if asynchronous threading is disabled
51    /// (default = false)
52    pub enable_asynchronous_threading: bool,
53
54    /// Enables an exponential backoff of the process CPU usage when there
55    /// are no active run tokens (when set holds the maximum amount of
56    /// time that it will pause the CPU)
57    /// (default = off)
58    pub enable_exponential_cpu_backoff: Option<Duration>,
59
60    /// Switches to a blocking sleep implementation instead
61    /// of the asynchronous runtime based implementation
62    pub enable_blocking_sleep: bool,
63}
64
65impl CapabilityThreadingV1 {
66    pub fn update(&mut self, other: CapabilityThreadingV1) {
67        let CapabilityThreadingV1 {
68            max_threads,
69            enable_asynchronous_threading,
70            enable_exponential_cpu_backoff,
71            enable_blocking_sleep,
72        } = other;
73        self.enable_asynchronous_threading |= enable_asynchronous_threading;
74        if let Some(val) = enable_exponential_cpu_backoff {
75            self.enable_exponential_cpu_backoff = Some(val);
76        }
77        self.max_threads = max_threads.or(self.max_threads);
78        self.enable_blocking_sleep |= enable_blocking_sleep;
79    }
80}