fix(core): no SystemTime on wasm32 — fixed boot seed + UTIME Forth error
CI / check (push) Waiting to run
CI / check (push) Waiting to run
This commit is contained in:
@@ -393,6 +393,11 @@ impl<R: Runtime> ForthVM<R> {
|
|||||||
substitutions: Arc::new(Mutex::new(HashMap::new())),
|
substitutions: Arc::new(Mutex::new(HashMap::new())),
|
||||||
search_order: Arc::new(Mutex::new(vec![1])),
|
search_order: Arc::new(Mutex::new(vec![1])),
|
||||||
next_wid: Arc::new(Mutex::new(2)),
|
next_wid: Arc::new(Mutex::new(2)),
|
||||||
|
// SystemTime::now() PANICS on wasm32-unknown-unknown (no time
|
||||||
|
// source), which turned VM construction into an `unreachable`
|
||||||
|
// trap in the browser. Seed from the wall clock only where one
|
||||||
|
// exists; wasm hosts start deterministic and reseed via RND-SEED.
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
rng_state: {
|
rng_state: {
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
let seed = SystemTime::now()
|
let seed = SystemTime::now()
|
||||||
@@ -404,6 +409,8 @@ impl<R: Runtime> ForthVM<R> {
|
|||||||
seed
|
seed
|
||||||
}))
|
}))
|
||||||
},
|
},
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
rng_state: Arc::new(Mutex::new(0xDEAD_BEEF_CAFE_BABE)),
|
||||||
compile_frames: Vec::new(),
|
compile_frames: Vec::new(),
|
||||||
compiling_word_addr: 0,
|
compiling_word_addr: 0,
|
||||||
};
|
};
|
||||||
@@ -5383,6 +5390,15 @@ impl<R: Runtime> ForthVM<R> {
|
|||||||
/// UTIME ( -- ud ) push microseconds since epoch as a double-cell value.
|
/// UTIME ( -- ud ) push microseconds since epoch as a double-cell value.
|
||||||
fn register_utime(&mut self) -> anyhow::Result<()> {
|
fn register_utime(&mut self) -> anyhow::Result<()> {
|
||||||
let func: HostFn = Box::new(move |ctx: &mut dyn HostAccess| {
|
let func: HostFn = Box::new(move |ctx: &mut dyn HostAccess| {
|
||||||
|
// SystemTime::now() panics on wasm32-unknown-unknown; fail as a
|
||||||
|
// catchable Forth error instead of poisoning the VM with a trap.
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
{
|
||||||
|
let _ = ctx;
|
||||||
|
Err(anyhow::anyhow!("UTIME: no time source on this platform"))
|
||||||
|
}
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
{
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
let us = SystemTime::now()
|
let us = SystemTime::now()
|
||||||
.duration_since(UNIX_EPOCH)
|
.duration_since(UNIX_EPOCH)
|
||||||
@@ -5397,6 +5413,7 @@ impl<R: Runtime> ForthVM<R> {
|
|||||||
ctx.mem_write_slice(new_sp as u32 + 4, &lo.to_le_bytes());
|
ctx.mem_write_slice(new_sp as u32 + 4, &lo.to_le_bytes());
|
||||||
ctx.set_dsp((new_sp as i32) as u32);
|
ctx.set_dsp((new_sp as i32) as u32);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
self.register_host_primitive("UTIME", false, func)?;
|
self.register_host_primitive("UTIME", false, func)?;
|
||||||
|
|||||||
Reference in New Issue
Block a user