syscall/js: allow copyBytesTo(Go|JS) to use Uint8ClampedArray

closes #38011

Change-Id: Ic50f2f27456dccdc3fca1bda076871af1eb81705
Reviewed-on: https://go-review.googlesource.com/c/go/+/224638
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Richard Musiol <neelance@gmail.com>
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Aurélio A. Heckert 2020-03-22 20:38:42 -03:00 committed by Richard Musiol
parent 6a9d850b82
commit f0e8b81aa3
2 changed files with 8 additions and 8 deletions

View File

@ -447,7 +447,7 @@
"syscall/js.copyBytesToGo": (sp) => { "syscall/js.copyBytesToGo": (sp) => {
const dst = loadSlice(sp + 8); const dst = loadSlice(sp + 8);
const src = loadValue(sp + 32); const src = loadValue(sp + 32);
if (!(src instanceof Uint8Array)) { if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
this.mem.setUint8(sp + 48, 0); this.mem.setUint8(sp + 48, 0);
return; return;
} }
@ -461,7 +461,7 @@
"syscall/js.copyBytesToJS": (sp) => { "syscall/js.copyBytesToJS": (sp) => {
const dst = loadValue(sp + 8); const dst = loadValue(sp + 8);
const src = loadSlice(sp + 16); const src = loadSlice(sp + 16);
if (!(dst instanceof Uint8Array)) { if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
this.mem.setUint8(sp + 48, 0); this.mem.setUint8(sp + 48, 0);
return; return;
} }

View File

@ -565,28 +565,28 @@ func (e *ValueError) Error() string {
return "syscall/js: call of " + e.Method + " on " + e.Type.String() return "syscall/js: call of " + e.Method + " on " + e.Type.String()
} }
// CopyBytesToGo copies bytes from the Uint8Array src to dst. // CopyBytesToGo copies bytes from src to dst.
// It panics if src is not an Uint8Array or Uint8ClampedArray.
// It returns the number of bytes copied, which will be the minimum of the lengths of src and dst. // It returns the number of bytes copied, which will be the minimum of the lengths of src and dst.
// CopyBytesToGo panics if src is not an Uint8Array.
func CopyBytesToGo(dst []byte, src Value) int { func CopyBytesToGo(dst []byte, src Value) int {
n, ok := copyBytesToGo(dst, src.ref) n, ok := copyBytesToGo(dst, src.ref)
runtime.KeepAlive(src) runtime.KeepAlive(src)
if !ok { if !ok {
panic("syscall/js: CopyBytesToGo: expected src to be an Uint8Array") panic("syscall/js: CopyBytesToGo: expected src to be an Uint8Array or Uint8ClampedArray")
} }
return n return n
} }
func copyBytesToGo(dst []byte, src ref) (int, bool) func copyBytesToGo(dst []byte, src ref) (int, bool)
// CopyBytesToJS copies bytes from src to the Uint8Array dst. // CopyBytesToJS copies bytes from src to dst.
// It panics if dst is not an Uint8Array or Uint8ClampedArray.
// It returns the number of bytes copied, which will be the minimum of the lengths of src and dst. // It returns the number of bytes copied, which will be the minimum of the lengths of src and dst.
// CopyBytesToJS panics if dst is not an Uint8Array.
func CopyBytesToJS(dst Value, src []byte) int { func CopyBytesToJS(dst Value, src []byte) int {
n, ok := copyBytesToJS(dst.ref, src) n, ok := copyBytesToJS(dst.ref, src)
runtime.KeepAlive(dst) runtime.KeepAlive(dst)
if !ok { if !ok {
panic("syscall/js: CopyBytesToJS: expected dst to be an Uint8Array") panic("syscall/js: CopyBytesToJS: expected dst to be an Uint8Array or Uint8ClampedArray")
} }
return n return n
} }