wa-lang.org/wazero@v1.0.2/internal/platform/buf_writer.go (about)

     1  package platform
     2  
     3  // bufWriter implements io.Writer.
     4  //
     5  // This is implemented because bytes.Buffer cannot write from the beginning of the underlying buffer
     6  // without changing the memory location. In this case, the underlying buffer is memory-mapped region,
     7  // and we have to write into that region via io.Copy since sometimes the original native code exists
     8  // as a file for external-cached cases.
     9  type bufWriter struct {
    10  	underlying []byte
    11  	pos        int
    12  }
    13  
    14  // Write implements io.Writer Write.
    15  func (b *bufWriter) Write(p []byte) (n int, err error) {
    16  	copy(b.underlying[b.pos:], p)
    17  	n = len(p)
    18  	b.pos += n
    19  	return
    20  }