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,5 @@
import { fnv1a64 } from "./replay.mjs";
import { exportNativeCheckpoint, restoreNativeCheckpoint } from "./native-checkpoint.mjs";
const SCHEMA = "wasm_program_runtime.v1";
const SEGMENT_BYTES = 216;
@@ -237,8 +238,15 @@ export class WasmProgramRuntime {
seek(line) { return this.invoke("sim_program_seek", [line], "program_seek"); }
restoreState(value) {
if (!value || ![SCHEMA, "sim_program.v1"].includes(value.schema) || !Number.isSafeInteger(value.current_line) || value.current_line < 0 || !this.source) throw new Error("wasm_program_restore_schema");
if (!value || ![SCHEMA, "sim_program.v1"].includes(value.schema) || !Number.isSafeInteger(value.current_line) || value.current_line < 0 || !this.source && !value.native_state) throw new Error("wasm_program_restore_schema");
if (value.source_hash && value.source_hash !== this.sourceHash) throw new Error("wasm_program_restore_source_hash");
if (value.native_state) {
if (value.execution_source_hash !== fnv1a64(this.executionSource)) throw new Error("wasm_program_restore_execution_source_hash");
restoreNativeCheckpoint(this.core, "sim_program", value.native_state, this.pointer);
this.synchronizedPosition = structuredClone(value.synchronized_position);
this.rapidVelocity = value.rapid_velocity;
return this.snapshot();
}
const subroutines = [...this.subroutines];
const loaded = this.load(this.source, this.path);
if (loaded.status !== "verified") throw new Error(loaded.code);
@@ -333,6 +341,10 @@ export class WasmProgramRuntime {
return { status: "verified", program_status: state.status, line: state.current_line, control_code: state.control_code ?? 0, has_control: state.has_control === 1, program: state, motion: this.readMotion(state) };
}
checkpoint() {
return { ...this.snapshot(), subroutine_sources: structuredClone(this.subroutines), synchronized_position: structuredClone(this.synchronizedPosition), execution_source_hash: fnv1a64(this.executionSource), native_state: exportNativeCheckpoint(this.core, "sim_program", this.pointer) };
}
snapshot() {
if (!this.available) return { schema: SCHEMA, status: "blocked", code: "wasm_program_unavailable" };
const required = call(this.core, "sim_program_canonical", "number", ["number", "number", "number"], [this.pointer, 0, 0]);