My .wat
file has the following 2 lines of code.
(memory $buf 1)
(data (i32.const 0) "hello")
I would like to pass $buf
as an argument to an imported Javascript function. The code on the Javascript side looks like this:
function consoleLogString(memory, offset,length){
var bytes = new Uint8Array(memory.buffer, 0, 5);
var string = new TextDecoder('utf8').decode(bytes);
console.log(string);
}
var importObj = {console : {log: consoleLogString}};
fetchAndInstantiate('stringLogger2.wasm',importObj).then(function(instance) {
instance.exports.writeHi();
});
}
On the wasm side I would like to be able to do something like
(import "console" "log" (func $log (param i32 i32 i32)))
(export "writeHi" (func $writeHi))
(memory $buf 1)
(data (i32.const 0) "hello")
(func $writeHi
;; how do I push my buffer onto the stack so it can be passed to the function?
i32.const 0
i32.const 5
call $log
)