1 package wasm
2
3 import (
4 "testing"
5
6 "github.com/tetratelabs/wazero/api"
7 "github.com/tetratelabs/wazero/internal/testing/require"
8 )
9
10 func TestModule_BuildMemoryDefinitions(t *testing.T) {
11 tests := []struct {
12 name string
13 m *Module
14 expected []MemoryDefinition
15 expectedImports []api.MemoryDefinition
16 expectedExports map[string]api.MemoryDefinition
17 }{
18 {
19 name: "no exports",
20 m: &Module{},
21 expectedExports: map[string]api.MemoryDefinition{},
22 },
23 {
24 name: "no memories",
25 m: &Module{
26 ExportSection: []Export{{Type: ExternTypeGlobal, Index: 0}},
27 GlobalSection: []Global{{}},
28 },
29 expectedExports: map[string]api.MemoryDefinition{},
30 },
31 {
32 name: "defines memory{0,}",
33 m: &Module{MemorySection: &Memory{Min: 0}},
34 expected: []MemoryDefinition{{index: 0, memory: &Memory{Min: 0}}},
35 expectedExports: map[string]api.MemoryDefinition{},
36 },
37 {
38 name: "exports defined memory{2,3}",
39 m: &Module{
40 ExportSection: []Export{
41 {Name: "memory_index=0", Type: ExternTypeMemory, Index: 0},
42 {Name: "", Type: ExternTypeGlobal, Index: 0},
43 },
44 GlobalSection: []Global{{}},
45 MemorySection: &Memory{Min: 2, Max: 3, IsMaxEncoded: true},
46 },
47 expected: []MemoryDefinition{
48 {
49 index: 0,
50 exportNames: []string{"memory_index=0"},
51 memory: &Memory{Min: 2, Max: 3, IsMaxEncoded: true},
52 },
53 },
54 expectedExports: map[string]api.MemoryDefinition{
55 "memory_index=0": &MemoryDefinition{
56 index: 0,
57 exportNames: []string{"memory_index=0"},
58 memory: &Memory{Min: 2, Max: 3, IsMaxEncoded: true},
59 },
60 },
61 },
62 {
63 name: "exports imported memory{0,} and defined memory{2,3}",
64 m: &Module{
65 ImportSection: []Import{{
66 Type: ExternTypeMemory,
67 DescMem: &Memory{Min: 0},
68 }},
69 ExportSection: []Export{
70 {Name: "imported_memory", Type: ExternTypeMemory, Index: 0},
71 {Name: "memory_index=1", Type: ExternTypeMemory, Index: 1},
72 },
73 MemorySection: &Memory{Min: 2, Max: 3, IsMaxEncoded: true},
74 },
75 expected: []MemoryDefinition{
76 {
77 index: 0,
78 importDesc: &[2]string{"", ""},
79 exportNames: []string{"imported_memory"},
80 memory: &Memory{Min: 0},
81 },
82 {
83 index: 1,
84 exportNames: []string{"memory_index=1"},
85 memory: &Memory{Min: 2, Max: 3, IsMaxEncoded: true},
86 },
87 },
88 expectedImports: []api.MemoryDefinition{
89 &MemoryDefinition{
90 index: 0,
91 importDesc: &[2]string{"", ""},
92 exportNames: []string{"imported_memory"},
93 memory: &Memory{Min: 0},
94 },
95 },
96 expectedExports: map[string]api.MemoryDefinition{
97 "imported_memory": &MemoryDefinition{
98 index: 0,
99 importDesc: &[2]string{"", ""},
100 exportNames: []string{"imported_memory"},
101 memory: &Memory{Min: 0},
102 },
103 "memory_index=1": &MemoryDefinition{
104 index: 1,
105 exportNames: []string{"memory_index=1"},
106 memory: &Memory{Min: 2, Max: 3, IsMaxEncoded: true},
107 },
108 },
109 },
110 }
111
112 for _, tc := range tests {
113 tc := tc
114 t.Run(tc.name, func(t *testing.T) {
115 tc.m.BuildMemoryDefinitions()
116 require.Equal(t, tc.expected, tc.m.MemoryDefinitionSection)
117 require.Equal(t, tc.expectedImports, tc.m.ImportedMemories())
118 require.Equal(t, tc.expectedExports, tc.m.ExportedMemories())
119 })
120 }
121 }
122
View as plain text