Skip to content

Commit 5ae93b3

Browse files
committed
wit, wit/bindgen: represent error-context as a primitive type
Mirrors bytecodealliance/wasm-tools#2060
1 parent 5f1cc39 commit 5ae93b3

File tree

5 files changed

+41
-47
lines changed

5 files changed

+41
-47
lines changed

wit/bindgen/generator.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,6 @@ func (g *generator) typeDefRep(file *gen.File, dir wit.Direction, t *wit.TypeDef
659659
return g.ownRep(file, dir, kind)
660660
case *wit.Borrow:
661661
return g.borrowRep(file, dir, kind)
662-
case *wit.ErrorContext:
663-
return g.errorContextRep(file, dir, kind)
664662
case *wit.Stream:
665663
return g.streamRep(file, dir, kind)
666664
case *wit.Future:
@@ -691,13 +689,13 @@ func (g *generator) typeRep(file *gen.File, dir wit.Direction, t wit.Type) strin
691689
// TODO: add wit.Type.BuiltIn() method?
692690
return g.typeDefRep(file, dir, t, "")
693691
case wit.Primitive:
694-
return g.primitiveRep(t)
692+
return g.primitiveRep(file, t)
695693
default:
696694
panic(fmt.Sprintf("BUG: unknown wit.Type %T", t)) // should never reach here
697695
}
698696
}
699697

