Make machine configuration authoritative and restore native checkpoints exactly

This commit is contained in:
2026-09-18 09:09:41 +08:00
parent 6a69511276
commit 4a568f1f4c
94 changed files with 10882 additions and 301 deletions

View File

@@ -1,4 +1,7 @@
import { fnv1a64 } from "./replay.mjs";
import { exportNativeCheckpoint, restoreNativeCheckpoint } from "./native-checkpoint.mjs";
import { WEB_SETTINGS } from "../workspace/web-settings.mjs";
const DEFAULTS = WEB_SETTINGS.runtime.io;
const IO_SCHEMA = "wasm_xyzac_io.v1";
const HAL_SCHEMA = "wasm_xyzac_hal.v1";
@@ -44,7 +47,7 @@ function hexToBytes(value) {
}
export class WasmXyzacIo {
constructor(core, { spindleCount = 2, toolCapacity = 1001 } = {}) {
constructor(core, { spindleCount = DEFAULTS.spindle_count, toolCapacity = DEFAULTS.tool_capacity, cycleTime = DEFAULTS.cycle_time, spindleAcceleration = DEFAULTS.spindle_acceleration, spindleTolerance = DEFAULTS.spindle_tolerance } = {}) {
this.core = core;
this.available = ["sim_io_size", "sim_io_init", "sim_io_canonical", "sim_io_state_hash"].every((name) => typeof core?.[`_${name}`] === "function" || typeof core?.[name] === "function");
this.pointer = 0;
@@ -58,7 +61,7 @@ export class WasmXyzacIo {
this.pointer = core._malloc(this.ioSize);
this.posePointer = core._malloc(POSE_BYTES);
if (!this.pointer || !this.posePointer) throw new Error("wasm_xyzac_io_allocation_failed");
const result = call(core, "sim_io_init", "number", ["number", "number", "number", "number", "number", "number"], [this.pointer, spindleCount, toolCapacity, 0.01, 600, 5]);
const result = call(core, "sim_io_init", "number", ["number", "number", "number", "number", "number", "number"], [this.pointer, spindleCount, toolCapacity, cycleTime, spindleAcceleration, spindleTolerance]);
if (result !== 0) throw new Error(`wasm_xyzac_io_init_${result}`);
}
@@ -102,7 +105,7 @@ export class WasmXyzacIo {
return this.export();
}
halExport() {
halExport({ includeState = false } = {}) {
if (!this.available) return { schema: HAL_SCHEMA, source: "wasm_hal", status: "blocked", code: "wasm_hal_unavailable" };
const tick = call(this.core, "sim_hal_tick", "number", [], []);
const unsigned = {
@@ -115,10 +118,16 @@ export class WasmXyzacIo {
thread_count: call(this.core, "sim_hal_thread_count", "number", [], []),
function_count: call(this.core, "sim_hal_function_count", "number", [], []),
component_count: call(this.core, "sim_hal_component_count", "number", [], []),
...(includeState ? { native_state: exportNativeCheckpoint(this.core, "sim_hal") } : {}),
};
return { ...unsigned, state_hash: fnv1a64(unsigned) };
}
restoreHalState(value) {
if (!value?.native_state) throw new Error("wasm_hal_native_checkpoint_required");
restoreNativeCheckpoint(this.core, "sim_hal", value.native_state);
}
halValue(name, value, type = 4, param = false) {
if (!this.available) return { status: "blocked", code: "wasm_hal_unavailable" };
if (param || !HAL_WRITE_ALLOWLIST.has(name)) return { status: "error", code: "wasm_hal_write_denied", name, target: param ? "param" : "pin" };