Skip to content

Commit

Permalink
Merge pull request #219 from bytecodealliance/ydnar/issue215
Browse files Browse the repository at this point in the history
  • Loading branch information
ydnar authored Oct 29, 2024
2 parents d9f65b7 + 81116a3 commit 5ebdb70
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- [#215](https://github.com/bytecodealliance/wasm-tools-go/issues/215): generate variant accessor methods with the correct scope for Go names.

## [v0.3.0] — 2024-10-11

### Added
Expand Down
13 changes: 13 additions & 0 deletions testdata/issues/issue215.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package issues:issue215;

interface i {
variant var {
var(u8),
}

foo: func(var: var);
}

world w {
import i;
}
67 changes: 67 additions & 0 deletions testdata/issues/issue215.wit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"worlds": [
{
"name": "w",
"imports": {
"interface-0": {
"interface": {
"id": 0
}
}
},
"exports": {},
"package": 0
}
],
"interfaces": [
{
"name": "i",
"types": {
"var": 0
},
"functions": {
"foo": {
"name": "foo",
"kind": "freestanding",
"params": [
{
"name": "var",
"type": 0
}
],
"results": []
}
},
"package": 0
}
],
"types": [
{
"name": "var",
"kind": {
"variant": {
"cases": [
{
"name": "var",
"type": "u8"
}
]
}
},
"owner": {
"interface": 0
}
}
],
"packages": [
{
"name": "issues:issue215",
"interfaces": {
"i": 0
},
"worlds": {
"w": 0
}
}
]
}
10 changes: 10 additions & 0 deletions testdata/issues/issue215.wit.json.golden.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package issues:issue215;

interface i {
variant var { var(u8) }
foo: func(var: var);
}

world w {
import i;
}
21 changes: 11 additions & 10 deletions wit/bindgen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,7 @@ func (g *generator) typeDir(dir wit.Direction, t wit.Type) (tdir wit.Direction,
}

func (g *generator) typeDefRep(file *gen.File, dir wit.Direction, t *wit.TypeDef, goName string) string {
return g.typeDefKindRep(file, dir, t.Kind, goName)
}

func (g *generator) typeDefKindRep(file *gen.File, dir wit.Direction, kind wit.TypeDefKind, goName string) string {
switch kind := kind.(type) {
switch kind := t.Kind.(type) {
case *wit.Pointer:
return g.pointerRep(file, dir, kind)
case wit.Type:
Expand All @@ -597,7 +593,7 @@ func (g *generator) typeDefKindRep(file *gen.File, dir wit.Direction, kind wit.T
case *wit.Enum:
return g.enumRep(file, dir, kind, goName)
case *wit.Variant:
return g.variantRep(file, dir, kind, goName)
return g.variantRep(file, dir, t, goName)
case *wit.Result:
return g.resultRep(file, dir, kind)
case *wit.Option:
Expand Down Expand Up @@ -713,7 +709,7 @@ func (g *generator) tupleRep(file *gen.File, dir wit.Direction, t *wit.Tuple, go
stringio.Write(&b, "[", strconv.Itoa(len(t.Types)), "]", g.typeRep(file, dir, typ))
} else if len(t.Types) == 0 || len(t.Types) > cm.MaxTuple {
// Force struct representation
return g.typeDefKindRep(file, dir, t.Despecialize(), goName)
return g.recordRep(file, dir, t.Despecialize().(*wit.Record), goName)
} else {
stringio.Write(&b, file.Import(g.opts.cmPackage), ".Tuple")
if len(t.Types) > 2 {
Expand Down Expand Up @@ -805,7 +801,10 @@ func (g *generator) enumRep(file *gen.File, dir wit.Direction, e *wit.Enum, goNa
return b.String()
}

func (g *generator) variantRep(file *gen.File, dir wit.Direction, v *wit.Variant, goName string) string {
// variantRep is special because it accepts a [wit.TypeDef] instead of a [wit.Variant].
// The TypeDef is used to determine the scope for generating accessor methods on the type.
func (g *generator) variantRep(file *gen.File, dir wit.Direction, t *wit.TypeDef, goName string) string {
v := t.Kind.(*wit.Variant)
// If the variant has no associated types, represent the variant as an enum.
if e := v.Enum(); e != nil {
return g.enumRep(file, dir, e, goName)
Expand All @@ -822,7 +821,8 @@ func (g *generator) variantRep(file *gen.File, dir wit.Direction, v *wit.Variant
typeShape = g.typeShape(file, dir, shape)
}

scope := gen.NewScope(file)
decl, _ := g.typeDecl(dir, t)
scope := decl.scope
scope.DeclareName("String") // For fmt.Stringer

// Emit type
Expand Down Expand Up @@ -1111,6 +1111,7 @@ func (g *generator) lowerFlags(file *gen.File, dir wit.Direction, t *wit.TypeDef
}

func (g *generator) lowerVariant(file *gen.File, dir wit.Direction, t *wit.TypeDef, input string) string {
decl, _ := g.typeDecl(dir, t)
v := t.Kind.(*wit.Variant)
flat := t.Flat()
if v.Enum() != nil {
Expand All @@ -1125,7 +1126,7 @@ func (g *generator) lowerVariant(file *gen.File, dir wit.Direction, t *wit.TypeD
continue
}
caseNum := strconv.Itoa(i)
caseName := GoName(c.Name, true)
caseName := decl.scope.GetName(GoName(c.Name, true))
stringio.Write(&b, "case ", caseNum, ": // ", c.Name, "\n")
b.WriteString(g.lowerVariantCaseInto(abiFile, dir, c.Type, flat[1:], "*v."+caseName+"()"))
}
Expand Down

0 comments on commit 5ebdb70

Please sign in to comment.