packages/core-wasm: browser-safe CoreRuntime (synchronous calls once ready; a WebAssembly.RuntimeError poisons the instance, the call answers internal, a fresh instance comes from the kept compiled module and a glue record imported in advance; at most 3 recoveries per page; retry after a load failure), SheetSession (handle rebuilt per instance generation, never called on a dropped one; invalid_sheet without poisoning), Node source. crates/wasm: SheetHandle (sheet held in the module memory). Two builds: pkg/ shipped without trap exports (checked), pkg-proofs/ for the trap tests; BUILD_STAMP (hash of the Rust sources) refused when missing or stale by the vitest global setup instead of skipping. Failure envelope carries a numeric found version; compact JS-number output.
73 lines
2.6 KiB
Rust
73 lines
2.6 KiB
Rust
//! wasm-bindgen facade over `faunosteo-core` (review III.3): JSON strings in and out, closed
|
|
//! outcome kinds. Built by `scripts/wasm-build.sh` into `packages/core-wasm/pkg` (shipped)
|
|
//! and `packages/core-wasm/pkg-proofs` (with the trap exports, tests only); loaded lazily by
|
|
//! the application behind the sheet and import paths (`packages/core-wasm/src/runtime.ts`).
|
|
|
|
use faunosteo_core::domain::sheet::LoadedSheet;
|
|
use wasm_bindgen::prelude::{JsError, wasm_bindgen};
|
|
|
|
/// Version of the core compiled into this module.
|
|
#[wasm_bindgen]
|
|
pub fn core_version() -> String {
|
|
faunosteo_core::CORE_VERSION.to_owned()
|
|
}
|
|
|
|
/// Runs one core operation: `op` names it, `input` is its JSON text, the result is the
|
|
/// JSON text of the outcome envelope (`{"ok":true,"value":…}` or `{"ok":false,"kind":…}`).
|
|
#[wasm_bindgen]
|
|
pub fn call(op: &str, input: &str) -> String {
|
|
faunosteo_core::facade::call(op, input)
|
|
}
|
|
|
|
/// The session's anatomical sheet, held in the module's memory and built once from the
|
|
/// bundled payload, so the per-action calls carry only the observation or the report.
|
|
#[wasm_bindgen]
|
|
pub struct SheetHandle {
|
|
sheet: LoadedSheet,
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
impl SheetHandle {
|
|
/// Loads the bundled sheet payload; throws an `Error` whose message is
|
|
/// `invalid_sheet: <detail>` when the payload, the document or the map is refused.
|
|
#[wasm_bindgen(constructor)]
|
|
pub fn new(bundle: &str) -> Result<SheetHandle, JsError> {
|
|
faunosteo_core::facade::load_sheet(bundle)
|
|
.map(|sheet| SheetHandle { sheet })
|
|
.map_err(|failure| JsError::new(&format!("{}: {}", failure.kind, failure.detail)))
|
|
}
|
|
|
|
/// Runs one operation on this sheet; same envelope as `call`.
|
|
pub fn call(&self, op: &str, input: &str) -> String {
|
|
faunosteo_core::facade::call_with_sheet(&self.sheet, op, input)
|
|
}
|
|
}
|
|
|
|
/// R0 proof: on wasm32 a panic is a trap (panic = abort), reaching JavaScript as a
|
|
/// `WebAssembly.RuntimeError`; the caller re-instantiates the module.
|
|
#[cfg(feature = "proofs")]
|
|
#[wasm_bindgen]
|
|
pub fn panic_for_test() {
|
|
panic!("deliberate panic for the R0 proof");
|
|
}
|
|
|
|
/// R0 proof: exhausting the shadow stack traps instead of corrupting memory.
|
|
#[cfg(feature = "proofs")]
|
|
#[wasm_bindgen]
|
|
pub fn recurse_for_test(depth: u32) -> u32 {
|
|
recurse(depth)
|
|
}
|
|
|
|
#[cfg(feature = "proofs")]
|
|
#[inline(never)]
|
|
fn recurse(depth: u32) -> u32 {
|
|
let mut frame = [depth; 32];
|
|
if depth == 0 {
|
|
return std::hint::black_box(&frame)[0];
|
|
}
|
|
let below = recurse(depth - 1);
|
|
frame[(below as usize) % 32] ^= below;
|
|
std::hint::black_box(&frame);
|
|
below.wrapping_add(1)
|
|
}
|