silk.platform.wasm.host.
Random bytes, clocks, and the time zone setting from a JavaScript host, for the providers of
Random, SystemClock, MonotonicClock, and LocalZone on wasm32-unknown-unknown.
When to use#
Application code does not call it. OsRandom, OsSystemClock, OsMonotonicClock, and
OsLocalZone 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 three host functions from the import module silk, and a program imports
only the ones it reads:
| Import | Does |
|---|---|
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 |
time-zone(address, capacity) | Writes the IANA name of the zone of the host, as Intl.DateTimeFormat().resolvedOptions().timeZone gives it, at address, and returns its length in bytes, or 0 if the host has no name or it is longer than capacity |
The standard library's runner, host/wasm-runner.mjs, supplies all three. The mount of
ui-app-dom supplies random and clock.
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#
pub struct JsHostThe random bytes and clocks of a JavaScript host. A bad answer of the host is a trap.
Implementation JsHost#
impl JsHostAssociated function fillRandom#
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#
pub fn now() -> MarkReturns the reading of the monotonic clock of the host.
Associated function systemNow#
pub fn systemNow() -> InstantReturns the date and time of the wall clock of the host.
Associated function resolution#
pub fn resolution(monotonic: bool) -> u64Returns 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 timeZone#
pub fn timeZone(output: &mut [u8]) -> usizeWrites the IANA name of the time zone of the host, such as America/Sao_Paulo, at the
start of output, and returns its length. Returns 0 if the host gives no name or the
name does not fit in output.
Associated function waitUntil#
pub fn waitUntil(when: Mark) -> ()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.