tests/run-make/wasm-exceptions-nostd/verify.mjs 75 lines View on github.com → Search inside
1import fs from 'fs';23const dec = new TextDecoder("utf-8");45if (process.argv.length != 3) {6    console.log("Usage: node verify.mjs <wasm-file>");7    process.exit(0);8}910const wasmfile = process.argv[2];11if (!fs.existsSync(wasmfile)) {12    console.log("Error: File not found:", wasmfile);13    process.exit(1);14}1516const wasmBuffer = fs.readFileSync(wasmfile);1718async function main() {1920    let memory = new ArrayBuffer(0) // will be changed after instantiate2122    const captured_output = [];2324    const imports = {25        env: {26            __log_utf8: (ptr, size) => {27                const str = dec.decode(new DataView(memory, ptr, size));28                captured_output.push(str);29                console.log(str);30            }31        }32    };3334    const wasmModule = await WebAssembly.instantiate(wasmBuffer, imports);35    memory = wasmModule.instance.exports.memory.buffer;3637    const start = wasmModule.instance.exports.start;38    const return_code = start();3940    console.log("Return-Code:", return_code);4142    if (return_code !== 0) {43        console.error("Expected return code 0");44        process.exit(return_code);45    }4647    const expected_output = [48        '`r#try` called with ptr 0x1234',49        'Dropped',50        'Caught something!',51        '  data     : 0x1234',52        '  exception: "index out of bounds: the len is 1 but the index is 4"',53        'This program terminates correctly.',54    ];55    56    assert_equal(captured_output, expected_output);57}5859function assert_equal(captured_output, expected_output) {60    if (captured_output.length != expected_output.length) {61        console.error("Unexpected number of output lines. Got", captured_output.length, "but expected", expected_output.length);62        process.exit(1); // exit with error63    }6465    for (let idx = 0; idx < expected_output.length; ++idx) {66        if (captured_output[idx] !== expected_output[idx]) {67            console.error("Unexpected output");68            console.error("[got]     ", captured_output[idx]);69            console.error("[expected]", expected_output[idx]);70            process.exit(2); // exit with error71        }72    }73}7475await main();

Code quality findings 9

Use strict inequality (!==) to prevent type coercion bugs
info correctness loose-inequality
if (process.argv.length != 3) {
Remove debugging statements or use a logging library
info correctness console-log
console.log("Usage: node verify.mjs <wasm-file>");
Remove debugging statements or use a logging library
info correctness console-log
console.log("Error: File not found:", wasmfile);
Ensure all async functions handle errors properly
info correctness async-without-catch
async function main() {
Remove debugging statements or use a logging library
info correctness console-log
console.log(str);
Remove debugging statements or use a logging library
info correctness console-log
console.log("Return-Code:", return_code);
Use strict equality (===) to prevent type coercion bugs
info correctness loose-equality
if (return_code !== 0) {
Use strict inequality (!==) to prevent type coercion bugs
info correctness loose-inequality
if (captured_output.length != expected_output.length) {
Use strict equality (===) to prevent type coercion bugs
info correctness loose-equality
if (captured_output[idx] !== expected_output[idx]) {

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.