@@ -1804,17 +1804,19 @@ func (g *generator) defineImportedFunction(decl *funcDecl) error {
18041804 b .WriteString ("}\n \n " )
18051805
18061806 // Emit wasmimport function
1807- stringio .Write (& b , "//go:wasmimport " , decl .linkerName , "\n " )
1808- b .WriteString ("//go:noescape\n " )
1809- b .WriteString ("func " )
1807+ wasmFile := g .wasmFileFor (decl .owner )
1808+
1809+ stringio .Write (wasmFile , "//go:wasmimport " , decl .linkerName , "\n " )
1810+ wasmFile .WriteString ("//go:noescape\n " )
1811+ wasmFile .WriteString ("func " )
18101812 if decl .wasmFunc .isMethod () {
1811- stringio .Write (& b , "(" , decl .wasmFunc .receiver .name , " " , g .typeRep (file , decl .wasmFunc .receiver .dir , decl .wasmFunc .receiver .typ ), ") " , decl .wasmFunc .name )
1813+ stringio .Write (wasmFile , "(" , decl .wasmFunc .receiver .name , " " , g .typeRep (wasmFile , decl .wasmFunc .receiver .dir , decl .wasmFunc .receiver .typ ), ") " , decl .wasmFunc .name )
18121814 } else {
1813- b .WriteString (decl .wasmFunc .name )
1815+ wasmFile .WriteString (decl .wasmFunc .name )
18141816 }
1815- b .WriteString (g .functionSignature (file , decl .wasmFunc ))
1817+ wasmFile .WriteString (g .functionSignature (wasmFile , decl .wasmFunc ))
18161818
1817- b .WriteString ("\n \n " )
1819+ wasmFile .WriteString ("\n \n " )
18181820
18191821 // Emit shared types
18201822 if t , ok := compoundParams .typ .(* wit.TypeDef ); ok {
@@ -1884,84 +1886,84 @@ func (g *generator) defineExportedFunction(decl *funcDecl) error {
18841886 stringio .Write (xfile , decl .goFunc .name , " func" , g .functionSignature (xfile , decl .goFunc ), "\n " )
18851887 }
18861888
1887- var b bytes. Buffer
1889+ wasmFile := g . wasmFileFor ( decl . owner )
18881890
18891891 // Emit wasmexport function
1890- stringio .Write (& b , "//go:wasmexport " , decl .linkerName , "\n " )
1891- stringio .Write (& b , "//export " , decl .linkerName , "\n " ) // TODO: remove this once TinyGo supports go:wasmexport.
1892- stringio .Write (& b , "func " , decl .wasmFunc .name , g .functionSignature (file , decl .wasmFunc ))
1892+ stringio .Write (wasmFile , "//go:wasmexport " , decl .linkerName , "\n " )
1893+ stringio .Write (wasmFile , "//export " , decl .linkerName , "\n " ) // TODO: remove this once TinyGo supports go:wasmexport.
1894+ stringio .Write (wasmFile , "func " , decl .wasmFunc .name , g .functionSignature (wasmFile , decl .wasmFunc ))
18931895
18941896 // Emit function body
1895- b .WriteString (" {\n " )
1897+ wasmFile .WriteString (" {\n " )
18961898
18971899 // Lift arguments
18981900 if compoundParams .typ == nil {
18991901 i := 0
19001902 for _ , p := range callParams {
19011903 if i < len (decl .wasmFunc .params ) && p .typ == derefPointer (decl .wasmFunc .params [i ].typ ) {
1902- stringio .Write (& b , p .name , " := *" , decl .wasmFunc .params [i ].name , "\n " )
1904+ stringio .Write (wasmFile , p .name , " := *" , decl .wasmFunc .params [i ].name , "\n " )
19031905 i ++
19041906 continue
19051907 }
19061908 flat := p .typ .Flat ()
19071909 var input string
19081910 if len (flat ) > 0 && len (decl .wasmFunc .params ) > 0 {
1909- input = g .liftTypeInput (file , p .dir , p .typ , decl .wasmFunc .params [i :i + len (flat )])
1911+ input = g .liftTypeInput (wasmFile , p .dir , p .typ , decl .wasmFunc .params [i :i + len (flat )])
19101912 }
1911- stringio .Write (& b , p .name , " := " , g .liftType (file , p .dir , p .typ , input ), "\n " )
1913+ stringio .Write (wasmFile , p .name , " := " , g .liftType (wasmFile , p .dir , p .typ , input ), "\n " )
19121914 i += len (flat )
19131915 }
19141916 }
19151917
19161918 // Emit call to caller-defined Go function
19171919 if compoundResults .typ != nil {
19181920 rec := wit.KindOf [* wit.Record ](compoundResults .typ )
1919- stringio .Write (& b , compoundResults .name , " = new(" , g .typeRep (file , compoundResults .dir , compoundResults .typ ), ")\n " )
1921+ stringio .Write (wasmFile , compoundResults .name , " = new(" , g .typeRep (wasmFile , compoundResults .dir , compoundResults .typ ), ")\n " )
19201922 for i , f := range rec .Fields {
19211923 if i > 0 {
1922- b .WriteString (", " )
1924+ wasmFile .WriteString (", " )
19231925 }
1924- stringio .Write (& b , compoundResults .name , "." , fieldName (f .Name , false ))
1926+ stringio .Write (wasmFile , compoundResults .name , "." , fieldName (f .Name , false ))
19251927 }
1926- b .WriteString (" = " )
1928+ wasmFile .WriteString (" = " )
19271929 } else if len (callResults ) > 0 {
19281930 for i , r := range callResults {
19291931 if i > 0 {
1930- b .WriteString (", " )
1932+ wasmFile .WriteString (", " )
19311933 }
1932- b .WriteString (r .name )
1934+ wasmFile .WriteString (r .name )
19331935 }
1934- b .WriteString (" := " )
1936+ wasmFile .WriteString (" := " )
19351937 }
19361938
19371939 // Emit caller-defined function name
19381940 fqName := file .GetName ("Exports" ) + "." + decl .goFunc .name
19391941 if t := decl .f .Type (); t != nil {
19401942 fqName = file .GetName ("Exports" ) + "." + scope .GetName (GoName (t .TypeName (), true )) + "." + decl .goFunc .name
19411943 }
1942- stringio .Write (& b , fqName , "(" )
1944+ stringio .Write (wasmFile , fqName , "(" )
19431945
19441946 // Emit call params
19451947 if compoundParams .typ != nil {
19461948 rec := wit.KindOf [* wit.Record ](compoundParams .typ )
19471949 for i , f := range rec .Fields {
19481950 if i > 0 {
1949- b .WriteString (", " )
1951+ wasmFile .WriteString (", " )
19501952 }
1951- stringio .Write (& b , compoundParams .name , "." , fieldName (f .Name , false ))
1953+ stringio .Write (wasmFile , compoundParams .name , "." , fieldName (f .Name , false ))
19521954 }
19531955 } else {
19541956 for i , p := range callParams {
19551957 if i > 0 {
1956- b .WriteString (", " )
1958+ wasmFile .WriteString (", " )
19571959 }
19581960 if isPointer (p .typ ) {
1959- b . WriteRune ( '*' )
1961+ wasmFile . WriteString ( "*" )
19601962 }
1961- b .WriteString (p .name )
1963+ wasmFile .WriteString (p .name )
19621964 }
19631965 }
1964- b .WriteString (")\n " )
1966+ wasmFile .WriteString (")\n " )
19651967
19661968 // Lower results
19671969 if len (callResults ) > 0 && compoundResults .typ == nil {
@@ -1970,29 +1972,31 @@ func (g *generator) defineExportedFunction(decl *funcDecl) error {
19701972 if i < len (decl .wasmFunc .results ) {
19711973 wr := decl .wasmFunc .results [i ]
19721974 if r .typ == derefPointer (wr .typ ) {
1973- stringio .Write (& b , wr .name , " = &" , r .name , "\n " )
1975+ stringio .Write (wasmFile , wr .name , " = &" , r .name , "\n " )
19741976 i ++
19751977 continue
19761978 }
19771979 }
19781980 flat := r .typ .Flat ()
19791981 if len (flat ) == 0 {
1980- stringio .Write (& b , "_ = " , r .name , "\n " )
1982+ stringio .Write (wasmFile , "_ = " , r .name , "\n " )
19811983 } else {
19821984 for j := range flat {
19831985 if j > 0 {
1984- b .WriteString (", " )
1986+ wasmFile .WriteString (", " )
19851987 }
1986- b .WriteString (decl .wasmFunc .results [i ].name )
1988+ wasmFile .WriteString (decl .wasmFunc .results [i ].name )
19871989 i ++
19881990 }
1989- stringio .Write (& b , " = " , g .lowerType (file , dir , r .typ , r .name ), "\n " )
1991+ stringio .Write (wasmFile , " = " , g .lowerType (wasmFile , dir , r .typ , r .name ), "\n " )
19901992 }
19911993 }
19921994 }
19931995
1994- b .WriteString ("return\n " )
1995- b .WriteString ("}\n \n " )
1996+ wasmFile .WriteString ("return\n " )
1997+ wasmFile .WriteString ("}\n \n " )
1998+
1999+ var b bytes.Buffer
19962000
19972001 // Emit default function body
19982002 if strings .HasPrefix (decl .f .Name , "[dtor]" ) || strings .HasPrefix (decl .f .Name , "cabi_post_" ) {
@@ -2175,6 +2179,16 @@ func (g *generator) exportsFileFor(owner wit.TypeOwner) *gen.File {
21752179 return file
21762180}
21772181
2182+ func (g * generator ) wasmFileFor (owner wit.TypeOwner ) * gen.File {
2183+ pkg := g .packageFor (owner )
2184+ file := pkg .File (pkg .Name + ".wasm.go" )
2185+ file .GeneratedBy = g .opts .generatedBy
2186+ if len (file .Header ) == 0 {
2187+ file .Header = fmt .Sprintf ("// This file contains wasmimport and wasmexport declarations for \" %s\" .\n \n " , owner .WITPackage ().Name .String ())
2188+ }
2189+ return file
2190+ }
2191+
21782192func (g * generator ) packageFor (owner wit.TypeOwner ) * gen.Package {
21792193 return g .witPackages [owner ]
21802194}
0 commit comments