1package binary
2
3import (
4 "bytes"
5 "fmt"
6
7 "github.com/tetratelabs/wazero/api"
8 "github.com/tetratelabs/wazero/experimental"
9 "github.com/tetratelabs/wazero/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
15func 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
31 // This restriction may be lifted in the future.
32 // https://webassembly.github.io/threads/core/binary/types.html#memory-types
33 if maxP == nil {
34 return nil, fmt.Errorf("shared memory requires a maximum size to be specified")
35 }
36 }
37
38 min, capacity, max := memorySizer(min, maxP)
39 mem := &wasm.Memory{Min: min, Cap: capacity, Max: max, IsMaxEncoded: maxP != nil, IsShared: shared}
40
41 return mem, mem.Validate(memoryLimitPages)
42}