Skip to content

Commit d0dd8dd

Browse files
committed
wit/bindgen: generate .wasm.syso files in each Go package
Currently stubbed out (if false). Depends on tinygo-org/tinygo#4593
1 parent fdd3c77 commit d0dd8dd

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

wit/bindgen/generator.go

+29-17
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"go.bytecodealliance.org/internal/codec"
2222
"go.bytecodealliance.org/internal/go/gen"
2323
"go.bytecodealliance.org/internal/stringio"
24+
"go.bytecodealliance.org/internal/wasm"
2425
"go.bytecodealliance.org/wit"
2526
"go.bytecodealliance.org/wit/logging"
2627
)
@@ -2327,34 +2328,45 @@ func (g *generator) newPackage(w *wit.World, i *wit.Interface, name string) (*ge
23272328
// Component Model types and functions imported into and/or exported
23282329
// from this Go package.
23292330
if false {
2330-
// Synthesize a unique-ish library name
2331-
lib := id.String()
2331+
// Synthesize a unique-ish name
2332+
worldName := id.String()
23322333
if name != id.Extension {
2333-
lib += "-" + name
2334-
}
2335-
lib = strings.ReplaceAll(lib, "/", "-")
2336-
lib = strings.ReplaceAll(lib, ":", "-")
2337-
libFile := pkg.File("lib" + lib + ".a")
2338-
2339-
// Generate
2340-
witText := g.res.WIT(wit.Filter(w, i), "")
2341-
witFile := g.witFileFor(owner)
2342-
witFile.WriteString(witText)
2334+
worldName += "-" + name
2335+
}
2336+
worldName += "-go"
2337+
worldName = replacer.Replace(worldName)
2338+
// libFile := pkg.File("lib" + lib + ".a")
2339+
sysoFile := pkg.File(worldName + ".wasm.syso")
2340+
2341+
// Generate wasm file
2342+
world := w.Clone()
2343+
world.Name = worldName
2344+
w.Package.Worlds.Set(worldName, world)
2345+
witText := g.res.WIT(wit.Filter(world, i), "")
2346+
w.Package.Worlds.Delete(worldName)
2347+
// witFile := g.witFileFor(owner)
2348+
// witFile.WriteString(witText)
23432349
content, err := g.componentEmbed(witText)
23442350
if err != nil {
2345-
// return nil, err
2351+
return nil, err
23462352
}
2347-
libFile.Write(content)
2353+
componentType := &wasm.CustomSection{
2354+
Name: "component-type:" + worldName,
2355+
Contents: content,
2356+
}
2357+
wasm.Write(sysoFile, []wasm.Section{&wasm.LinkingSection{}, componentType})
23482358

23492359
// Write Cgo file
2350-
cgoFile := g.cgoFileFor(owner)
2351-
stringio.Write(cgoFile, "// #cgo LDFLAGS: -L. -l", lib, "\n")
2352-
stringio.Write(cgoFile, "import \"C\"\n")
2360+
// cgoFile := g.cgoFileFor(owner)
2361+
// stringio.Write(cgoFile, "// #cgo LDFLAGS: -L. -l", lib, "\n")
2362+
// stringio.Write(cgoFile, "import \"C\"\n")
23532363
}
23542364

23552365
return pkg, nil
23562366
}
23572367

2368+
var replacer = strings.NewReplacer("/", "-", ":", "-", "@", "-v", ".", "")
2369+
23582370
// componentEmbed runs generated WIT through wasm-tools to generate a wasm file with a component-type custom section.
23592371
func (g *generator) componentEmbed(witData string) ([]byte, error) {
23602372
// TODO: --all-features?

0 commit comments

Comments
 (0)