bytes.go 804 B

123456789101112131415161718192021222324252627
  1. //go:build js && wasm
  2. package safejs
  3. import (
  4. "syscall/js"
  5. "github.com/hack-pad/safejs/internal/catch"
  6. )
  7. // CopyBytesToGo copies bytes from src to dst.
  8. // Returns the number of bytes copied, which is the minimum of the lengths of src and dst.
  9. // Returns an error if src is not an Uint8Array or Uint8ClampedArray.
  10. func CopyBytesToGo(dst []byte, src Value) (int, error) {
  11. return catch.Try(func() int {
  12. return js.CopyBytesToGo(dst, src.jsValue)
  13. })
  14. }
  15. // CopyBytesToJS copies bytes from src to dst.
  16. // Returns the number of bytes copied, which is the minimum of the lengths of src and dst.
  17. // Returns an error if dst is not an Uint8Array or Uint8ClampedArray.
  18. func CopyBytesToJS(dst Value, src []byte) (int, error) {
  19. return catch.Try(func() int {
  20. return js.CopyBytesToJS(dst.jsValue, src)
  21. })
  22. }