export function readDebugValue(memory, address, size, type, budget = { remaining: 256 }) { if (--budget.remaining < 0) return '…'; const view = new DataView(memory.buffer); const clean = type.replace(/\b(const|volatile|restrict)\b/g, '').trim(); if (address < 0 || address + size > view.byteLength) return ''; const array = clean.match(/^(.*?)\s*\[(\d+)\](.*)$/); if (array) { const count = Number(array[2]), stride = count ? size / count : 0; const values = []; for (let i = 0; i < Math.min(count, 64) && budget.remaining > 0; i++) values.push(readDebugValue(memory, address + i * stride, stride, array[1] + array[3], budget)); if (values.length < count) values.push('…'); return values; } if (clean.includes('*')) return `0x${view.getUint32(address, true).toString(16)}`; if (clean === 'float' && size === 4) return view.getFloat32(address, true); if (clean === 'double' && size === 8) return view.getFloat64(address, true); if (/^(?:(?:unsigned|signed|long|short|int|char|_Bool)\s*)+$/.test(clean) || /^enum\s/.test(clean)) { const unsigned = /unsigned|_Bool/.test(clean); const method = { 1: unsigned ? 'getUint8' : 'getInt8', 2: unsigned ? 'getUint16' : 'getInt16', 4: unsigned ? 'getUint32' : 'getInt32', 8: unsigned ? 'getBigUint64' : 'getBigInt64' }[size]; if (method) { const value = view[method](address, true); return typeof value === 'bigint' ? value.toString() : value; } } return `${size} bytes: ${Array.from(new Uint8Array(memory.buffer, address, Math.min(size, 32)), n => n.toString(16).padStart(2, '0')).join(' ')}${size > 32 ? ' …' : ''}`; } export class DebugController { constructor(breakpoints = []) { this.breakpoints = new Set(breakpoints); this.mode = 'into'; this.depth = Infinity; } resume(command, depth) { this.mode = command; this.depth = depth; } shouldPause(point) { return this.breakpoints.has(point.line) || this.mode === 'into' || this.mode === 'over' && point.depth <= this.depth || this.mode === 'out' && point.depth < this.depth; } } // Arrays stay in the suspended WASM memory. Only the requested page crosses // the worker boundary, so every index is accessible without huge snapshots. export function createDebugInspector(memory) { const arrays = new Map(); let sequence = 0; function describe(address, size, type) { const clean = type.replace(/\b(const|volatile|restrict)\b/g, '').trim(); const array = clean.match(/^(.*?)\s*\[(\d+)\](.*)$/); if (!array) return readDebugValue(memory, address, size, type); const length = Number(array[2]), stride = length ? size / length : 0; if (!Number.isSafeInteger(length) || !Number.isInteger(stride) || address < 0 || address + size > memory.buffer.byteLength) return ''; const elementType = array[1] + array[3], id = `array-${++sequence}`; arrays.set(id, { address, stride, length, elementType }); const preview = elementType.includes('[') ? null : Array.from({ length: Math.min(length, 8) }, (_, i) => readDebugValue(memory, address + i * stride, stride, elementType)); return { kind: 'array', id, length, preview }; } return { snapshot(variables) { arrays.clear(); return variables.map(({ name, type, address, size }) => ({ name, type, value: describe(address, size, type) })); }, page(id, start = 0) { const array = arrays.get(id); if (!array || !Number.isInteger(start) || start < 0 || start > array.length) throw new Error('Array is no longer available at this pause.'); const end = Math.min(start + 64, array.length), items = []; for (let index = start; index < end; index++) items.push({ index, value: describe(array.address + index * array.stride, array.stride, array.elementType) }); return { start, total: array.length, items }; }, }; }