700-
func (g *generator) primitiveRep(p wit.Primitive) string {
698+
func (g *generator) primitiveRep(file *gen.File, p wit.Primitive) string {
701699
switch p := p.(type) {
702700
case wit.Bool:
703701
return "bool"
@@ -725,6 +723,8 @@ func (g *generator) primitiveRep(p wit.Primitive) string {
725723
return "rune"
726724
case wit.String:
727725
return "string"
726+
case wit.ErrorContext:
727+
return file.Import(g.opts.cmPackage) + ".ErrorContext"
728728
default:
729729
panic(fmt.Sprintf("BUG: unknown wit.Primitive %T", p)) // should never reach here
730730
}
@@ -1005,10 +1005,6 @@ func (g *generator) borrowRep(file *gen.File, dir wit.Direction, b *wit.Borrow)
10051005
}
10061006
}
10071007

1008-
func (g *generator) errorContextRep(file *gen.File, dir wit.Direction, e *wit.ErrorContext) string {
1009-
return file.Import(g.opts.cmPackage) + ".ErrorContext"
1010-
}
1011-
10121008
func (g *generator) streamRep(file *gen.File, dir wit.Direction, s *wit.Stream) string {
10131009
var b strings.Builder
10141010
stringio.Write(&b, file.Import(g.opts.cmPackage), ".Stream[", g.typeRep(file, dir, s.Type), "]")
@@ -1050,7 +1046,7 @@ func (g *generator) typeDefShape(file *gen.File, dir wit.Direction, t *wit.TypeD
10501046
return g.typeRep(file, dir, t)
10511047
}
10521048
case *wit.Enum, *wit.Flags, *wit.List,
1053-
*wit.Resource, *wit.Own, *wit.Borrow, *wit.ErrorContext, *wit.Stream, *wit.Future:
1049+
*wit.Resource, *wit.Own, *wit.Borrow, *wit.Stream, *wit.Future:
10541050
// Certain types do not need custom type shapes:
10551051
// own, borrow, error-context, stream, future, enum, flags, and list
10561052
return g.typeRep(file, dir, t)
@@ -1120,8 +1116,7 @@ func (g *generator) lowerTypeDef(file *gen.File, dir wit.Direction, t *wit.TypeD
11201116
return g.lowerOption(file, dir, t, input)
11211117
case *wit.List:
11221118
return g.cmCall(file, "LowerList", input)
1123-
case *wit.Resource, *wit.Own, *wit.Borrow,
1124-
*wit.ErrorContext, *wit.Stream, *wit.Future:
1119+
case *wit.Resource, *wit.Own, *wit.Borrow, *wit.Stream, *wit.Future:
11251120
return g.cmCall(file, "Reinterpret["+g.typeRep(file, dir, flat[0])+"]", input)
11261121
default:
11271122
panic(fmt.Sprintf("BUG: unknown wit.TypeDef %T", kind)) // should never reach here
@@ -1275,6 +1270,8 @@ func (g *generator) lowerPrimitive(file *gen.File, dir wit.Direction, p wit.Prim
12751270
switch p := p.(type) {
12761271
case wit.String:
12771272
return g.cmCall(file, "LowerString", input)
1273+
case wit.ErrorContext:
1274+
return g.cmCall(file, "Reinterpret["+g.typeRep(file, dir, flat[0])+"]", input)
12781275
default:
12791276
return g.cast(file, dir, p, flat[0], input)
12801277
}
@@ -1334,8 +1331,7 @@ func (g *generator) liftTypeDef(file *gen.File, dir wit.Direction, t *wit.TypeDe
13341331
return g.liftOption(file, dir, t, input)
13351332
case *wit.List:
13361333
return g.cmCall(file, "LiftList["+g.typeRep(file, dir, t)+"]", input)
1337-
case *wit.Resource, *wit.Own, *wit.Borrow,
1338-
*wit.ErrorContext, *wit.Stream, *wit.Future:
1334+
case *wit.Resource, *wit.Own, *wit.Borrow, *wit.Stream, *wit.Future:
13391335
return g.cmCall(file, "Reinterpret["+g.typeRep(file, dir, t)+"]", input)
13401336
default:
13411337
panic(fmt.Sprintf("BUG: unknown wit.TypeDef %T", kind)) // should never reach here
@@ -1485,6 +1481,8 @@ func (g *generator) liftPrimitive(file *gen.File, dir wit.Direction, t wit.Type,
14851481
switch p.(type) {
14861482
case wit.String:
14871483
return g.cmCall(file, "LiftString["+g.typeRep(file, dir, t)+"]", input)
1484+
case wit.ErrorContext:
1485+
return g.cmCall(file, "Reinterpret["+g.typeRep(file, dir, t)+"]", input)
14881486
default:
14891487
return g.cast(file, dir, flat[0], t, input)
14901488
}

wit/codec.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ func (c *typeDefKindCodec) DecodeString(s string) error {
326326
*c.v = &Resource{}
327327
case "errorcontext", // https://github.com/bytecodealliance/wasm-tools/pull/1964
328328
"error-context":
329-
*c.v = &ErrorContext{}
329+
*c.v = ErrorContext{}
330330
}
331331
return nil
332332
}
@@ -577,6 +577,12 @@ func (f *Function) DecodeField(dec codec.Decoder, name string) error {
577577
return dec.Decode(&f.Kind)
578578
case "params":
579579
return codec.DecodeSlice(dec, &f.Params)
580+
case "result":
581+
// Multiple function results were removed in this PR:
582+
// https://github.com/bytecodealliance/wasm-tools/pull/2050/files
583+
// For now, this package preserves the underlying ability to support > 1 result.
584+
f.Results = []Param{{}}
585+
return dec.Decode(&f.Results[0].Type)
580586
case "results":
581587
return codec.DecodeSlice(dec, &f.Results)
582588
case "stability":

wit/error.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

wit/type.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ func ParseType(s string) (Type, error) {
7171
return Char{}, nil
7272
case "string":
7373
return String{}, nil
74+
case "error-context":
75+
return &ErrorContext{}, nil
7476
}
7577
return nil, fmt.Errorf("unknown primitive type %q", s)
7678
}
@@ -79,12 +81,15 @@ func ParseType(s string) (Type, error) {
7981
//
8082
// [primitive types]: https://component-model.bytecodealliance.org/design/wit.html#primitive-types
8183
type primitive interface {
82-
bool | int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64 | float32 | float64 | char | string
84+
bool | int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64 | float32 | float64 | char | string | errorContext
8385
}
8486

8587
// char is defined because [rune] is an alias of [int32]
8688
type char rune
8789

90+
// errorContext is defined because WIT error-context is a primitive type in the Component Model.
91+
type errorContext uint32
92+
8893
// Primitive is the interface implemented by WIT [primitive types].
8994
// It also conforms to the [Node], [ABI], [Type], and [TypeDefKind] interfaces.
9095
//
@@ -144,7 +149,7 @@ func (_primitive[T]) hasPointer() bool {
144149
func (_primitive[T]) Flat() []Type {
145150
var v T
146151
switch any(v).(type) {
147-
case bool, int8, uint8, int16, uint16, int32, uint32, char:
152+
case bool, int8, uint8, int16, uint16, int32, uint32, char, errorContext:
148153
return []Type{U32{}}
149154
case int64, uint64:
150155
return []Type{U64{}}
@@ -263,3 +268,9 @@ type Char struct{ _primitive[char] }
263268
// [primitive type]: https://component-model.bytecodealliance.org/design/wit.html#primitive-types
264269
// [string]: https://pkg.go.dev/builtin#string
265270
type String struct{ _primitive[string] }
271+
272+
// ErrorContext represents a WIT [error-context] type.
273+
// It implements the [Node], [ABI], and [TypeDefKind] interfaces.
274+
//
275+
// [resource type]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md#error-context-type
276+
type ErrorContext struct{ _primitive[errorContext] }

wit/wit.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -693,12 +693,12 @@ func (b *Borrow) WIT(ctx Node, name string) string {
693693
}
694694

695695
// WITKind returns the WIT kind.
696-
func (*ErrorContext) WITKind() string { return "error-context" }
696+
func (ErrorContext) WITKind() string { return "error-context" }
697697

698698
// WIT returns the [WIT] text format for [ErrorContext] r.
699699
//
700700
// [WIT]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md
701-
func (*ErrorContext) WIT(_ Node, name string) string {
701+
func (ErrorContext) WIT(_ Node, name string) string {
702702
var b strings.Builder
703703
if name != "" {
704704
b.WriteString("type ")
@@ -962,9 +962,12 @@ func (s *Stream) WIT(_ Node, name string) string {
962962
b.WriteString(escape(name))
963963
b.WriteString(" = ")
964964
}
965-
b.WriteString("stream<")
966-
b.WriteString(s.Type.WIT(s, ""))
967-
b.WriteRune('>')
965+
b.WriteString("stream")
966+
if s.Type != nil {
967+
b.WriteRune('<')
968+
b.WriteString(s.Type.WIT(s, ""))
969+
b.WriteRune('>')
970+
}
968971
return b.String()
969972
}
970973

@@ -1001,6 +1004,8 @@ func (_primitive[T]) WITKind() string {
10011004
return "char"
10021005
case string:
10031006
return "string"
1007+
case errorContext:
1008+
return "error-context"
10041009
default:
10051010
panic(fmt.Sprintf("BUG: unknown primitive type %T", v)) // should never reach here
10061011
}

0 commit comments

Comments
 (0)