Skip to main content

Host

Trait Host 

Source
pub trait Host {
    // Provided methods
    fn write(&mut self, stream: Stream, text: &[u8]) { ... }
    fn now_ms(&mut self) -> f64 { ... }
    fn local_tz_offset_minutes(&mut self, _utc_ms: f64) -> i32 { ... }
    fn random_seed(&mut self) -> u64 { ... }
    fn promise_rejection(&mut self, event: RejectionEvent, promise: Value) { ... }
    fn read_file(&mut self, path: &str) -> Option<String> { ... }
    fn interrupt(&mut self) -> bool { ... }
}
Expand description

The embedder’s services. Every method has a default suitable for a standalone runtime: the process’s streams, the system clock and UTC.

Provided Methods§

Source

fn write(&mut self, stream: Stream, text: &[u8])

Writes host output (print): UTF-8, with a lone surrogate of the JavaScript string encoded as is (WTF-8), as QuickJS’s JS_ToCStringLen writes it. The default writes to the process’s streams.

Source

fn now_ms(&mut self) -> f64

Milliseconds since the Unix epoch (Date.now()).

Source

fn local_tz_offset_minutes(&mut self, _utc_ms: f64) -> i32

UTC minus local time, in minutes, at utc_ms (Date.prototype.getTimezoneOffset’s sign). The default is UTC.

Source

fn random_seed(&mut self) -> u64

The initial state of Math.random. The default derives it from the system clock, as QuickJS seeds from host time.

Source

fn promise_rejection(&mut self, event: RejectionEvent, promise: Value)

HostPromiseRejectionTracker(promise, operation). The promise is a value of the runtime; while rejection tracking is on (Runtime::set_track_rejections) the runtime also keeps every unhandled rejected promise alive in Runtime::unhandled_rejections. The default ignores the event.

Source

fn read_file(&mut self, path: &str) -> Option<String>

The text of the file at path, for the CLI’s load and read (qjs --std). The default reads the process’s file system.

Source

fn interrupt(&mut self) -> bool

Polled about every thousand loop back-edges and calls while a script runs: true stops the script with an uncatchable InternalError: interrupted. The default never interrupts.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§