0

When C/C++ .wasm code is compiled with clang (C) - it loads in Chrome and works well, but when with clang++ (C++) - wasm load fails with error (in JS console):

Uncaught (in promise) LinkError: WebAssembly.instantiate(): Import #1 module="wasi_snapshot_preview1" function="fd_close" error: function import requires a callable

Why?

WASM compile arguments:

          "clang", <=== I only changed this to "clang++" - and it fails
          "-O0",
          // "-std=c++14",
          "--target=wasm32-unknown-wasi",
          "--sysroot C:\\OpenGL\\wasi-sdk-11.0-mingw.tar\\wasi-sdk-11.0\\share\\wasi-sysroot",
          "-fno-exceptions",
          "-nostartfiles",
          "-Wl,--import-memory",
          "-Wl,--no-entry",
          "-Wl,--export-all",
          "-o templates/my-app/public/hello_wasm.wasm",
          "wasm/hello_wasm.cpp"

JS wasm load code:

      const response = await fetch("./hello_wasm.wasm");
      const bytes = await response.arrayBuffer();
      const { instance } = await WebAssembly.instantiate(bytes, {
        env: { memory: this.memory },
        },
      });
      this.instance = instance;
      console.log("c" + instance);
    })();

hello_wasm.cpp (compilation was without an error):

#include <math.h>

// extern "C"
// {

int int_sqrt(int x)
{
  return sqrt(x);
};

float *run_sin(float x[], int n)
{
//  float *a = new float[n];
  float *a = (float *)malloc(sizeof(float) * n);
  for (int i = 0; i < n; i++)
  {
    a[i] = x[i] * 2;
 
  }
 
  return a;
}

LLVM v10 I use wasi sysroot from https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-11/wasi-sdk-11.0-mingw.tar.gz

Also discussing this issue here https://github.com/WebAssembly/wasi-sdk/issues/145

KestMa
  • 1
  • 2

1 Answers1

0

In order to run a WASI binary on the web you need to provide an implementation of the WASI APIs. The web platform does not natively support WASI. There are some polyfills out there that try to emulate some/all of the WASI APIs which might work for your case.

sbc100
  • 2,619
  • 14
  • 11