Hello thank you for this tutorials. I took your examples as a base for writing my demo
I would like to suggest a point that could be actually more explicit (IMHO): the WebAssembly functions once instantiated are available as any JS function.
This may sound obvious when you know it but it wasn't for me until I read another article (I can't find it back) where the author had the same observation.
For those in the same situation, here is what I did: loadWasm.then dispatches a custom event and Hello is called in the custom event handler.
For instance:
loadWasm(wasmFile).then(wasm => {
const wasmLoaded = new Event('wasmLoaded', {
bubbles: false,
cancelable: true,
composed: false
})
document.dispatchEvent(wasmLoaded)
console.log("wasmLoaded")
}).catch(error => {
console.log(error)
})
I extracted the <script></script> part from your index.html file and put it in a separate file loadWsm.Js which can be generically used to load and instantiate any wasm file (at least those written and compiled from GO).
Then WASM functions can be used:
document.addEventListener("wasmLoaded",()=>{
let jsonData = Hello("Bob", "Morane")
console.log(jsonData)
document.querySelector("h1").innerHTML = JSON.stringify(jsonData)
})
I'd like also to add a remark: tinygo doesn't support all libraries/functions.
For instance, as you did in your example, objects have to be converted by "hand" to JSON strings.
It took me some time to realize that the issues I had with crypto/rand (resolved with tinygo 0.21) and json.Marshal were due to tinygo
Amari