#445·wasm3

List import-functions (with signature)

Author: konsumerCreated Sep 10, 2023Updated Aug 26, 2026
LabelsAPI

On web/nodejs host, loading wasm with undefined imports will give me a hint about what is missing, in the default error. It would be helpful to see this sort of output in wasm3, too.

If I do this:

c
M3Result result = m3Err_none;
IM3Environment env = m3_NewEnvironment();
IM3Runtime runtime = m3_NewRuntime(env, WASM_STACK_SIZE, NULL);
runtime->memoryLimit = WASM_MEMORY_LIMIT;

IM3Module module;
result = m3_ParseModule(env, &module, (const uint8_t*)wasmBytes, bytesRead);
if (result) {
  fprintf(stderr, "Failed to parse module: %s\n", result);
  main_unload();
  return 1;
}

result = m3_LoadModule(runtime, module);
if (result) {
  fprintf(stderr, "Failed to load module: %s\n", result);
  return 1;
}

IM3Function start = NULL;
result = m3_FindFunction(&start, runtime, "_start");
if (result == m3Err_none) {
  result = m3_CallV(start);
  printf("_start entry: %s\n", result);
} else {
  result = m3_FindFunction(&start, runtime, "load");
  if (result == m3Err_none) {
    result = m3_CallV(start);
    printf("load entry: %s\n", result);
  } else {
    printf("no entry found\n");
  }
}

I see this:

_start entry: missing imported function

If I knew which function, I could implement a stub to get it working quickly. It would be especially useful if it outputted the signature, like this:

_start entry: missing imported function wasi_snapshot_preview1.proc_exit: v(i)

On web, I automate making stub-functions (and log) so things will run without needing me to set them up, and it gives me a hint as to what is missing:

javascript
const cartWasm = await WebAssembly.compile(wasmBytes)

// this will fill cart imports with stub functions
for (const c of WebAssembly.Module.imports(cartWasm)) {
  imports[c.module] = imports[c.module] || {}
  if (c.kind === 'function' && !imports[c.module][c.name]) {
    console.log(`STUB ${c.module}.${c.name}`)
    imports[c.module][c.name] = (...args) => output(`${c.module}.${c.name}(${args.join(', ')})\n`)
  }
}

const cart = {...(await WebAssembly.instantiate(wasmBytes, imports)).instance.exports}

Which logs like this:

STUB wasi_snapshot_preview1.proc_exit

and if anything calls it, shows how it was called. Because it added the stub-import, the program will keep running (in this case no WASI exit is not a huge problem, for example.)