wa-lang.org/wazero@v1.0.2/internal/wasm/memory_definition.go (about) 1 package wasm 2 3 import "wa-lang.org/wazero/api" 4 5 // ImportedMemories implements the same method as documented on wazero.CompiledModule. 6 func (m *Module) ImportedMemories() (ret []api.MemoryDefinition) { 7 for _, d := range m.MemoryDefinitionSection { 8 if d.importDesc != nil { 9 ret = append(ret, d) 10 } 11 } 12 return 13 } 14 15 // ExportedMemories implements the same method as documented on wazero.CompiledModule. 16 func (m *Module) ExportedMemories() map[string]api.MemoryDefinition { 17 ret := map[string]api.MemoryDefinition{} 18 for _, d := range m.MemoryDefinitionSection { 19 for _, e := range d.exportNames { 20 ret[e] = d 21 } 22 } 23 return ret 24 } 25 26 // BuildMemoryDefinitions generates memory metadata that can be parsed from 27 // the module. This must be called after all validation. 28 // 29 // Note: This is exported for wazero.Runtime `CompileModule`. 30 func (m *Module) BuildMemoryDefinitions() { 31 var moduleName string 32 if m.NameSection != nil { 33 moduleName = m.NameSection.ModuleName 34 } 35 36 memoryCount := m.ImportMemoryCount() 37 if m.MemorySection != nil { 38 memoryCount++ 39 } 40 41 if memoryCount == 0 { 42 return 43 } 44 45 m.MemoryDefinitionSection = make([]*MemoryDefinition, 0, memoryCount) 46 importMemIdx := Index(0) 47 for _, i := range m.ImportSection { 48 if i.Type != ExternTypeMemory { 49 continue 50 } 51 52 m.MemoryDefinitionSection = append(m.MemoryDefinitionSection, &MemoryDefinition{ 53 importDesc: &[2]string{i.Module, i.Name}, 54 index: importMemIdx, 55 memory: i.DescMem, 56 }) 57 importMemIdx++ 58 } 59 60 if m.MemorySection != nil { 61 m.MemoryDefinitionSection = append(m.MemoryDefinitionSection, &MemoryDefinition{ 62 index: importMemIdx, 63 memory: m.MemorySection, 64 }) 65 } 66 67 for _, d := range m.MemoryDefinitionSection { 68 d.moduleName = moduleName 69 for _, e := range m.ExportSection { 70 if e.Type == ExternTypeMemory && e.Index == d.index { 71 d.exportNames = append(d.exportNames, e.Name) 72 } 73 } 74 } 75 } 76 77 // MemoryDefinition implements api.MemoryDefinition 78 type MemoryDefinition struct { 79 moduleName string 80 index Index 81 importDesc *[2]string 82 exportNames []string 83 memory *Memory 84 } 85 86 // ModuleName implements the same method as documented on api.MemoryDefinition. 87 func (f *MemoryDefinition) ModuleName() string { 88 return f.moduleName 89 } 90 91 // Index implements the same method as documented on api.MemoryDefinition. 92 func (f *MemoryDefinition) Index() uint32 { 93 return f.index 94 } 95 96 // Import implements the same method as documented on api.MemoryDefinition. 97 func (f *MemoryDefinition) Import() (moduleName, name string, isImport bool) { 98 if importDesc := f.importDesc; importDesc != nil { 99 moduleName, name, isImport = importDesc[0], importDesc[1], true 100 } 101 return 102 } 103 104 // ExportNames implements the same method as documented on api.MemoryDefinition. 105 func (f *MemoryDefinition) ExportNames() []string { 106 return f.exportNames 107 } 108 109 // Min implements the same method as documented on api.MemoryDefinition. 110 func (f *MemoryDefinition) Min() uint32 { 111 return f.memory.Min 112 } 113 114 // Max implements the same method as documented on api.MemoryDefinition. 115 func (f *MemoryDefinition) Max() (max uint32, encoded bool) { 116 max = f.memory.Max 117 encoded = f.memory.IsMaxEncoded 118 return 119 }