bytes.go raw

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