From cf37a79f343dba3b89adca5d3b4baa7025f756ef Mon Sep 17 00:00:00 2001 From: karim Date: Tue, 27 Oct 2026 19:13:05 +0100 Subject: [PATCH] WASM-Waechter guarded() fuer truck (Trap -> neu instanziieren, einmal wiederholen) + Tests --- src/engine/truckGuard.test.ts | 37 ++++++++++++++++++ src/engine/truckSolid.ts | 71 +++++++++++++++++++++++------------ 2 files changed, 85 insertions(+), 23 deletions(-) create mode 100644 src/engine/truckGuard.test.ts diff --git a/src/engine/truckGuard.test.ts b/src/engine/truckGuard.test.ts new file mode 100644 index 00000000..e7168805 --- /dev/null +++ b/src/engine/truckGuard.test.ts @@ -0,0 +1,37 @@ +import { describe, expect, it, vi } from "vitest"; + +// `guarded` wird gegen ein Fake-WASM geprüft: Trap → neu instanziieren + einmal wiederholen. +vi.mock("./pkgTruck/trucksolid.js", () => { + let instances = 0; + return { + default: async () => { + instances++; + (globalThis as { __instances?: number }).__instances = instances; + }, + }; +}); + +import { guarded } from "./truckSolid"; + +const instances = () => (globalThis as { __instances?: number }).__instances ?? 0; + +describe("guarded (WASM-Wächter)", () => { + it("normale Fehler laufen unveraendert durch, ohne Neustart", async () => { + const before = instances(); + await expect(guarded(() => { throw new Error("Boolean fehlgeschlagen"); })).rejects.toThrow("Boolean fehlgeschlagen"); + expect(instances()).toBe(before || 1); + }); + it("Trap: Instanz wird neu erzeugt, zweiter Versuch gelingt", async () => { + let calls = 0; + const r = await guarded(() => { + if (calls++ === 0) throw new WebAssembly.RuntimeError("unreachable"); + return "ok"; + }); + expect(r).toBe("ok"); + expect(calls).toBe(2); + expect(instances()).toBeGreaterThanOrEqual(2); + }); + it("wiederholter Trap: klare Meldung statt rohem Trap", async () => { + await expect(guarded(() => { throw new WebAssembly.RuntimeError("unreachable"); })).rejects.toThrow(/abgestürzt/); + }); +}); diff --git a/src/engine/truckSolid.ts b/src/engine/truckSolid.ts index 8bb7b2fa..327bcc1a 100644 --- a/src/engine/truckSolid.ts +++ b/src/engine/truckSolid.ts @@ -10,6 +10,37 @@ async function getModule(): Promise { return modulePromise; } +/** Ob ein Fehler ein WASM-Panic ist (Rust `panic!`/`unwrap` → Trap „unreachable"). */ +function isWasmTrap(e: unknown): boolean { + const msg = e instanceof Error ? `${e.name} ${e.message}` : String(e); + return /RuntimeError|unreachable|memory access out of bounds|recursive use of an object/i.test(msg); +} + +/** + * Ruft eine WASM-Funktion abgesichert auf (lessons-bimcraft [14]): nach einem Panic ist die + * Modul-Instanz unbrauchbar (Rust-Zustand verwaist) — sie wird verworfen, neu instanziiert und + * der Aufruf EINMAL wiederholt. Scheitert auch das, kommt eine klare Fehlermeldung statt des + * rohen Traps; der Aufrufer fällt auf Prisma/Platzhalter zurück (so tun es voids3d/extrusions). + * Normale Rust-Fehler (`JsError`, z. B. „Boolean fehlgeschlagen") laufen unverändert durch. + */ +export async function guarded(call: (m: any) => T): Promise { + try { + return call(await getModule()); + } catch (e) { + if (!isWasmTrap(e)) throw e; + modulePromise = null; // Instanz verwerfen + try { + return call(await getModule()); + } catch (e2) { + if (isWasmTrap(e2)) { + modulePromise = null; + throw new Error("Geometrie-Kern (truck) ist abgestürzt und wurde neu gestartet — Vorgang übersprungen."); + } + throw e2; + } + } +} + export interface ExtrudedMesh { positions: number[]; indices: number[]; @@ -22,8 +53,7 @@ export async function extrudePolygon( height: number, taper = 0, ): Promise { - const m = await getModule(); - const json = m.extrude_polygon(JSON.stringify({ points, height, taper })); + const json = await guarded((m) => m.extrude_polygon(JSON.stringify({ points, height, taper }))); return JSON.parse(json) as ExtrudedMesh; } @@ -34,8 +64,7 @@ export async function extrudeCircle( height: number, taper = 0, ): Promise { - const m = await getModule(); - const json = m.extrude_circle(JSON.stringify({ cx, cy, r, height, taper })); + const json = await guarded((m) => m.extrude_circle(JSON.stringify({ cx, cy, r, height, taper }))); return JSON.parse(json) as ExtrudedMesh; } @@ -56,15 +85,16 @@ export async function booleanMesh( b: ExtrudedMesh, op: BooleanOp, ): Promise { - const m = await getModule(); - const json = m.boolean_mesh( - JSON.stringify({ - a_positions: a.positions, - a_indices: a.indices, - b_positions: b.positions, - b_indices: b.indices, - op, - }), + const json = await guarded((m) => + m.boolean_mesh( + JSON.stringify({ + a_positions: a.positions, + a_indices: a.indices, + b_positions: b.positions, + b_indices: b.indices, + op, + }), + ), ); return JSON.parse(json) as BooleanMeshResult; } @@ -118,8 +148,7 @@ export type SolidSpec = /** Tesselliert einen B-Rep-Körper (Sehnenabweichung `tol` Meter). */ export async function buildSolidMesh(spec: SolidSpec, tol = 0.01): Promise { - const m = await getModule(); - return JSON.parse(m.build_solid_mesh(JSON.stringify({ spec, tol }))) as ExtrudedMesh; + return JSON.parse(await guarded((m) => m.build_solid_mesh(JSON.stringify({ spec, tol })))) as ExtrudedMesh; } /** Kennwerte eines Körpers (Meter / m² / m³). */ @@ -134,8 +163,7 @@ export interface SolidProps { } export async function solidProps(spec: SolidSpec, tol = 0.005): Promise { - const m = await getModule(); - return JSON.parse(m.solid_props(JSON.stringify({ spec, tol }))) as SolidProps; + return JSON.parse(await guarded((m) => m.solid_props(JSON.stringify({ spec, tol })))) as SolidProps; } export interface ClashResult { @@ -148,8 +176,7 @@ export interface ClashResult { /** Kollisionsprüfung zweier Körper (truck-meshalgo). */ export async function solidClash(a: SolidSpec, b: SolidSpec, tol = 0.01): Promise { - const m = await getModule(); - const r = JSON.parse(m.solid_clash(JSON.stringify({ a, b, tol }))) as { + const r = JSON.parse(await guarded((m) => m.solid_clash(JSON.stringify({ a, b, tol })))) as { colliding: boolean; lines: ClashResult["lines"]; overlap_volume: number | null; @@ -162,12 +189,10 @@ export async function exportStepSolids( solids: SolidSpec[], fileName = "", ): Promise { - const m = await getModule(); - return m.export_step(JSON.stringify({ solids, fileName, organizationSystem: "dossier" })); + return guarded((m) => m.export_step(JSON.stringify({ solids, fileName, organizationSystem: "dossier" }))); } /** Liest STEP-Text und liefert je Körper ein Dreiecks-Mesh (`relTol` relativ zur Diagonale). */ export async function importStepMeshes(text: string, relTol = 0.0005): Promise { - const m = await getModule(); - return JSON.parse(m.import_step(text, relTol)) as ExtrudedMesh[]; + return JSON.parse(await guarded((m) => m.import_step(text, relTol))) as ExtrudedMesh[]; }