Home > Net >  What does (import) in .wat mean?
What does (import) in .wat mean?

Time:08-15

The Problem

I have been fiddeling around with wasm all day, now (import $import0 "env" "_Znaj" (param i32) (result i32)) popped up in my .wat. And it breaks my code.

The Error Message

The exact error I get is:

Uncaught (in promise) LinkError: import object field '_Znaj' is not a Function

JavaScript implementation

This is how I try And use it:

const importObject = {
            env: {
              __memory_base: 0,
              __table_base: 0,
              memory: new WebAssembly.Memory({initial: 1})
            }
          }
        fetch('wasmPrimes.wasm').then((response) =>
            response.arrayBuffer()
        ).then((bytes) =>
            WebAssembly.instantiate(bytes, importObject)
        ).then((results) => {
            primes = results.instance.exports._Z10wasmPrimesi(amt);
        });

Pic & Pastebin

Here is a pic of my c code and the .wat (pls follow the link I dont have 10 rep jet)
Or heres a pastebin of the entire .wat code: https://pastebin.com/6wm7sHLG

C Source for the wasm Module

And heres the c for anyone, who didn't wanna open the pic:

int* wasmPrimes(int amt)
{
  int num = 1;
  int* primes = new int[amt];
  if (amt > 0) primes[0] = 2;
  amt --;
  for (int i = 0; i < amt; i  )
  {
    bool prime = false;
    while (!prime)
    {
      num  ;
      prime = true;
      for (int j = 0; j < i 1; j  )
      {
        if (num%primes[j]==0)
        {
          prime = false;
          break;
        }
      }
    }
    primes[i 1] = num;
  }
  
  return primes;
}

CodePudding user response:

_Znaj. Is the array allocator used by the new operator when creating new arrays. At least some compiler do it that way. And whatever the compiler was you used it did the same. This _Znaj is then linked dynamically or statically. In this case it is dynamic for whatever reason but on most online WASM compilers it is put inside the importObject which you overwrite or which was not supplied by the environment. Which is speculative because I don not know what kind of environment you used.

  • Related