Skip to content

Commit bb6b875

Browse files
committed
wit/bindgen: add WIT generation for each WIT interface (Go package)
Currently disabled.
1 parent 0466234 commit bb6b875

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

wit/bindgen/generator.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ type typeUse struct {
8383
}
8484

8585
type generator struct {
86-
opts options
87-
res *wit.Resolve
86+
opts options
87+
res *wit.Resolve
88+
world *wit.World // optional, can be nil
8889

8990
// versioned is set to true if there are multiple versions of a WIT package in res,
9091
// which affects the generated Go package paths.
@@ -152,9 +153,25 @@ func newGenerator(res *wit.Resolve, opts ...Option) (*generator, error) {
152153
g.opts.cmPackage = cmPackage
153154
}
154155
g.res = res
156+
for _, g.world = range res.Worlds {
157+
if g.world.Match(g.opts.world) {
158+
break
159+
}
160+
// otherwise chose the last world
161+
}
155162
return g, nil
156163
}
157164

165+
// TODO: factor this out
166+
func findWorld(r *wit.Resolve, pattern string) *wit.World {
167+
for _, w := range r.Worlds {
168+
if w.Match(pattern) {
169+
return w
170+
}
171+
}
172+
return nil
173+
}
174+
158175
func (g *generator) generate() ([]*gen.Package, error) {
159176
g.detectVersionedPackages()
160177
err := g.defineWorlds()
@@ -205,8 +222,8 @@ func (g *generator) define(dir wit.Direction, v wit.Node) (defined bool) {
205222
// WIT interfaces and/or worlds into a single Go package.
206223
func (g *generator) defineWorlds() error {
207224
g.opts.logger.Infof("Generating Go for %d world(s)\n", len(g.res.Worlds))
208-
for i, w := range g.res.Worlds {
209-
if w.Match(g.opts.world) || (g.opts.world == "" && i == len(g.res.Worlds)-1) {
225+
for _, w := range g.res.Worlds {
226+
if w == g.world || g.world == nil {
210227
err := g.defineWorld(w)
211228
if err != nil {
212229
return err
@@ -299,8 +316,13 @@ func (g *generator) defineInterface(w *wit.World, dir wit.Direction, i *wit.Inte
299316
if err != nil {
300317
return err
301318
}
302-
file := g.fileFor(i)
303319

320+
// Write WIT file for this interface
321+
// Disabled until we write metadata .wasm file
322+
// witFile := g.witFileFor(i)
323+
// witFile.WriteString(g.res.WIT(wit.Filter(g.world, i), ""))
324+
325+
file := g.fileFor(i)
304326
{
305327
var b strings.Builder
306328
stringio.Write(&b, "Package ", pkg.Name, " represents the ", dir.String(), " ", i.WITKind(), " \"", g.moduleNames[i], "\".\n")

0 commit comments

Comments
 (0)