Most of the demos in WebAssembly are still in C/C++ and don't show much wat
. The few examples so far show things such as this:
;; Convert this to add.wasm by running:
;;
;; wat2wasm add.wat -o add.wasm
;;
(module
(func (export "add") (param i32 i32) (result i32)
get_local 0
get_local 1
i32.add))
That uses local variables and calls a native function. I know there's get_global
and such too.
What I'm wondering is how to use load and store to manage global(?) memory. I am having trouble understanding how to use the functions.
As an example, how would you load in an array of strings from JavaScript into WebAssembly, then print them out. Something like this:
const fs = require('fs')
const buf = fs.readFileSync('./add.wasm')
WebAssembly.instantiate(new Uint8Array(buf)).then(function(results){
var lib = results.instance.exports
lib.storeArray(['hello', 'world'])
lib.logArray()
// hello
// world
})
With assembly along the lines of:
(module
(func (export "storeArray") (param ?) (result ?)
iterate and i32.store somehow)
(func (export "logArray") (param ?) (result ?)
i32.load ? iterate through something
console.log(item)))
Specifically wondering how to reference the memory addresses (loading/storing values) and use that feature.