Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: This expression is not callable. No constituent of type 'ExportValue' is callable. WebAssembly module

I got this problem when I run this code in index.ts with Deno.

const wasm = await Deno.readFile("./wasm_test/pkg/wasm_test_bg.wasm");
const wasmModule = new WebAssembly.Module(wasm);
const wasmInstance = new WebAssembly.Instance(wasmModule);
const wasmTest = wasmInstance.exports;
wasmTest.sum(1, 3); // Error

Error: This expression is not callable. No constituent of type 'ExportValue' is callable.

I get and error when calling sum, it should give me a 4 as a result. When I run it as index.js it works perfectly. I used wasm-pack to compile the Rust code.

like image 791
lescuer Avatar asked Oct 28 '25 03:10

lescuer


1 Answers

The problem is that the name add is not known. Change the 4th line of your code as below:

const wasm = await Deno.readFile("./add.wasm");
const wasmModule = new WebAssembly.Module(wasm);
const wasmInstance = new WebAssembly.Instance(wasmModule);
const sum = wasmInstance.exports.sum as CallableFunction; // exports.add if you test with the below linked wasm.
console.log(sum(1, 3))

See documentation.

For my test I found a wasm example with an add-function here.

like image 143
jps Avatar answered Oct 30 '25 18:10

jps