Download source/test/debug-instrumentation.test.js from FormaLau/microduck-reachy-simulator: direct link, hf CLI and curl.
- Browser
- Download file 5.29 kB
-
https://huggingface.co/spaces/FormaLau/microduck-reachy-simulator/resolve/main/source/test/debug-instrumentation.test.js
- Command line
-
hf download hf://spaces/FormaLau/microduck-reachy-simulator/source/test/debug-instrumentation.test.js
-
curl -L -o debug-instrumentation.test.js https://huggingface.co/spaces/FormaLau/microduck-reachy-simulator/resolve/main/source/test/debug-instrumentation.test.js
5.29 kB
| import test from 'node:test'; | |
| import assert from 'node:assert/strict'; | |
| import { runClang } from '@yowasp/clang'; | |
| import { WASI, ConsoleStdout } from '@bjorn3/browser_wasi_shim'; | |
| import { instrumentC } from '../src/training/debugInstrumentation.js'; | |
| import { DebugController, readDebugValue, createDebugInspector } from '../src/training/debugRuntime.js'; | |
| async function compile(source, debug = false) { | |
| let diagnostics = '', tree = ''; | |
| const decoder = new TextDecoder(); | |
| const stderr = bytes => { if (bytes) diagnostics += decoder.decode(bytes, { stream: true }); }; | |
| const options = { stderr, decodeASCII: false, fetchProgress() {} }; | |
| try { | |
| if (debug) { | |
| await runClang(['clang', '-std=c17', '-Xclang', '-ast-dump=json', '-fsyntax-only', 'main.c'], { 'main.c': source }, { | |
| ...options, stdout: bytes => { if (bytes) tree += decoder.decode(bytes, { stream: true }); }, | |
| }); | |
| source = instrumentC(source, JSON.parse(tree)).source; | |
| } | |
| const files = await runClang(['clang', '-std=c17', '-O0', 'main.c', '-o', 'main.wasm'], { 'main.c': source }, options); | |
| return files['main.wasm']; | |
| } catch (error) { throw new Error(`${diagnostics}\n${error.message}\n${source}`); } | |
| } | |
| async function execute(binary) { | |
| let output = '', instance, vars = []; const points = []; | |
| const wasi = new WASI(['main'], [], [new ConsoleStdout(() => {}), new ConsoleStdout(bytes => { output += new TextDecoder().decode(bytes); }), new ConsoleStdout(() => {})]); | |
| const string = at => { const bytes = new Uint8Array(instance.exports.memory.buffer); return new TextDecoder().decode(bytes.subarray(at, bytes.indexOf(0, at))); }; | |
| instance = await WebAssembly.instantiate(await WebAssembly.compile(binary), { wasi_snapshot_preview1: wasi.wasiImport, debug: { | |
| reset() { vars = []; }, | |
| value(name, type, address, size) { vars.push({ name: string(name), value: readDebugValue(instance.exports.memory, address, size, string(type)) }); }, | |
| pause(line, site, depth, fn) { points.push({ line, site, depth, fn: string(fn), vars }); }, | |
| } }); | |
| const code = wasi.start(instance); | |
| return { output, code, points }; | |
| } | |
| test('AST instrumentation preserves macros, UTF-8 offsets, branches, arrays and loop lifetimes', async () => { | |
| const source = `#include <stdio.h> | |
| #define LIMIT 3 | |
| #define ADD(x) ((x) + 2) | |
| // caractères UTF-8 : é 🦆 | |
| int calc(int x) { if (x > 1) return ADD(x); else return 0; } | |
| int main(void) { | |
| int matrix[2][3]={{1,2,3},{4,5,6}}; | |
| int x=0; | |
| for (int i=0; i<LIMIT; i++) x += calc(i); | |
| if (x>1) { x++; } else x--; | |
| while (x<6) ++x; | |
| do x--; while (x>5); | |
| for(int i=0;;i++) { x+=i; if(i==2) break; } | |
| switch(x) { case 8: x++; break; default: x=0; } | |
| printf("%d %d\\n",x,matrix[1][2]); | |
| return x; | |
| }`; | |
| const normal = await execute(await compile(source)); | |
| const debug = await execute(await compile(source, true)); | |
| assert.equal(debug.output, normal.output); | |
| assert.equal(debug.code, normal.code); | |
| assert.equal(debug.output, '9 6\n'); | |
| assert.ok(debug.points.some(p => p.fn === 'calc' && p.depth === 2)); | |
| const matrix = debug.points.flatMap(p => p.vars).find(v => v.name === 'matrix'); | |
| assert.deepEqual(matrix.value, [[1, 2, 3], [4, 5, 6]]); | |
| const afterLoop = debug.points.find(p => p.line === 10); | |
| assert.ok(!afterLoop.vars.some(v => v.name === 'i')); | |
| }); | |
| test('step over respects call depth, step into visits callees and breakpoints override stepping', () => { | |
| const control = new DebugController([20]); | |
| assert.equal(control.shouldPause({ line: 1, depth: 1 }), true); | |
| control.resume('over', 2); | |
| assert.equal(control.shouldPause({ line: 3, depth: 3 }), false); | |
| assert.equal(control.shouldPause({ line: 4, depth: 2 }), true); | |
| assert.equal(control.shouldPause({ line: 20, depth: 3 }), true); | |
| control.resume('out', 2); | |
| assert.equal(control.shouldPause({ line: 3, depth: 2 }), false); | |
| assert.equal(control.shouldPause({ line: 4, depth: 1 }), true); | |
| control.resume('continue', 1); | |
| assert.equal(control.shouldPause({ line: 4, depth: 1 }), false); | |
| control.breakpoints = new Set([4]); | |
| assert.equal(control.shouldPause({ line: 4, depth: 1 }), true); | |
| }); | |
| test('array inspection reaches every index, nested dimension and fresh paused value', () => { | |
| const memory = new WebAssembly.Memory({ initial: 1 }); | |
| const data = new Int32Array(memory.buffer); | |
| for (let i = 0; i < 600; i++) data[i] = i * 3; | |
| const inspector = createDebugInspector(memory); | |
| const variables = [{ name: 'values', type: 'int[600]', address: 0, size: 2400 }, { name: 'matrix', type: 'int[2][300]', address: 0, size: 2400 }]; | |
| const first = inspector.snapshot(variables); | |
| assert.equal(first[0].value.length, 600); | |
| assert.equal(first[0].value.preview.length, 8); | |
| const last = inspector.page(first[0].value.id, 576); | |
| assert.equal(last.items.at(-1).index, 599); | |
| assert.equal(last.items.at(-1).value, 1797); | |
| const row = inspector.page(first[1].value.id).items[1].value; | |
| assert.equal(inspector.page(row.id, 256).items.at(-1).value, 1797); | |
| data[599] = 42; | |
| const second = inspector.snapshot(variables); | |
| assert.throws(() => inspector.page(first[0].value.id), /no longer available/); | |
| assert.equal(inspector.page(second[0].value.id, 576).items.at(-1).value, 42); | |
| assert.throws(() => inspector.page(second[0].value.id, -1)); | |
| }); | |