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

Commit 55a1639

Browse files
justincliftsbinet
authored andcommitted
wasm: when function names are present, include them
* wasm: when function names are present, include them * wasm: ensure exports are written in a stable order
1 parent 633bd83 commit 55a1639

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

wasm/index.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package wasm
66

77
import (
8+
"bytes"
89
"fmt"
910
"reflect"
1011
)
@@ -38,14 +39,43 @@ func (m *Module) populateFunctions() error {
3839
return nil
3940
}
4041

42+
// If present, extract the function names from the custom 'name' section
43+
var names NameMap
44+
if s := m.Custom(CustomSectionName); s != nil {
45+
var nSec NameSection
46+
err := nSec.UnmarshalWASM(bytes.NewReader(s.Data))
47+
if err != nil {
48+
return err
49+
}
50+
if len(nSec.Types[NameFunction]) > 0 {
51+
sub, err := nSec.Decode(NameFunction)
52+
if err != nil {
53+
return err
54+
}
55+
funcs, ok := sub.(*FunctionNames)
56+
if ok {
57+
names = funcs.Names
58+
}
59+
}
60+
}
61+
62+
// If available, fill in the name field for the imported functions
63+
for i := range m.FunctionIndexSpace {
64+
m.FunctionIndexSpace[i].Name = names[uint32(i)]
65+
}
66+
67+
// Add the functions from the wasm itself to the function list
68+
numImports := len(m.FunctionIndexSpace)
4169
for codeIndex, typeIndex := range m.Function.Types {
4270
if int(typeIndex) >= len(m.Types.Entries) {
4371
return InvalidFunctionIndexError(typeIndex)
4472
}
4573

74+
// Create the main function structure
4675
fn := Function{
4776
Sig: &m.Types.Entries[typeIndex],
4877
Body: &m.Code.Bodies[codeIndex],
78+
Name: names[uint32(codeIndex+numImports)], // Add the name string if we have it
4979
}
5080

5181
m.FunctionIndexSpace = append(m.FunctionIndexSpace, fn)

wasm/module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Function struct {
2424
Sig *FunctionSig
2525
Body *FunctionBody
2626
Host reflect.Value
27+
Name string
2728
}
2829

2930
// IsHost indicates whether this function is a host function as defined in:
@@ -159,7 +160,6 @@ func ReadModule(r io.Reader, resolvePath ResolveFunc) (*Module, error) {
159160
if err := fn(); err != nil {
160161
return nil, err
161162
}
162-
163163
}
164164

165165
logger.Printf("There are %d entries in the function index space.", len(m.FunctionIndexSpace))

wasm/section.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,11 @@ func (s *SectionExports) WritePayload(w io.Writer) error {
615615
entries = append(entries, e)
616616
}
617617
sort.Slice(entries, func(i, j int) bool {
618+
// If the Index # is the same, fall back to string comparing the field name. This should ensure a
619+
// deterministic sort order for the exports occurs, when run on the same .wasm file multiple times
620+
if entries[i].Index == entries[j].Index {
621+
return entries[i].FieldStr < entries[j].FieldStr
622+
}
618623
return entries[i].Index < entries[j].Index
619624
})
620625
for _, e := range entries {
718 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)