While developing a docsify plugin to run Go WASM binaries and send their console output to an xterm.js inside a markdown document I ran into what looks like incompatibilities. Can you please clarify?
Yes, I checked that I was using tinygo's wasm_exec.js (from the repo's targets/ directory), not Go's.
There seems to be two issues here at hand:
- hooking into
fs.writeSync,
- argv passing.
fs.writeSync Hook
In my web worker I use globalThis (which is what https://github.com/golang/go/blob/go1.26.2/lib/wasm/`wasm_exec.js` does). However, to be safe I tried tinygo's wasm_exec.js also with my modified worker using self, and the results were the same.
Now, I need to call importScripts(...) on TinyGo's wasm_exec.js inside globalThis.onMessage because only the document script knows which implementation to import. Next comes basically...
globalThis.onmessage = async (e) => {
const { wasm, wasmexec, args } = e.data
importScripts(wasmexec || 'wasm_exec.js')
const fsWriteSync = globalThis.fs.writeSync
globalThis.fs.writeSync = function (fd, buf) {
switch (fd) {
case 1:
case 2:
globalThis.postMessage({
type: fd === 1 ? 'stdout' : 'stderr',
data: decoder.decode(buf),
})
return buf.length // don't chain
default:
return fsWriteSync(fd, buf)
}
}
When executing this still prints to the console instead of going through my hook.
argv passing
const go = new Go()
go.argv = [wasm, ...args]
doesn't seem to work and searching tinygo's script I don't see any place where the Go object does handle argv. It's used only in the nodejs execution environment from what I understand.
While developing a docsify plugin to run Go WASM binaries and send their console output to an xterm.js inside a markdown document I ran into what looks like incompatibilities. Can you please clarify?
Yes, I checked that I was using tinygo's
wasm_exec.js(from the repo'stargets/directory), not Go's.There seems to be two issues here at hand:
fs.writeSync,fs.writeSync Hook
In my web worker I use
globalThis(which is what https://github.com/golang/go/blob/go1.26.2/lib/wasm/`wasm_exec.js` does). However, to be safe I tried tinygo'swasm_exec.jsalso with my modified worker usingself, and the results were the same.Now, I need to call
importScripts(...)on TinyGo'swasm_exec.jsinsideglobalThis.onMessagebecause only the document script knows which implementation to import. Next comes basically...When executing this still prints to the console instead of going through my hook.
argv passing
doesn't seem to work and searching tinygo's script I don't see any place where the Go object does handle
argv. It's used only in the nodejs execution environment from what I understand.