github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/wasm/counts.go (about) 1 package wasm 2 3 import "fmt" 4 5 // SectionElementCount returns the count of elements in a given section ID 6 // 7 // For example... 8 // * SectionIDType returns the count of FunctionType 9 // * SectionIDCustom returns the count of CustomSections plus one if NameSection is present 10 // * SectionIDHostFunction returns the count of HostFunctionSection 11 // * SectionIDExport returns the count of unique export names 12 func (m *Module) SectionElementCount(sectionID SectionID) uint32 { // element as in vector elements! 13 switch sectionID { 14 case SectionIDCustom: 15 numCustomSections := uint32(len(m.CustomSections)) 16 if m.NameSection != nil { 17 numCustomSections++ 18 } 19 return numCustomSections 20 case SectionIDType: 21 return uint32(len(m.TypeSection)) 22 case SectionIDImport: 23 return uint32(len(m.ImportSection)) 24 case SectionIDFunction: 25 return uint32(len(m.FunctionSection)) 26 case SectionIDTable: 27 return uint32(len(m.TableSection)) 28 case SectionIDMemory: 29 if m.MemorySection != nil { 30 return 1 31 } 32 return 0 33 case SectionIDGlobal: 34 return uint32(len(m.GlobalSection)) 35 case SectionIDExport: 36 return uint32(len(m.ExportSection)) 37 case SectionIDStart: 38 if m.StartSection != nil { 39 return 1 40 } 41 return 0 42 case SectionIDElement: 43 return uint32(len(m.ElementSection)) 44 case SectionIDCode: 45 return uint32(len(m.CodeSection)) 46 case SectionIDData: 47 return uint32(len(m.DataSection)) 48 default: 49 panic(fmt.Errorf("BUG: unknown section: %d", sectionID)) 50 } 51 }