github.com/taubyte/vm-wasm-utils@v1.0.2/binary/memory.go (about)

     1  package binary
     2  
     3  import (
     4  	"bytes"
     5  
     6  	wasm "github.com/taubyte/vm-wasm-utils"
     7  )
     8  
     9  // decodeMemory returns the api.Memory decoded with the WebAssembly 1.0 (20191205) Binary Format.
    10  //
    11  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-memory
    12  func decodeMemory(
    13  	r *bytes.Reader,
    14  	memorySizer func(minPages uint32, maxPages *uint32) (min, capacity, max uint32),
    15  ) (*wasm.Memory, error) {
    16  	min, maxP, err := decodeLimitsType(r)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  
    21  	min, capacity, max := memorySizer(min, maxP)
    22  	mem := &wasm.Memory{Min: min, Cap: capacity, Max: max, IsMaxEncoded: maxP != nil}
    23  
    24  	return mem, mem.Validate()
    25  }
    26  
    27  // encodeMemory returns the wasm.Memory encoded in WebAssembly 1.0 (20191205) Binary Format.
    28  //
    29  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-memory
    30  func encodeMemory(i *wasm.Memory) []byte {
    31  	maxPtr := &i.Max
    32  	if !i.IsMaxEncoded {
    33  		maxPtr = nil
    34  	}
    35  	return encodeLimitsType(i.Min, maxPtr)
    36  }