Skip to content

Commit

Permalink
bindgen, wit: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ydnar committed Nov 2, 2023
1 parent d56520f commit 0d72040
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
63 changes: 57 additions & 6 deletions bindgen/bindgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ func Go(res *wit.Resolve, opts ...Option) ([]*gen.Package, error) {
return nil, err
}

// By default, each WIT interface and world maps to a single Go package.
// Options might override the Go package, including combining multiple
// WIT interfaces and/or worlds into a single Go package.
for _, w := range g.res.Worlds {
id := worldIdent(w)
fmt.Printf("%s → %s\n", id, id)
fmt.Printf(HeaderPattern, g.opts.generator)
fmt.Println()
err = g.generateWorlds()
if err != nil {
return nil, err
}

return g.finish()
Expand Down Expand Up @@ -66,6 +65,58 @@ func (g *generator) finish() ([]*gen.Package, error) {
return packages, nil
}

// By default, each WIT interface and world maps to a single Go package.
// Options might override the Go package, including combining multiple
// WIT interfaces and/or worlds into a single Go package.
func (g *generator) generateWorlds() error {
fmt.Printf("Generating Go for %d world(s)\n\n", len(g.res.Worlds))
for _, w := range g.res.Worlds {
g.generateWorld(w)
}
return nil
}

func (g *generator) generateWorld(w *wit.World) error {
fmt.Println(w.Path())
for _, name := range codec.SortedKeys(w.Imports) {
var err error
switch v := w.Imports[name].(type) {
case *wit.Interface:
err = g.generateImportedInterface(w, v, name)
case *wit.TypeDef:
err = g.generateImportedTypeDef(w, v, name)
case *wit.Function:
err = g.generateImportedFunction(w, v)
}
if err != nil {
return err
}
}
fmt.Println()
return nil
}

func (g *generator) generateImportedInterface(w *wit.World, i *wit.Interface, name string) error {
if i.Name != nil {
name = *i.Name
}
fmt.Printf("\timport interface %s/%s\n", w.Path(), name)
return nil
}

func (g *generator) generateImportedTypeDef(w *wit.World, t *wit.TypeDef, name string) error {
if t.Name != nil {
name = *t.Name
}
fmt.Printf("\timport type %s.%s\n", w.Path(), name)
return nil
}

func (g *generator) generateImportedFunction(w *wit.World, f *wit.Function) error {
fmt.Printf("\timport func %s.%s\n", w.Path(), f.Name)
return nil
}

// packageForWorld returns a Go package for [wit.World] w.
// It attempts to map the WIT package and world name to a Go package path,
// using the mappings in g.opts if present.
Expand Down
5 changes: 5 additions & 0 deletions wit/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ type World struct {
_typeOwner
}

// Path returns the fully-qualified path for this world, such as "wasi:clocks/imports".
func (w *World) Path() string {
return w.Package.Name.String() + "/" + w.Name
}

// AllFunctions [iterates] through all functions exported from or imported into a [World],
// calling yield for each. Iteration will stop if yield returns false.
//
Expand Down

0 comments on commit 0d72040

Please sign in to comment.