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

Commit

Permalink
calculate number of imported funcs
Browse files Browse the repository at this point in the history
* calculate number of imported funcs

* add a test to check function's name
  • Loading branch information
张志强 authored Apr 18, 2020
1 parent dc6758c commit 1b43961
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
12 changes: 11 additions & 1 deletion wasm/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,17 @@ func (m *Module) populateFunctions() error {
}

// Add the functions from the wasm itself to the function list
numImports := len(m.FunctionIndexSpace)

// when imported module not provided, len(m.FunctionIndecSpace) == 0
// so must calculate number of imported funcs
var numImports int
if m.Import != nil {
for _, importEntry := range m.Import.Entries {
if importEntry.Type.Kind() == ExternalFunction {
numImports++
}
}
}
for codeIndex, typeIndex := range m.Function.Types {
if int(typeIndex) >= len(m.Types.Entries) {
return InvalidFunctionIndexError(typeIndex)
Expand Down
47 changes: 47 additions & 0 deletions wasm/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,50 @@ func TestGetFuntionSig(t *testing.T) {
}

}

func TestFunctionName(t *testing.T) {
f, err := os.Open("testdata/hello-world-tinygo.wasm")
if err != nil {
t.Fatalf("%v", err)
}

defer f.Close()
m, err := wasm.ReadModule(f, nil)
if err != nil {
t.Fatalf("error reading module %v", err)
}

var names wasm.NameMap
if s := m.Custom(wasm.CustomSectionName); s != nil {
var nSec wasm.NameSection
err := nSec.UnmarshalWASM(bytes.NewReader(s.Data))
if err != nil {
t.Fatalf("error Unmarhsal NameSection %v", err)
}
if len(nSec.Types[wasm.NameFunction]) > 0 {
sub, err := nSec.Decode(wasm.NameFunction)
if err != nil {
t.Fatalf("error Decode NameFunction %v", err)
}
funcs, ok := sub.(*wasm.FunctionNames)
if ok {
names = funcs.Names
}
}
}

var numImports int
if m.Import != nil {
for _, importEntry := range m.Import.Entries {
if importEntry.Type.Kind() == wasm.ExternalFunction {
numImports++
}
}
}

for index, function := range m.FunctionIndexSpace {
if function.Name != names[uint32(index+numImports)] {
t.Fatalf("Err:function name expect %s, got %s", names[uint32(index+numImports)], function.Name)
}
}
}

0 comments on commit 1b43961

Please sign in to comment.