Files
cnc_wams/wasm-port/runtime/opfs/linuxcnc-tool-table-bridge.js
wangdequan 3f4eb06b47 按规划继续工作
结论:补齐 LinuxCNC tooldata_common.cc 随机换刀器 load/save 验证边界,WASM/SDK/OPFS 只透传 random-toolchanger 配置;native、WASM、OPFS 和浏览器 smoke 验证已通过。
2026-06-08 08:39:26 +08:00

49 lines
1.5 KiB
JavaScript

import { loadTextFile, saveTextFile } from "./file-service.js";
import { toolTablePath } from "./path-model.js";
const DEFAULT_WASM_TOOL_TABLE_PATH = "/work/tool.tbl";
function requireToolTableSdk(interp) {
for (const method of ["writeTextFile", "readTextFile", "loadToolTable", "saveToolTable"]) {
if (typeof interp?.[method] !== "function") {
throw new Error(`interpreter SDK is missing ${method}().`);
}
}
}
function resolvePaths(machineId, options = {}) {
const opfsPath = options.opfsPath ?? toolTablePath(machineId, options.filename);
const wasmPath = options.wasmPath ?? DEFAULT_WASM_TOOL_TABLE_PATH;
return { opfsPath, wasmPath };
}
export async function loadMachineToolTableFromOpfs(interp, machineId, options = {}) {
requireToolTableSdk(interp);
const { opfsPath, wasmPath } = resolvePaths(machineId, options);
const text = await loadTextFile(opfsPath, options.storage);
interp.writeTextFile(wasmPath, text);
return {
opfsPath,
wasmPath,
result: interp.loadToolTable(wasmPath, {
randomToolChanger: options.randomToolChanger === true,
}),
};
}
export async function saveMachineToolTableToOpfs(interp, machineId, options = {}) {
requireToolTableSdk(interp);
const { opfsPath, wasmPath } = resolvePaths(machineId, options);
const result = interp.saveToolTable(wasmPath);
const savedText = interp.readTextFile(wasmPath);
await saveTextFile(opfsPath, savedText, options.storage);
return {
opfsPath,
wasmPath,
result,
savedText,
};
}