RunKit WASM Hello World

node v10.24.1
version: 3.0.0
endpointsharetweet
const libwabt = require("wat2wasm/libwabt"); const { TextDecoder } = require("util"); function compileWat(watStr, imports = {}) { return new Promise((resolve, reject) => { libwabt().then(wabt => { const buffer = wabt.parseWat('', watStr).toBinary({}).buffer; resolve(new WebAssembly.Instance(new WebAssembly.Module(buffer), imports)); }).catch(reject); }); } const watStr = ` (module (import "console" "log" (func $log (param i32 i32))) (import "js" "mem" (memory 1)) (data (i32.const 0) "Hello World!") (func (export "hello") i32.const 0 ;; pass offset to log i32.const 12 ;; pass length to log call $log) ) `; const memory = new WebAssembly.Memory({initial:1}); function consoleLogString(offset, length) { var bytes = new Uint8Array(memory.buffer, offset, length); var string = new TextDecoder("utf8").decode(bytes); console.log(string); } const { hello } = (await compileWat(watStr, { console: { log: consoleLogString }, js: { mem: memory } })).exports; hello();
Loading…

no comments

    sign in to comment