Skip to content

tinygo's wasm_exec.js feature incompatible with Go's? #5300

@thediveo

Description

@thediveo

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:

  1. hooking into fs.writeSync,
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    wasmWebAssembly

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions