@@ -83,8 +83,9 @@ type typeUse struct {
83
83
}
84
84
85
85
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
88
89
89
90
// versioned is set to true if there are multiple versions of a WIT package in res,
90
91
// which affects the generated Go package paths.
@@ -152,9 +153,25 @@ func newGenerator(res *wit.Resolve, opts ...Option) (*generator, error) {
152
153
g .opts .cmPackage = cmPackage
153
154
}
154
155
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
+ }
155
162
return g , nil
156
163
}
157
164
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
+
158
175
func (g * generator ) generate () ([]* gen.Package , error ) {
159
176
g .detectVersionedPackages ()
160
177
err := g .defineWorlds ()
@@ -205,8 +222,8 @@ func (g *generator) define(dir wit.Direction, v wit.Node) (defined bool) {
205
222
// WIT interfaces and/or worlds into a single Go package.
206
223
func (g * generator ) defineWorlds () error {
207
224
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 {
210
227
err := g .defineWorld (w )
211
228
if err != nil {
212
229
return err
@@ -299,8 +316,13 @@ func (g *generator) defineInterface(w *wit.World, dir wit.Direction, i *wit.Inte
299
316
if err != nil {
300
317
return err
301
318
}
302
- file := g .fileFor (i )
303
319
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 )
304
326
{
305
327
var b strings.Builder
306
328
stringio .Write (& b , "Package " , pkg .Name , " represents the " , dir .String (), " " , i .WITKind (), " \" " , g .moduleNames [i ], "\" .\n " )
0 commit comments