From 146357c298a185321c739ce65e7e4032ed8f0895 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 15 Dec 2020 01:09:40 +0900 Subject: [PATCH] jsutil: Enable to specify length at Uint8ArrayToSlice --- internal/graphicsdriver/opengl/context_js.go | 10 ++++++---- internal/jsutil/slice_js.go | 7 +++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/internal/graphicsdriver/opengl/context_js.go b/internal/graphicsdriver/opengl/context_js.go index f7b3166d9..530756e26 100644 --- a/internal/graphicsdriver/opengl/context_js.go +++ b/internal/graphicsdriver/opengl/context_js.go @@ -201,10 +201,11 @@ func (c *context) framebufferPixels(f *framebuffer, width, height int) []byte { c.bindFramebuffer(f.native) - p := jsutil.TemporaryUint8Array(4 * width * height) + l := 4 * width * height + p := jsutil.TemporaryUint8Array(l) gl.Call("readPixels", 0, 0, width, height, gles.RGBA, gles.UNSIGNED_BYTE, p) - return jsutil.Uint8ArrayToSlice(p) + return jsutil.Uint8ArrayToSlice(p, l) } func (c *context) framebufferPixelsToBuffer(f *framebuffer, buffer buffer, width, height int) { @@ -574,8 +575,9 @@ func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, he func (c *context) getBufferSubData(buffer buffer, width, height int) []byte { gl := c.gl gl.Call("bindBuffer", gles.PIXEL_UNPACK_BUFFER, buffer) - arr := jsutil.TemporaryUint8Array(4 * width * height) + l := 4 * width * height + arr := jsutil.TemporaryUint8Array(l) gl.Call("getBufferSubData", gles.PIXEL_UNPACK_BUFFER, 0, arr) gl.Call("bindBuffer", gles.PIXEL_UNPACK_BUFFER, 0) - return jsutil.Uint8ArrayToSlice(arr) + return jsutil.Uint8ArrayToSlice(arr, l) } diff --git a/internal/jsutil/slice_js.go b/internal/jsutil/slice_js.go index d4ace466c..6946cf619 100644 --- a/internal/jsutil/slice_js.go +++ b/internal/jsutil/slice_js.go @@ -22,8 +22,11 @@ import ( "unsafe" ) -func Uint8ArrayToSlice(value js.Value) []byte { - s := make([]byte, value.Get("byteLength").Int()) +func Uint8ArrayToSlice(value js.Value, length int) []byte { + if l := value.Get("byteLength").Int(); length > l { + length = l + } + s := make([]byte, length) js.CopyBytesToGo(s, value) return s }