WebAssemblyOS / wasmos

OS primitives and shell for AssembyScript and WebAssembly
MIT License
42 stars 21 forks source link

How can you import external functions into the web-assembly programs? #104

Open JRMurr opened 5 years ago

willemneal commented 5 years ago

Well it depends. If you are using the browser version you must pass an object containing the functions, memories, or function tables, and the names must match what the binary expects. e.g. Assemblyscript:

@external("log", "logInt")
declare function logInt(i: u32): void;
let imports = {
   "log": {
               "logInt": (i) => { console.log(i);}
              }
}

You also typically want the imported functions to have access to the memory of the WebAssembly instance as then you can read/write data directly to it. This is done by declaring the memory that is imported in the scope of the imports and then passing the memory as an import.