github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/wasm/binary/memory.go (about)

     1  package binary
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/wasilibs/wazerox/api"
     8  	"github.com/wasilibs/wazerox/experimental"
     9  	"github.com/wasilibs/wazerox/internal/wasm"
    10  )
    11  
    12  // decodeMemory returns the api.Memory decoded with the WebAssembly 1.0 (20191205) Binary Format.
    13  //
    14  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-memory
    15  func decodeMemory(
    16  	r *bytes.Reader,
    17  	enabledFeatures api.CoreFeatures,
    18  	memorySizer func(minPages uint32, maxPages *uint32) (min, capacity, max uint32),
    19  	memoryLimitPages uint32,
    20  ) (*wasm.Memory, error) {
    21  	min, maxP, shared, err := decodeLimitsType(r)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	if shared {
    27  		if !enabledFeatures.IsEnabled(experimental.CoreFeaturesThreads) {
    28  			return nil, fmt.Errorf("shared memory requested but threads feature not enabled")
    29  		}
    30  		// This restriction may be lifted in the future.
    31  		// https://webassembly.github.io/threads/core/binary/types.html#memory-types
    32  		if maxP == nil {
    33  			return nil, fmt.Errorf("shared memory requires a maximum size to be specified")
    34  		}
    35  	}
    36  
    37  	min, capacity, max := memorySizer(min, maxP)
    38  	mem := &wasm.Memory{Min: min, Cap: capacity, Max: max, IsMaxEncoded: maxP != nil, IsShared: shared}
    39  
    40  	return mem, mem.Validate(memoryLimitPages)
    41  }