Support long G-code preview and virtualized execution display
This commit is contained in:
66
web/worker/test_xyzac_3d_chips.mjs
Normal file
66
web/worker/test_xyzac_3d_chips.mjs
Normal file
@@ -0,0 +1,66 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
import { XyzacSession, CONTEXT_ID } from "./xyzac-session.mjs";
|
||||
import { fnv1a64 } from "./replay.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const PROGRAM_PATH = "linuxcnc/nc_files/3D_Chips.ngc";
|
||||
const fetchImpl = async (url) => {
|
||||
try {
|
||||
const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url));
|
||||
return { ok: true, text: async () => body.toString("utf8") };
|
||||
} catch {
|
||||
return { ok: false, text: async () => "" };
|
||||
}
|
||||
};
|
||||
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
assert.equal(bundle.status, "ready");
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
const session = new XyzacSession(core, { sessionId: "xyzac-3d-chips" });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
||||
let seq = 0;
|
||||
const send = (kind, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id: `chips-${seq}`, payload });
|
||||
|
||||
send("HELLO");
|
||||
send("SESSION_ATTACH");
|
||||
assert.equal(send("LOAD_WORKSPACE", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.status, "verified");
|
||||
const source = await readFile(new URL(`../../${PROGRAM_PATH}`, import.meta.url), "utf8");
|
||||
const loaded = send("PROGRAM_LOAD", { source, path: PROGRAM_PATH });
|
||||
assert.equal(loaded.kind, "COMMAND_RESULT");
|
||||
assert.equal(loaded.payload.status, "verified");
|
||||
assert.equal(loaded.payload.program.path, PROGRAM_PATH);
|
||||
assert.equal(loaded.payload.program.source_length, 200509);
|
||||
assert.equal(loaded.payload.program.line_count, 4711);
|
||||
assert.equal(loaded.payload.program.source_hash, fnv1a64(source));
|
||||
assert.equal(loaded.payload.preview.status, "verified");
|
||||
assert.equal(loaded.payload.preview.source, "linuxcnc-wasm-sim_program");
|
||||
assert.equal(loaded.payload.preview.line_count, 4711);
|
||||
assert.equal(loaded.payload.preview.segments.length, 4684);
|
||||
|
||||
assert.equal(send("POWER", { active: true }).kind, "COMMAND_RESULT");
|
||||
assert.equal(send("HOME", { all: true }).kind, "COMMAND_RESULT");
|
||||
const run = send("PROGRAM_RUN");
|
||||
assert.equal(run.kind, "COMMAND_RESULT");
|
||||
const stepped = send("SIM_STEP", { cycles: 16, response: "motion_delta" });
|
||||
assert.equal(stepped.kind, "COMMAND_RESULT");
|
||||
assert.equal(stepped.payload.program.program_status, "running");
|
||||
assert.ok(stepped.payload.program.current_line > 0);
|
||||
assert.ok(stepped.payload.execution_trace.length > 0);
|
||||
assert.ok(stepped.payload.program.execution_line_text.length > 0);
|
||||
assert.equal(stepped.payload.motion_authority, "wasm");
|
||||
|
||||
console.log(JSON.stringify({
|
||||
schema: "xyzac-3d-chips-program.v1",
|
||||
status: "verified",
|
||||
program_path: PROGRAM_PATH,
|
||||
source_length: source.length,
|
||||
source_hash: loaded.payload.program.source_hash,
|
||||
line_count: loaded.payload.program.line_count,
|
||||
preview_segments: loaded.payload.preview.segments.length,
|
||||
execution_line: stepped.payload.program.current_line,
|
||||
execution_trace: stepped.payload.execution_trace.length,
|
||||
motion_authority: stepped.payload.motion_authority,
|
||||
}));
|
||||
@@ -145,7 +145,7 @@ export class WasmProgramRuntime {
|
||||
if (step.status !== "verified") return { status: "error", code: step.code, line: temporary.snapshot().current_line, segments };
|
||||
if (step.has_control && [428, 429, 430].includes(step.control_code)) kinematicsMode = { 428: 1, 429: 0, 430: 2 }[step.control_code];
|
||||
if (step.motion) segments.push({ id: segments.length + 1, kinematics_mode: kinematicsMode, ...step.motion });
|
||||
if ([4, 5, 6].includes(temporary.snapshot().status)) break;
|
||||
if ([4, 5, 6].includes(step.program.status)) break;
|
||||
}
|
||||
return { status: "verified", schema: "wasm-canonical-preview.v1", source: "linuxcnc-wasm-sim_program", path: this.path, source_hash: fnv1a64(this.source), line_count: this.snapshot().line_count, segments, preview_hash: fnv1a64(segments) };
|
||||
} finally { temporary.destroy(); }
|
||||
@@ -220,12 +220,12 @@ export class WasmProgramRuntime {
|
||||
try {
|
||||
const result = call(this.core, "sim_program_execute_block", "number", ["number", "number", "number"], [this.pointer, pointer, bytes.byteLength]);
|
||||
if (result !== 0) return programError("program_execute", result);
|
||||
return { status: "verified", program: this.snapshot(), motion: this.readMotion() };
|
||||
const program = this.snapshot();
|
||||
return { status: "verified", program, motion: this.readMotion(program) };
|
||||
} finally { this.core._free(pointer); }
|
||||
}
|
||||
|
||||
readMotion() {
|
||||
const state = this.snapshot();
|
||||
readMotion(state = this.snapshot()) {
|
||||
if (!state.has_motion) return null;
|
||||
const result = call(this.core, "sim_program_read_motion", "number", ["number", "number"], [this.pointer, this.motionPointer]);
|
||||
if (result !== 0) return null;
|
||||
@@ -254,7 +254,7 @@ export class WasmProgramRuntime {
|
||||
const result = this.invoke("sim_program_step", [], "program_step");
|
||||
if (result.status !== "verified") return result;
|
||||
const state = this.snapshot();
|
||||
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() };
|
||||
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) };
|
||||
}
|
||||
|
||||
snapshot() {
|
||||
|
||||
Reference in New Issue
Block a user