Files
linuxcncWebWASM/web/workspace/hal-configuration.mjs

50 lines
2.5 KiB
JavaScript

export const HAL_CONFIGURATION_SCHEMA = "hal-configuration-preflight.v1";
const BUILTIN_THREADS = new Set(["servo-thread", "base-thread"]);
function text(value) {
return typeof value === "string" && value.length > 0 ? value : null;
}
export function parseHalLine(raw, line = 0) {
if (typeof raw !== "string") return { status: "blocked", code: "hal_line_invalid", line };
const clean = raw.split("#", 1)[0].trim();
if (!clean) return null;
const tokens = clean.split(/\s+/);
const command = tokens[0];
if (!["loadrt", "loadusr", "addf", "net", "setp", "source", "newsig", "linksp", "linkps", "linkpp"].includes(command)) return null;
if (command === "loadrt") {
const module = tokens[1] ?? "";
const namesToken = tokens.find((token) => token.startsWith("names="));
return { schema: HAL_CONFIGURATION_SCHEMA, line, command, module, instances: namesToken ? namesToken.slice(6).split(",") : [], status: module.startsWith("[") ? "blocked" : "declared", code: module.startsWith("[") ? "hal_component_variable" : null };
}
if (command === "loadusr") return { schema: HAL_CONFIGURATION_SCHEMA, line, command, status: "blocked", code: "hal_host_dynamic_loader", program: tokens[1] ?? "" };
if (command === "addf") {
const thread = tokens[2] ?? "";
return { schema: HAL_CONFIGURATION_SCHEMA, line, command, function: tokens[1] ?? null, thread, status: text(thread) && BUILTIN_THREADS.has(thread) ? "checked" : "blocked", code: text(thread) && BUILTIN_THREADS.has(thread) ? null : "hal_thread_undeclared" };
}
return { schema: HAL_CONFIGURATION_SCHEMA, line, command, tokens: tokens.slice(1), status: "checked", code: null };
}
export function preflightHalConfiguration({ lines = [], manifest = [] } = {}) {
const declared = new Set((Array.isArray(manifest) ? manifest : []).map((row) => row?.component).filter(Boolean));
const records = [];
for (let index = 0; index < lines.length; index += 1) {
const parsed = parseHalLine(lines[index], index + 1);
if (!parsed) continue;
if (parsed.command === "loadrt" && parsed.status === "declared" && !declared.has(parsed.module)) {
parsed.status = "blocked";
parsed.code = "hal_component_undeclared";
}
records.push(parsed);
}
const blocked = records.filter((record) => record.status === "blocked");
return {
schema: HAL_CONFIGURATION_SCHEMA,
status: blocked.length === 0 ? "ready" : "blocked",
records,
record_count: records.length,
blocked_count: blocked.length,
};
}