silk.platform.wasm.host.

Random bytes and clocks from a JavaScript host, for the providers of Random, SystemClock and MonotonicClock on wasm32-unknown-unknown.

When to use#

Application code does not call it. OsRandom, OsSystemClock and OsMonotonicClock use it when the program runs in a browser, Node.js, Deno or another JavaScript host, so the same provider works on every target.

Details#

The module imports two host functions from the import module silk, and only a program that reads random bytes or a clock imports them:

ImportDoes
random(address, length)Fills length bytes at address with secure random bytes, as crypto.getRandomValues does
clock(monotonic)Returns milliseconds as an f64: since 1970 when monotonic is 0, since a fixed start (performance.now()) when it is 1

The standard library's runner, host/wasm-runner.mjs, supplies both, and so does the mount of ui-app-dom.

Gotchas#

  • A host that instantiates the module itself supplies the imports the module names, or the instantiation fails.

  • A JavaScript host cannot block, so a wait reads the monotonic clock until the deadline. It holds the thread, and in a browser page it holds the page.

  • A browser rounds performance.now(), often to 5 or 100 microseconds.

Import as JsHost with import silk.platform.wasm.host { JsHost }.

Public declarations: 1.

JsHost#

silk
pub struct JsHost

The random bytes and clocks of a JavaScript host. A bad answer of the host is a trap.

Implementation JsHost#

silk
impl JsHost

Associated function fillRandom#

silk
pub fn fillRandom(output: &mut [u8]) -> ()

Fills output with secure random bytes from the host. An empty slice does not call the host.

Associated function now#

silk
pub fn now(monotonic: bool) -> Instant

Returns the time of the monotonic clock if monotonic is true, or of the wall clock, as seconds since 1970, if it is false.

Associated function resolution#

silk
pub fn resolution(monotonic: bool) -> u64

Returns the resolution of the selected clock, in nanoseconds: one millisecond for the wall clock and one microsecond for the monotonic clock, the finest a host promises.

Associated function waitUntil#

silk
pub fn waitUntil(when: Instant) -> ()

Reads the monotonic clock until it reaches when. It holds the thread while it waits.

Category: Native platform.

Profiles: wasm32-unknown-unknown, wasm64-unknown-unknown.

View the source of this page