Ошибка:
test2.html:1 Uncaught (in promise) LinkError: WebAssembly.instantiate(): Import #0 module="env" function="iprintf" error: function import requires a callable
function loadWebAssembly(filename, imports = {}) {
return fetch(filename)
.then((response) => response.arrayBuffer())
.then((buffer) => {
imports.env = imports.env || {};
Object.assign(imports.env, {
memoryBase: 0,
tableBase: 0,
__memory_base: 0,
__table_base: 0,
memory: new WebAssembly.Memory({ initial: 256, maximum: 256 }),
table: new WebAssembly.Table({
initial: 0,
maximum: 0,
element: "anyfunc",
}),
square: (a) => a * a,
print: () => console.log("Hello world"),
});
return WebAssembly.instantiate(buffer, imports);
})
.then((result) => result.instance);
}
function main() {
loadWebAssembly("test.wasm").then((instance) => {
const square = instance.exports._Z6squarei;
console.log(instance.exports);
console.log("2^2 =", square(2));
});
}
#include <emscripten.h>
#include <cstdio>
EMSCRIPTEN_KEEPALIVE
int square (int x) {
return x * x;
}
EMSCRIPTEN_KEEPALIVE
void print () {
printf("Hello world");
}
Собираю вот так: em++ test2.cpp -Os -s WASM=1 -s SIDE_MODULE=1 -o test.wasm
До того как добавил функцию print все работало.