结论:L4-TOOL-DB 已接入浏览器/WASM/OPFS 仿真证明链,并纳入 release/host gate;axis/db_demo/base.ngc 仍保持 L4-TOOL-DB locked,promotion_allowed=0,sim-config baseline 保持 28/28/131/0。
97 lines
2.7 KiB
JavaScript
97 lines
2.7 KiB
JavaScript
function postReply(id, type, payload) {
|
|
self.postMessage({ id, type, ok: true, ...payload });
|
|
}
|
|
|
|
function postError(id, type, error) {
|
|
self.postMessage({
|
|
id,
|
|
type,
|
|
ok: false,
|
|
error: error?.message ?? String(error),
|
|
});
|
|
}
|
|
|
|
function pythonRuntimeAvailable() {
|
|
return typeof self.loadPyodide === "function" || Boolean(self.pyodide?.runPythonAsync);
|
|
}
|
|
|
|
function getRuntimeProvider() {
|
|
return self.linuxCncToolDbPythonRuntimeProvider ?? null;
|
|
}
|
|
|
|
async function loadRuntimeProvider(moduleUrl) {
|
|
if (!moduleUrl) {
|
|
return {
|
|
status: "blocked_browser_python_wasm_runtime_missing",
|
|
provider: getRuntimeProvider(),
|
|
};
|
|
}
|
|
try {
|
|
await import(moduleUrl);
|
|
} catch (error) {
|
|
return {
|
|
status: "blocked_browser_python_wasm_runtime_load_failed",
|
|
error,
|
|
provider: getRuntimeProvider(),
|
|
};
|
|
}
|
|
const provider = getRuntimeProvider();
|
|
if (!provider) {
|
|
return {
|
|
status: "blocked_browser_python_wasm_provider_missing",
|
|
provider,
|
|
};
|
|
}
|
|
return {
|
|
status: "browser_python_wasm_runtime_ready",
|
|
provider,
|
|
};
|
|
}
|
|
|
|
self.addEventListener("message", async (event) => {
|
|
const message = event.data ?? {};
|
|
const { id, type, line } = message;
|
|
try {
|
|
if (type === "start") {
|
|
const runtimeLoad = await loadRuntimeProvider(message.pythonRuntimeModuleUrl);
|
|
const provider = runtimeLoad.provider;
|
|
const providerReady = Boolean(provider?.start && provider?.writeLine && provider?.readLine);
|
|
const ready = providerReady || pythonRuntimeAvailable();
|
|
if (providerReady) {
|
|
await provider.start(message);
|
|
}
|
|
postReply(id, type, {
|
|
runtimeMode: "browser-python-wasm-worker",
|
|
runtimeExecutionReady: ready,
|
|
status: ready ? "browser_python_wasm_runtime_ready" : runtimeLoad.status,
|
|
error: runtimeLoad.error?.message ?? null,
|
|
executionEnabled: false,
|
|
promotionAllowed: false,
|
|
});
|
|
return;
|
|
}
|
|
const provider = getRuntimeProvider();
|
|
if (type === "writeLine" && provider?.writeLine) {
|
|
await provider.writeLine(line);
|
|
postReply(id, type, { executionEnabled: false, promotionAllowed: false });
|
|
return;
|
|
}
|
|
if (type === "readLine" && provider?.readLine) {
|
|
postReply(id, type, {
|
|
line: await provider.readLine(),
|
|
executionEnabled: false,
|
|
promotionAllowed: false,
|
|
});
|
|
return;
|
|
}
|
|
if (type === "close" && provider?.close) {
|
|
await provider.close();
|
|
postReply(id, type, { executionEnabled: false, promotionAllowed: false });
|
|
return;
|
|
}
|
|
throw new Error("blocked_browser_python_wasm_runtime_missing");
|
|
} catch (error) {
|
|
postError(id, type, error);
|
|
}
|
|
});
|