diff --git a/cmd/wasm-dump/main.go b/cmd/wasm-dump/main.go index 19e90078..388a9a24 100644 --- a/cmd/wasm-dump/main.go +++ b/cmd/wasm-dump/main.go @@ -82,7 +82,42 @@ func process(w io.Writer, fname string) { } defer f.Close() - m, err := wasm.ReadModule(f, nil) + // Decode the module in order to fake its imports as exports + mod, err := wasm.DecodeModule(f) + if err != nil { + log.Fatalf("Could not read module: %v", err) + } + + resolver := func(name string) (*wasm.Module, error) { + m := wasm.NewModule() + m.Types = mod.Types + m.Export = &wasm.SectionExports{ + Entries: make(map[string]wasm.ExportEntry), + } + m.FunctionIndexSpace = []wasm.Function{} + for index, imp := range mod.Import.Entries { + fmt.Println(imp.ModuleName, imp.FieldName, imp.Type) + if imp.ModuleName == name { + m.Export.Entries[imp.FieldName] = wasm.ExportEntry{ + FieldStr: name, + Kind: imp.Type.Kind(), + Index: uint32(index), + } + if imp.Type.Kind() == wasm.ExternalFunction { + m.FunctionIndexSpace = append(m.FunctionIndexSpace, wasm.Function{ + Body: &wasm.FunctionBody{}, + }) + } + } + } + return m, nil + } + + // We already read the file without trying to resolve imports, + // start over in order to do that work now. + f.Seek(0, os.SEEK_SET) + + m, err := wasm.ReadModule(f, resolver) if err != nil { log.Fatalf("could not read module: %v", err) } diff --git a/disasm/disasm.go b/disasm/disasm.go index 743c19c9..8d148ed8 100644 --- a/disasm/disasm.go +++ b/disasm/disasm.go @@ -9,6 +9,7 @@ import ( "bytes" "encoding/binary" "errors" + "fmt" "io" "math" @@ -97,6 +98,14 @@ func NewDisassembly(fn wasm.Function, module *wasm.Module) (*Disassembly, error) } disas := &Disassembly{} + if module.Import != nil { + ninternal, _ := leb128.ReadVarint32(bytes.NewReader(module.Code.Bytes)) + nexternal := len(module.FunctionIndexSpace) - int(ninternal) + if nexternal != len(module.Import.Entries) { + panic(fmt.Sprintf("Attempting to disassemble a module that is missing imports %d != %d", nexternal, len(module.Import.Entries))) + } + } + // A stack of int arrays holding indices to instructions that make the stack // polymorphic. Each block has its corresponding array. We start with one // array for the root stack