Sha256: 90f061cb55354552a40c437525aabe20b2c2d39b3a6bb87fbf76a11be67b750e

Contents?: true

Size: 1.67 KB

Versions: 13

Compression:

Stored size: 1.67 KB

Contents

use cap_primitives::time::{Duration, Instant, SystemTime};
use wasi_common::clocks::{WasiClocks, WasiMonotonicClock, WasiSystemClock};

// There is no resolution given it's a stubbed clock
const TIMER_RESOLUTION_MS: u64 = 0;

pub fn new_clocks() -> WasiClocks {
    let instant = Instant::from_std(std::time::Instant::now());
    WasiClocks::new()
        .with_system(DeterministicSystemClock::new())
        .with_monotonic(DeterministicMonotonicClock::new(instant))
}

struct DeterministicSystemClock {
    stubbed_time: SystemTime,
}

impl DeterministicSystemClock {
    pub fn new() -> DeterministicSystemClock {
        let stubbed_time = SystemTime::from_std(std::time::UNIX_EPOCH);
        DeterministicSystemClock { stubbed_time }
    }
}

impl WasiSystemClock for DeterministicSystemClock {
    fn resolution(&self) -> Duration {
        Duration::from_millis(TIMER_RESOLUTION_MS)
    }

    fn now(&self, _precision: Duration) -> SystemTime {
        self.stubbed_time
    }
}

// `std::time::Instant` (and `cap_primitives::time::Instant`) are opaque and cannot be constructed from a particular
// datetime. This implementation results in a value of `0` being written into the memory for the Wasm module on each
// invocation of `clock_time_get`.
struct DeterministicMonotonicClock {
    instant: Instant,
}

impl DeterministicMonotonicClock {
    pub fn new(instant: Instant) -> DeterministicMonotonicClock {
        DeterministicMonotonicClock { instant }
    }
}

impl WasiMonotonicClock for DeterministicMonotonicClock {
    fn resolution(&self) -> Duration {
        Duration::from_millis(TIMER_RESOLUTION_MS)
    }

    fn now(&self, _precision: Duration) -> Instant {
        self.instant
    }
}

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/deterministic-wasi-ctx-0.1.29/src/clocks.rs
wasmtime-28.0.0 ./ext/cargo-vendor/deterministic-wasi-ctx-0.1.28/src/clocks.rs
wasmtime-27.0.0 ./ext/cargo-vendor/deterministic-wasi-ctx-0.1.27/src/clocks.rs
wasmtime-26.0.0 ./ext/cargo-vendor/deterministic-wasi-ctx-0.1.26/src/clocks.rs
wasmtime-25.0.2 ./ext/cargo-vendor/deterministic-wasi-ctx-0.1.25/src/clocks.rs
wasmtime-25.0.1 ./ext/cargo-vendor/deterministic-wasi-ctx-0.1.25/src/clocks.rs
wasmtime-25.0.0 ./ext/cargo-vendor/deterministic-wasi-ctx-0.1.25/src/clocks.rs
wasmtime-24.0.0 ./ext/cargo-vendor/deterministic-wasi-ctx-0.1.24/src/clocks.rs
wasmtime-23.0.2 ./ext/cargo-vendor/deterministic-wasi-ctx-0.1.23/src/clocks.rs
wasmtime-22.0.0 ./ext/cargo-vendor/deterministic-wasi-ctx-0.1.22/src/clocks.rs
wasmtime-21.0.1 ./ext/cargo-vendor/deterministic-wasi-ctx-0.1.22/src/clocks.rs
wasmtime-20.0.2 ./ext/cargo-vendor/deterministic-wasi-ctx-0.1.21/src/clocks.rs
wasmtime-20.0.0 ./ext/cargo-vendor/deterministic-wasi-ctx-0.1.21/src/clocks.rs