Skip to content

Commit

Permalink
wasm: close base64 encoder (#22)
Browse files Browse the repository at this point in the history
Close the base64 encoder before returning the buffer. This ensures that
all the bytes are written to the buffer. Without this, the last few
bytes may be missing, resulting in a corrupted image. Usually, this
would only corrupt the checksum, and the image would still be displayed
fine. However, some applications do not like this (e.g., slack).
  • Loading branch information
oncilla committed Nov 14, 2023
1 parent cdfff40 commit 99e5fea
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cmd/wasm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func yellerWrapper() js.Func {
if err := png.Encode(enc, yelledAt); err != nil {
return newError(fmt.Errorf("encoding image: %w", err))
}
if err := enc.Close(); err != nil {
return newError(fmt.Errorf("closing encoder: %w", err))
}

return map[string]any{
"result": buf.String(),
}
Expand Down
13 changes: 10 additions & 3 deletions docs/wasm_exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@
this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
}

const setInt32 = (addr, v) => {
this.mem.setUint32(addr + 0, v, true);
}

const getInt64 = (addr) => {
const low = this.mem.getUint32(addr + 0, true);
const high = this.mem.getInt32(addr + 4, true);
Expand Down Expand Up @@ -206,7 +210,10 @@

const timeOrigin = Date.now() - performance.now();
this.importObject = {
go: {
_gotest: {
add: (a, b) => a + b,
},
gojs: {
// Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
// may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
// function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
Expand Down Expand Up @@ -269,7 +276,7 @@
this._resume();
}
},
getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early
getInt64(sp + 8),
));
this.mem.setInt32(sp + 16, id, true);
},
Expand Down Expand Up @@ -551,4 +558,4 @@
};
}
}
})();
})();
Binary file modified docs/yell-at.wasm
Binary file not shown.

0 comments on commit 99e5fea

Please sign in to comment.