Slide 14
Slide 14 text
Wasm и JavaScript — лучшие друзья
14
(module
(func $i (import "imports" "logger") (param i32))
(memory (import "imports" "importedMemory") 1)
(func (export "exportedFunc")
i32.const 42
call $i)
(func (export "add") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add)
(data (i32.const 0) "tver.io"))
const importedMemory = new WebAssembly.Memory({
initial: 1,
maximum: 10
});
WebAssembly.instantiateStreaming(fetch("simple.wasm"), {
imports: {
logger: function(arg) {
console.log("first function call", arg);
},
importedMemory
}
}).then(obj => {
obj.instance.exports.exportedFunc();
const three = obj.instance.exports.add(1, 2);
console.log("1 + 2 =", three);
var memoryArray = new Uint32Array(importedMemory.buffer, 0, 10);
console.log(
"reading from memory",
new TextDecoder("utf8").decode(memoryArray)
);
});