Skip to content
This repository has been archived by the owner on May 11, 2020. It is now read-only.

Commit

Permalink
wasm: add a helper to find custom section by name
Browse files Browse the repository at this point in the history
  • Loading branch information
Denys Smirnov authored and sbinet committed Aug 8, 2018
1 parent d06cd44 commit 461df7e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cmd/wasm-dump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func printHeaders(w io.Writer, fname string, m *wasm.Module) {
len(sec.Entries),
)
}
for _, sec := range m.Other {
for _, sec := range m.Customs {
fmt.Fprintf(w, "%9s start=0x%08x end=0x%08x (size=0x%08x) %q\n",
sec.ID.String(),
sec.Start, sec.End, len(sec.Bytes),
Expand Down Expand Up @@ -336,7 +336,7 @@ func printDetails(w io.Writer, fname string, m *wasm.Module) {
fmt.Fprintf(w, "%s", hexDump(e.Data, 0))
}
}
for _, sec := range m.Other {
for _, sec := range m.Customs {
fmt.Fprintf(w, "%v:\n", sec.ID)
fmt.Fprintf(w, " - name: %q\n", sec.Name)
raw := bytes.NewReader(sec.Bytes[6:])
Expand Down
12 changes: 9 additions & 3 deletions wasm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Module struct {
Elements *SectionElements
Code *SectionCode
Data *SectionData
Other []*SectionCustom
Customs []*SectionCustom

// The function index space of the module
FunctionIndexSpace []Function
Expand All @@ -68,8 +68,14 @@ type Module struct {
}
}

func (m *Module) GetSections() []Section {
return m.Sections
// Custom returns a custom section with a specific name, if it exists.
func (m *Module) Custom(name string) *SectionCustom {
for _, s := range m.Customs {
if s.Name == name {
return s
}
}
return nil
}

// NewModule creates a new empty module
Expand Down
2 changes: 1 addition & 1 deletion wasm/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (m *Module) readSection(r *readpos.ReadPos) (bool, error) {
case SectionIDCustom:
logger.Println("section custom")
cs := &SectionCustom{}
m.Other = append(m.Other, cs)
m.Customs = append(m.Customs, cs)
sec = cs
case SectionIDType:
logger.Println("section type")
Expand Down

0 comments on commit 461df7e

Please sign in to comment.