diff --git a/apps/gophers/web/wasm_exec.js b/apps/gophers/web/wasm_exec.js index 29427d91..a54bb9a9 100644 --- a/apps/gophers/web/wasm_exec.js +++ b/apps/gophers/web/wasm_exec.js @@ -387,6 +387,34 @@ mem().setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16)); }, + // func copyBytesToGo(dst []byte, src ref) (int, bool) + "syscall/js.copyBytesToGo": (sp) => { + const dst = loadSlice(sp + 8); + const src = loadValue(sp + 32); + if (!(src instanceof Uint8Array)) { + mem().setUint8(sp + 48, 0); + return; + } + const toCopy = src.subarray(0, dst.length); + dst.set(toCopy); + setInt64(sp + 40, toCopy.length); + mem().setUint8(sp + 48, 1); + }, + + // func copyBytesToJS(dst ref, src []byte) (int, bool) + "syscall/js.copyBytesToJS": (sp) => { + const dst = loadValue(sp + 8); + const src = loadSlice(sp + 16); + if (!(dst instanceof Uint8Array)) { + mem().setUint8(sp + 48, 0); + return; + } + const toCopy = src.subarray(0, dst.length); + dst.set(toCopy); + setInt64(sp + 40, toCopy.length); + mem().setUint8(sp + 48, 1); + }, + "debug": (value) => { console.log(value); }, @@ -403,7 +431,6 @@ true, false, global, - this._inst.exports.mem, this, ]; this._refs = new Map(); @@ -415,9 +442,13 @@ let offset = 4096; const strPtr = (str) => { - let ptr = offset; - new Uint8Array(mem.buffer, offset, str.length + 1).set(encoder.encode(str + "\0")); - offset += str.length + (8 - (str.length % 8)); + const ptr = offset; + const bytes = encoder.encode(str + "\0"); + new Uint8Array(mem.buffer, offset, bytes.length).set(bytes); + offset += bytes.length; + if (offset % 8 !== 0) { + offset += 8 - (offset % 8); + } return ptr; }; diff --git a/ui/app/internal/gl/gl_js.go b/ui/app/internal/gl/gl_js.go index 885291aa..d6aff17a 100644 --- a/ui/app/internal/gl/gl_js.go +++ b/ui/app/internal/gl/gl_js.go @@ -245,9 +245,7 @@ func (f *Functions) RenderbufferStorage(target, internalformat Enum, width, heig func (f *Functions) ReadPixels(x, y, width, height int, format, ty Enum, data []byte) { f.resizeByteBuffer(len(data)) f.Ctx.Call("readPixels", x, y, width, height, int(format), int(ty), f.byteBuf) - sub := f.byteBuf.Call("subarray", 0, len(data)) - d := js.TypedArrayOf(data) - d.Call("set", sub) + js.CopyBytesToGo(data, f.byteBuf) } func (f *Functions) Scissor(x, y, width, height int32) { f.Ctx.Call("scissor", x, y, width, height) @@ -294,9 +292,7 @@ func (f *Functions) byteArrayOf(data []byte) js.Value { return js.Null() } f.resizeByteBuffer(len(data)) - s := js.TypedArrayOf(data) - f.byteBuf.Call("set", s) - s.Release() + js.CopyBytesToJS(f.byteBuf, data) return f.byteBuf }