repl: Create dumy Wasm implementations of SystemTime and Duration

This commit is contained in:
Brian Carroll 2022-02-09 08:14:58 +00:00
parent 8e370a32b6
commit a0ccae2865
3 changed files with 50 additions and 1 deletions

View file

@ -43,9 +43,13 @@ use std::iter;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str::from_utf8_unchecked; use std::str::from_utf8_unchecked;
use std::sync::Arc; use std::sync::Arc;
use std::time::{Duration, SystemTime};
use std::{env, fs}; use std::{env, fs};
#[cfg(not(target_family = "wasm"))]
use std::time::{Duration, SystemTime};
#[cfg(target_family = "wasm")]
use wasm_system_time::{Duration, SystemTime};
/// Default name for the binary generated for an app, if an invalid one was specified. /// Default name for the binary generated for an app, if an invalid one was specified.
const DEFAULT_APP_OUTPUT_PATH: &str = "app"; const DEFAULT_APP_OUTPUT_PATH: &str = "app";

View file

@ -3,3 +3,6 @@
#![allow(clippy::large_enum_variant)] #![allow(clippy::large_enum_variant)]
pub mod docs; pub mod docs;
pub mod file; pub mod file;
#[cfg(target_family = "wasm")]
mod wasm_system_time;

View file

@ -0,0 +1,42 @@
#![cfg(target_family = "wasm")]
/*
For the Web REPL (repl_www), we build the compiler as a Wasm module.
SystemTime is the only thing in the compiler that would need a special implementation for this.
There is a WASI implementation for it, but we are targeting the browser, not WASI!
It's possible to write browser versions of WASI's low-level ABI but we'd rather avoid it.
Instead we use these dummy implementations, which should just disappear at compile time.
*/
#[derive(Debug, Clone, Copy)]
pub struct SystemTime;
impl SystemTime {
fn now() -> Self {
SystemTime
}
fn duration_since(&self, _: SystemTime) -> Result<Duration, String> {
Ok(Duration)
}
fn elapsed(&self) -> Result<Duration, String> {
Ok(Duration)
}
}
#[derive(Debug, Clone, Copy)]
pub struct Duration;
impl Duration {
fn checked_sub(&self, _: Duration) -> Option<Duration> {
Some(Duration)
}
}
impl Default for Duration {
fn default() -> Self {
Duration
}
}
impl std::ops::AddAssign for Duration {
fn add_assign(&mut self, _: Duration) {}
}