Skip to content

Commit

Permalink
wit/bindgen: generate .wasm.syso files in each Go package
Browse files Browse the repository at this point in the history
Currently stubbed out (if false). Depends on tinygo-org/tinygo#4593
  • Loading branch information
ydnar committed Nov 30, 2024
1 parent fdd3c77 commit d0dd8dd
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions wit/bindgen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"go.bytecodealliance.org/internal/codec"
"go.bytecodealliance.org/internal/go/gen"
"go.bytecodealliance.org/internal/stringio"
"go.bytecodealliance.org/internal/wasm"
"go.bytecodealliance.org/wit"
"go.bytecodealliance.org/wit/logging"
)
Expand Down Expand Up @@ -2327,34 +2328,45 @@ func (g *generator) newPackage(w *wit.World, i *wit.Interface, name string) (*ge
// Component Model types and functions imported into and/or exported
// from this Go package.
if false {
// Synthesize a unique-ish library name
lib := id.String()
// Synthesize a unique-ish name
worldName := id.String()
if name != id.Extension {
lib += "-" + name
}
lib = strings.ReplaceAll(lib, "/", "-")
lib = strings.ReplaceAll(lib, ":", "-")
libFile := pkg.File("lib" + lib + ".a")

// Generate
witText := g.res.WIT(wit.Filter(w, i), "")
witFile := g.witFileFor(owner)
witFile.WriteString(witText)
worldName += "-" + name
}
worldName += "-go"
worldName = replacer.Replace(worldName)
// libFile := pkg.File("lib" + lib + ".a")
sysoFile := pkg.File(worldName + ".wasm.syso")

// Generate wasm file
world := w.Clone()
world.Name = worldName
w.Package.Worlds.Set(worldName, world)
witText := g.res.WIT(wit.Filter(world, i), "")
w.Package.Worlds.Delete(worldName)
// witFile := g.witFileFor(owner)
// witFile.WriteString(witText)
content, err := g.componentEmbed(witText)
if err != nil {
// return nil, err
return nil, err
}
libFile.Write(content)
componentType := &wasm.CustomSection{
Name: "component-type:" + worldName,
Contents: content,
}
wasm.Write(sysoFile, []wasm.Section{&wasm.LinkingSection{}, componentType})

// Write Cgo file
cgoFile := g.cgoFileFor(owner)
stringio.Write(cgoFile, "// #cgo LDFLAGS: -L. -l", lib, "\n")
stringio.Write(cgoFile, "import \"C\"\n")
// cgoFile := g.cgoFileFor(owner)
// stringio.Write(cgoFile, "// #cgo LDFLAGS: -L. -l", lib, "\n")
// stringio.Write(cgoFile, "import \"C\"\n")
}

return pkg, nil
}

var replacer = strings.NewReplacer("/", "-", ":", "-", "@", "-v", ".", "")

// componentEmbed runs generated WIT through wasm-tools to generate a wasm file with a component-type custom section.
func (g *generator) componentEmbed(witData string) ([]byte, error) {
// TODO: --all-features?
Expand Down

0 comments on commit d0dd8dd

Please sign in to comment.