diff --git a/libc-gen/main.go b/libc-gen/main.go index b2b6d2de..39c319bf 100644 --- a/libc-gen/main.go +++ b/libc-gen/main.go @@ -22,6 +22,7 @@ import ( "golang.org/x/tools/go/ast/astutil" "github.com/ncruces/wasm2go/internal/mangle" + "github.com/ncruces/wasm2go/internal/passes" ) //go:embed c go @@ -171,9 +172,15 @@ func main() { } usedImports := make(set[string]) + names := make(map[string]*ast.Ident, len(keep)) + for _, fd := range keep { + names[fd.Name.Name] = mangle.ID(fd.Name.Name, mangle.Internal) + } + callSites := make(map[*ast.Ident][]*ast.CallExpr) // Execute the rewriting permutations for _, fd := range keep { + origName := fd.Name.Name fd.Recv = &ast.FieldList{ List: []*ast.Field{{ Names: []*ast.Ident{ast.NewIdent("m")}, @@ -205,8 +212,10 @@ func main() { x.X = makeMemoryExpr(*deref) } case *ast.CallExpr: - if id, ok := x.Fun.(*ast.Ident); ok && available[id.Name] != nil { - x.Fun = &ast.SelectorExpr{X: ast.NewIdent("m"), Sel: mangle.ID(id.Name, mangle.Internal)} + if id, ok := x.Fun.(*ast.Ident); ok && names[id.Name] != nil { + sel := names[id.Name] + x.Fun = &ast.SelectorExpr{X: ast.NewIdent("m"), Sel: sel} + callSites[sel] = append(callSites[sel], x) } for i, arg := range x.Args { if id, ok := arg.(*ast.Ident); ok && id.Name == "memory" { @@ -231,7 +240,19 @@ func main() { }) // Finally mangle the function's own name - fd.Name = mangle.ID(fd.Name.Name, mangle.Internal) + fd.Name = names[origName] + } + + for changed := true; changed; { + changed = false + for _, fd := range keep { + if fd.Recv != nil && passes.RemoveReceiver(fd) { + for _, call := range callSites[fd.Name] { + call.Fun = fd.Name + } + changed = true + } + } } // Write the unified file diff --git a/translate.go b/translate.go index f309d7c4..ac668edf 100644 --- a/translate.go +++ b/translate.go @@ -64,7 +64,7 @@ type translator struct { out ast.File // Dependencies. packages set[string] - provided set[string] + provided map[string]ast.Expr helpers set[string] // Sections. types []funcType @@ -94,7 +94,7 @@ func translate(r io.Reader, w io.Writer) error { fset := token.NewFileSet() t.packages = set[string]{} - t.provided = set[string]{} + t.provided = map[string]ast.Expr{} t.helpers = set[string]{} helperNames, err := t.findHelpers(fset, helpersSrc, helpersAtomicsSrc) @@ -108,13 +108,15 @@ func translate(r io.Reader, w io.Writer) error { } for _, decl := range f.Decls { if fn, ok := decl.(*ast.FuncDecl); ok { - // Check if the receiver type is *Module. + // Check if the receiver type is *Module, or if fn is an unexported function. if fn.Recv != nil { if star, ok := fn.Recv.List[0].Type.(*ast.StarExpr); ok { if id, ok := star.X.(*ast.Ident); ok && id.Name == "Module" { - t.provided.add(fn.Name.Name) + t.provided[fn.Name.Name] = latecall(newID(fn.Name.Name)) } } + } else if !ast.IsExported(fn.Name.Name) { + t.provided[fn.Name.Name] = newID(fn.Name.Name) } // Provided files may call helpers by name. ast.Inspect(fn.Body, func(n ast.Node) bool { @@ -469,11 +471,11 @@ func (t *translator) readImportSection() error { } typ := t.types[index] - if n := mangle.Name(name, mangle.Internal); t.provided.has(n) { + if n := mangle.Name(name, mangle.Internal); t.provided[n] != nil { id := ast.NewIdent(n) fn := funcCompiler{ typ: typ, - call: latecall(id), + call: t.provided[n], decl: &ast.FuncDecl{Name: id}, provided: true} t.functions = append(t.functions, fn) diff --git a/translate_test.go b/translate_test.go index 403a3419..794c4fc9 100644 --- a/translate_test.go +++ b/translate_test.go @@ -70,6 +70,51 @@ func Test_translate_provided(t *testing.T) { } } +func Test_provided_receiverless(t *testing.T) { + prov := filepath.Join(t.TempDir(), "provided.go") + const provSrc = `package wasm2go +import "math" +func _sin(x float64) float64 { return math.Sin(x) } +func _inc(x int32) int32 { return x + 1 } +func Exported(x int32) int32 { return x } +` + if err := os.WriteFile(prov, []byte(provSrc), 0644); err != nil { + t.Fatal(err) + } + provided = stringFlags{prov} + t.Cleanup(func() { provided = nil }) + + // (module + // (import "env" "sin" (func $sin (param f64) (result f64))) + // (import "env" "inc" (func $inc (param i32) (result i32))) + // (table 1 funcref) + // (elem (i32.const 0) $sin) + // (func (export "f") (param f64 i32) (result f64) + // local.get 0 call $sin + // local.get 1 call $inc f64.convert_i32_s f64.add)) + wasm := []byte{ + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, + 0x01, 0x11, 0x03, 0x60, 0x01, 0x7c, 0x01, 0x7c, 0x60, 0x01, 0x7f, 0x01, 0x7f, 0x60, 0x02, 0x7c, 0x7f, 0x01, 0x7c, + 0x02, 0x15, 0x02, 0x03, 0x65, 0x6e, 0x76, 0x03, 0x73, 0x69, 0x6e, 0x00, 0x00, 0x03, 0x65, 0x6e, 0x76, 0x03, 0x69, 0x6e, 0x63, 0x00, 0x01, + 0x03, 0x02, 0x01, 0x02, + 0x04, 0x04, 0x01, 0x70, 0x00, 0x01, + 0x07, 0x05, 0x01, 0x01, 0x66, 0x00, 0x02, + 0x09, 0x07, 0x01, 0x00, 0x41, 0x00, 0x0b, 0x01, 0x00, + 0x0a, 0x0e, 0x01, 0x0c, 0x00, 0x20, 0x00, 0x10, 0x00, 0x20, 0x01, 0x10, 0x01, 0xb7, 0xa0, 0x0b, + } + + var out bytes.Buffer + if err := translate(bytes.NewReader(wasm), &out); err != nil { + t.Fatal(err) + } + got := out.String() + for _, want := range []string{"{_sin}", "_sin(v0)", "_inc(v1)"} { + if !strings.Contains(got, want) { + t.Errorf("missing %q in:\n%s", want, got) + } + } +} + func Test_translateSpecTest(t *testing.T) { name := *pkg *nanbox = true