wa-lang.org/wazero@v1.0.2/internal/wasm/binary/memory.go (about)

     1  package binary
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"wa-lang.org/wazero/internal/wasm"
     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  	memoryLimitPages uint32,
    16  ) (*wasm.Memory, error) {
    17  	min, maxP, err := decodeLimitsType(r)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  
    22  	min, capacity, max := memorySizer(min, maxP)
    23  	mem := &wasm.Memory{Min: min, Cap: capacity, Max: max, IsMaxEncoded: maxP != nil}
    24  
    25  	return mem, mem.Validate(memoryLimitPages)
    26  }
    27  
    28  // encodeMemory returns the wasm.Memory encoded in WebAssembly 1.0 (20191205) Binary Format.
    29  //
    30  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-memory
    31  func encodeMemory(i *wasm.Memory) []byte {
    32  	maxPtr := &i.Max
    33  	if !i.IsMaxEncoded {
    34  		maxPtr = nil
    35  	}
    36  	return encodeLimitsType(i.Min, maxPtr)
    37  }