From 4e05a45b856f13ac9a49746f83c0ace2d9e73d7f Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Thu, 6 Jun 2024 16:40:06 -0700 Subject: [PATCH 01/15] .github/workflows: update wasm-tools to 1.209.1 --- .github/workflows/test.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 4e69d210..7074f80f 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -43,7 +43,7 @@ jobs: - name: Set up wasm-tools uses: bytecodealliance/actions/wasm-tools/setup@v1.1.0 with: - version: v1.205.0 + version: v1.209.1 - name: Set up Go uses: actions/setup-go@v5.0.1 @@ -82,7 +82,7 @@ jobs: - name: Set up wasm-tools uses: bytecodealliance/actions/wasm-tools/setup@v1.1.0 with: - version: v1.205.0 + version: v1.209.1 - name: Set up Go uses: actions/setup-go@v5.0.1 @@ -119,7 +119,7 @@ jobs: - name: Set up wasm-tools uses: bytecodealliance/actions/wasm-tools/setup@v1.1.0 with: - version: v1.205.0 + version: v1.209.1 - name: Set up Wasmtime uses: bytecodealliance/actions/wasmtime/setup@v1.1.0 From cf5063c90d9332f3a1a372063a1941dc277747cc Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Fri, 7 Jun 2024 23:40:58 -0400 Subject: [PATCH 02/15] add Makefile to simplify generation of WIT JSON files --- Makefile | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..c249cd80 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +wit_files = $(sort $(shell find testdata -name '*.wit' ! -name '*.golden.*')) + +.PHONY: json +json: $(wit_files) + +.PHONY: $(wit_files) +$(wit_files): + wasm-tools component wit -j $@ > $@.json From 699f4d9565a744053a1a08ed23b71bf750315747 Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Fri, 7 Jun 2024 23:45:37 -0400 Subject: [PATCH 03/15] wit, wit/bindgen: add InterfaceStability and Stability types TODO: thread Stability into Function and TypeDef. --- wit/bindgen/generator.go | 10 +++++---- wit/codec.go | 37 +++++++++++++++++++++++++++++-- wit/docs.go | 2 +- wit/resolve.go | 41 ++++++++++++++++++++++++++++++++--- wit/wit.go | 47 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 126 insertions(+), 11 deletions(-) diff --git a/wit/bindgen/generator.go b/wit/bindgen/generator.go index b26a15dd..45cec74b 100644 --- a/wit/bindgen/generator.go +++ b/wit/bindgen/generator.go @@ -251,8 +251,9 @@ func (g *generator) defineWorld(w *wit.World) error { var err error w.Imports.All()(func(name string, v wit.WorldItem) bool { switch v := v.(type) { - case *wit.Interface: - err = g.defineInterface(wit.Imported, v, name) + case *wit.InterfaceStability: + // TODO: handle Stability + err = g.defineInterface(wit.Imported, v.Interface, name) case *wit.TypeDef: err = g.defineTypeDef(wit.Imported, v, name) case *wit.Function: @@ -268,8 +269,9 @@ func (g *generator) defineWorld(w *wit.World) error { w.Exports.All()(func(name string, v wit.WorldItem) bool { switch v := v.(type) { - case *wit.Interface: - err = g.defineInterface(wit.Exported, v, name) + case *wit.InterfaceStability: + // TODO: handle Stability + err = g.defineInterface(wit.Exported, v.Interface, name) case *wit.TypeDef: // WIT does not currently allow worlds to export types. err = errors.New("exported type in world " + w.Name) diff --git a/wit/codec.go b/wit/codec.go index 0f8e4643..43515573 100644 --- a/wit/codec.go +++ b/wit/codec.go @@ -39,6 +39,8 @@ func (res *Resolve) ResolveCodec(v any) codec.Codec { return &functionKindCodec{v} case *Handle: return &handleCodec{v} + case *Stability: + return &stabilityCodec{v} case *Type: return &typeCodec{v, res} case *TypeDefKind: @@ -200,6 +202,18 @@ func (pn *Ident) DecodeString(s string) error { return err } +// DecodeField implements the [codec.FieldDecoder] interface +// to decode a struct or JSON object. +func (i *InterfaceStability) DecodeField(dec codec.Decoder, name string) error { + switch name { + case "id": + return dec.Decode(&i.Interface) + case "stability": + return dec.Decode(&i.Stability) + } + return nil +} + // DecodeField implements the [codec.FieldDecoder] interface // to decode a struct or JSON object. func (d *Docs) DecodeField(dec codec.Decoder, name string) error { @@ -220,8 +234,8 @@ func (c *worldItemCodec) DecodeField(dec codec.Decoder, name string) error { var err error switch name { case "interface": - var v *Interface - err = dec.Decode(&v) + v := &InterfaceStability{} + err = dec.Decode(v) *c.v = v case "function": var v *Function @@ -444,6 +458,25 @@ func (c *handleCodec) DecodeField(dec codec.Decoder, name string) error { return err } +type stabilityCodec struct { + v *Stability +} + +func (c *stabilityCodec) DecodeField(dec codec.Decoder, name string) error { + var err error + switch name { + case "stable": + v := &Stable{} + err = dec.Decode(v) + *c.v = v + case "unstable": + v := &Unstable{} + err = dec.Decode(v) + *c.v = v + } + return err +} + // DecodeField implements the [codec.FieldDecoder] interface // to decode a struct or JSON object. func (e *Enum) DecodeField(dec codec.Decoder, name string) error { diff --git a/wit/docs.go b/wit/docs.go index 4dd7542f..c77927db 100644 --- a/wit/docs.go +++ b/wit/docs.go @@ -10,7 +10,7 @@ // // Types that are represented as Rust enums are implemented via sealed Go interfaces, implemented by other types // in this package. An example is [WorldItem], which is the set of types that a [World] may -// import or export, currently [Interface], [TypeDef], and [Function]. +// import or export, currently [InterfaceStability], [TypeDef], and [Function]. // // # JSON // diff --git a/wit/resolve.go b/wit/resolve.go index d51c33df..1f0466b7 100644 --- a/wit/resolve.go +++ b/wit/resolve.go @@ -8,6 +8,7 @@ import ( "strings" "unsafe" + "github.com/coreos/go-semver/semver" "github.com/ydnar/wasm-tools-go/wit/iterate" "github.com/ydnar/wasm-tools-go/wit/ordered" ) @@ -89,7 +90,7 @@ func (w *World) AllFunctions() iterate.Seq[*Function] { } // A WorldItem is any item that can be exported from or imported into a [World], -// currently either an [Interface], [TypeDef], or [Function]. +// currently either an [InterfaceStability], [TypeDef], or [Function]. // Any WorldItem is also a [Node]. type WorldItem interface { Node @@ -101,15 +102,23 @@ type _worldItem struct{} func (_worldItem) isWorldItem() {} +// An InterfaceStability represents a reference to an [Interface] with a [Stability] attribute. +// It implements the [Node] and [WorldItem] interfaces. +type InterfaceStability struct { + _worldItem + + Interface *Interface + Stability Stability +} + // An Interface represents a [collection of types and functions], which are imported into // or exported from a [WebAssembly component]. -// It implements the [Node], [TypeOwner], and [WorldItem] interfaces. +// It implements the [Node], and [TypeOwner] interfaces. // // [collection of types and functions]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md#wit-interfaces. // [WebAssembly component]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md#wit-worlds type Interface struct { _typeOwner - _worldItem Name *string TypeDefs ordered.Map[string, *TypeDef] @@ -1445,6 +1454,32 @@ type Package struct { Docs Docs } +// Stability represents the version or feature-gated stability of a given feature. +type Stability interface { + Node + isStability() +} + +// _stability is an embeddable type that conforms to the [Stability] interface. +type _stability struct{} + +func (_stability) isStability() {} + +// Stable represents a stable WIT feature, for example: @since(version = 1.2.3) +// +// Stable features have an explicit since version and an optional feature name. +type Stable struct { + _stability + Since semver.Version + Feature string +} + +// Unstable represents an unstable WIT feature defined by name. +type Unstable struct { + _stability + Feature string +} + // Docs represent WIT documentation text extracted from comments. type Docs struct { Contents string // may be empty diff --git a/wit/wit.go b/wit/wit.go index 65ff4c8a..0683c97b 100644 --- a/wit/wit.go +++ b/wit/wit.go @@ -66,6 +66,38 @@ func (r *Resolve) WIT(_ Node, _ string) string { return b.String() } +// WITKind returns the WIT kind. +func (*Stable) WITKind() string { return "@since" } + +// WIT returns the [WIT] text format for [Stable] s. +// +// [WIT]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md +func (s *Stable) WIT(_ Node, _ string) string { + var b strings.Builder + b.WriteString("@since(version = ") + b.WriteString(s.Since.String()) + if s.Feature != "" { + b.WriteString(", feature = ") + b.WriteString(s.Feature) + } + b.WriteRune(')') + return b.String() +} + +// WITKind returns the WIT kind. +func (*Unstable) WITKind() string { return "@unstable" } + +// WIT returns the [WIT] text format for [Unstable] u. +// +// [WIT]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md +func (u *Unstable) WIT(_ Node, _ string) string { + var b strings.Builder + b.WriteString("@unstable(feature = ") + b.WriteString(u.Feature) + b.WriteRune(')') + return b.String() +} + // WITKind returns the WIT kind. func (*Docs) WITKind() string { return "docs" } @@ -163,7 +195,7 @@ func (w *World) WIT(ctx Node, name string) string { func (w *World) itemWIT(motion, name string, v WorldItem) string { switch v := v.(type) { - case *Interface, *Function: + case *InterfaceStability, *Function: return motion + " " + v.WIT(w, name) // TODO: handle resource methods? case *TypeDef: return v.WIT(w, name) // no motion, in Imports only @@ -171,6 +203,19 @@ func (w *World) itemWIT(motion, name string, v WorldItem) string { panic("BUG: unknown WorldItem") } +// WITKind returns the WIT kind. +func (*InterfaceStability) WITKind() string { return "interface stability" } + +// WIT returns the [WIT] text format for [InterfaceStability] i. +// +// [WIT]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md +func (i *InterfaceStability) WIT(ctx Node, name string) string { + if i.Stability == nil { + return i.Interface.WIT(ctx, name) + } + return i.Stability.WIT(ctx, "") + "\n" + i.Interface.WIT(ctx, name) +} + // WITKind returns the WIT kind. func (*Interface) WITKind() string { return "interface" } From d514f1a7e53805503a9e8b2e31448a0ba90a811b Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Fri, 7 Jun 2024 23:46:33 -0400 Subject: [PATCH 04/15] testdata: date with stability metadata; breaking JSON schema change --- testdata/escape/escaped-names.wit.json | 81 ++- .../escape/escaped-names.wit.json.golden.wit | 16 + testdata/example/aqua.wit.json | 4 +- testdata/example/exported-list.wit.json | 4 +- testdata/example/exported-resource.wit.json | 4 +- testdata/example/flat-variant.wit.json | 4 +- testdata/example/non-flat-params.wit.json | 8 +- testdata/example/post-return.wit.json | 4 +- testdata/example/resource-in-world.wit.json | 8 +- testdata/example/use-of-export.wit.json | 39 +- .../example/use-of-export.wit.json.golden.wit | 4 +- .../example/use-of-import-or-export.wit.json | 20 +- testdata/example/use-of-import.wit.json | 39 +- .../example/use-of-import.wit.json.golden.wit | 4 +- testdata/wasi/cli.wit.json | 324 ++++++++--- testdata/wasi/http.wit.json | 416 +++++++++---- testdata/wit-parser/complex-include.wit.json | 64 +- testdata/wit-parser/diamond1.wit.json | 8 +- .../wit-parser/disambiguate-diamond.wit.json | 16 +- testdata/wit-parser/feature-gates.wit.json | 288 +++++++++ .../feature-gates.wit.json.golden.wit | 37 ++ .../wit-parser/foreign-deps-union.wit.json | 60 +- testdata/wit-parser/foreign-deps.wit.json | 36 +- .../wit-parser/ignore-files-deps.wit.json | 4 +- .../import-export-overlap2.wit.json | 4 +- testdata/wit-parser/include-reps.wit.json | 16 +- testdata/wit-parser/kinds-of-deps.wit.json | 16 +- testdata/wit-parser/many-names.wit.json | 4 +- .../multi-file-multi-package.wit.json | 250 ++++++++ ...lti-file-multi-package.wit.json.golden.wit | 54 ++ testdata/wit-parser/multi-file.wit.json | 12 +- .../multi-package-shared-deps.wit.json | 83 +++ ...ti-package-shared-deps.wit.json.golden.wit | 24 + .../multi-package-transitive-deps.wit.json | 116 ++++ ...ackage-transitive-deps.wit.json.golden.wit | 28 + ...ges-explicit-colliding-decl-names.wit.json | 130 +++++ ...t-colliding-decl-names.wit.json.golden.wit | 26 + ...ages-explicit-internal-references.wit.json | 87 +++ ...it-internal-references.wit.json.golden.wit | 16 + .../packages-explicit-with-semver.wit.json | 130 +++++ ...s-explicit-with-semver.wit.json.golden.wit | 26 + .../packages-multiple-explicit.wit.json | 130 +++++ ...ages-multiple-explicit.wit.json.golden.wit | 26 + .../packages-single-explicit.wit.json | 70 +++ ...ckages-single-explicit.wit.json.golden.wit | 12 + testdata/wit-parser/shared-types.wit.json | 8 +- .../wit-parser/since-and-unstable.wit.json | 549 ++++++++++++++++++ .../since-and-unstable.wit.json.golden.wit | 45 ++ .../stress-export-elaborate.wit.json | 40 +- testdata/wit-parser/world-diamond.wit.json | 12 +- .../world-iface-no-collide.wit.json | 4 +- .../world-implicit-import1.wit.json | 12 +- .../world-implicit-import2.wit.json | 4 +- .../world-implicit-import3.wit.json | 4 +- .../wit-parser/world-same-fields4.wit.json | 12 +- .../world-top-level-resources.wit.json | 12 +- .../wit-parser/worlds-union-dedup.wit.json | 24 +- .../wit-parser/worlds-with-types.wit.json | 4 +- 58 files changed, 3171 insertions(+), 311 deletions(-) create mode 100644 testdata/wit-parser/feature-gates.wit.json create mode 100644 testdata/wit-parser/feature-gates.wit.json.golden.wit create mode 100644 testdata/wit-parser/multi-file-multi-package.wit.json create mode 100644 testdata/wit-parser/multi-file-multi-package.wit.json.golden.wit create mode 100644 testdata/wit-parser/multi-package-shared-deps.wit.json create mode 100644 testdata/wit-parser/multi-package-shared-deps.wit.json.golden.wit create mode 100644 testdata/wit-parser/multi-package-transitive-deps.wit.json create mode 100644 testdata/wit-parser/multi-package-transitive-deps.wit.json.golden.wit create mode 100644 testdata/wit-parser/packages-explicit-colliding-decl-names.wit.json create mode 100644 testdata/wit-parser/packages-explicit-colliding-decl-names.wit.json.golden.wit create mode 100644 testdata/wit-parser/packages-explicit-internal-references.wit.json create mode 100644 testdata/wit-parser/packages-explicit-internal-references.wit.json.golden.wit create mode 100644 testdata/wit-parser/packages-explicit-with-semver.wit.json create mode 100644 testdata/wit-parser/packages-explicit-with-semver.wit.json.golden.wit create mode 100644 testdata/wit-parser/packages-multiple-explicit.wit.json create mode 100644 testdata/wit-parser/packages-multiple-explicit.wit.json.golden.wit create mode 100644 testdata/wit-parser/packages-single-explicit.wit.json create mode 100644 testdata/wit-parser/packages-single-explicit.wit.json.golden.wit create mode 100644 testdata/wit-parser/since-and-unstable.wit.json create mode 100644 testdata/wit-parser/since-and-unstable.wit.json.golden.wit diff --git a/testdata/escape/escaped-names.wit.json b/testdata/escape/escaped-names.wit.json index c9c43a11..4ddba399 100644 --- a/testdata/escape/escaped-names.wit.json +++ b/testdata/escape/escaped-names.wit.json @@ -4,12 +4,16 @@ "name": "world", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "exports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "package": 0 @@ -22,7 +26,8 @@ "error": 0, "export": 4, "enum": 5, - "record": 7 + "record": 7, + "variant": 8 }, "functions": { "func": { @@ -36,7 +41,7 @@ ], "results": [ { - "type": 8 + "type": 9 } ] } @@ -193,6 +198,74 @@ "interface": 0 } }, + { + "name": "variant", + "kind": { + "variant": { + "cases": [ + { + "name": "enum", + "type": 7 + }, + { + "name": "export", + "type": 7 + }, + { + "name": "flags", + "type": 7 + }, + { + "name": "func", + "type": 7 + }, + { + "name": "import", + "type": 7 + }, + { + "name": "include", + "type": 7 + }, + { + "name": "interface", + "type": 7 + }, + { + "name": "package", + "type": 7 + }, + { + "name": "record", + "type": 7 + }, + { + "name": "resource", + "type": 7 + }, + { + "name": "static", + "type": 7 + }, + { + "name": "type", + "type": 7 + }, + { + "name": "variant", + "type": 7 + }, + { + "name": "world", + "type": 7 + } + ] + } + }, + "owner": { + "interface": 0 + } + }, { "name": null, "kind": { diff --git a/testdata/escape/escaped-names.wit.json.golden.wit b/testdata/escape/escaped-names.wit.json.golden.wit index 13dd73a4..9d5450b5 100644 --- a/testdata/escape/escaped-names.wit.json.golden.wit +++ b/testdata/escape/escaped-names.wit.json.golden.wit @@ -26,6 +26,22 @@ interface %interface { %enum: %enum, %result: result, } + variant %variant { + %enum(%record), + %export(%record), + %flags(%record), + %func(%record), + %import(%record), + %include(%record), + %interface(%record), + %package(%record), + %record(%record), + %resource(%record), + %static(%record), + %type(%record), + %variant(%record), + %world(%record), + } %func: func(rec: %record) -> result<%export, error>; } diff --git a/testdata/example/aqua.wit.json b/testdata/example/aqua.wit.json index dfee18ec..71136b37 100644 --- a/testdata/example/aqua.wit.json +++ b/testdata/example/aqua.wit.json @@ -5,7 +5,9 @@ "imports": {}, "exports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "package": 0 diff --git a/testdata/example/exported-list.wit.json b/testdata/example/exported-list.wit.json index 7e4ea0b4..ed157234 100644 --- a/testdata/example/exported-list.wit.json +++ b/testdata/example/exported-list.wit.json @@ -5,7 +5,9 @@ "imports": {}, "exports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "package": 0 diff --git a/testdata/example/exported-resource.wit.json b/testdata/example/exported-resource.wit.json index c6b0283f..49f9555b 100644 --- a/testdata/example/exported-resource.wit.json +++ b/testdata/example/exported-resource.wit.json @@ -5,7 +5,9 @@ "imports": {}, "exports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "package": 0 diff --git a/testdata/example/flat-variant.wit.json b/testdata/example/flat-variant.wit.json index b007f0fc..5c456d0a 100644 --- a/testdata/example/flat-variant.wit.json +++ b/testdata/example/flat-variant.wit.json @@ -4,7 +4,9 @@ "name": "imports", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "exports": {}, diff --git a/testdata/example/non-flat-params.wit.json b/testdata/example/non-flat-params.wit.json index e911cc42..47bfb522 100644 --- a/testdata/example/non-flat-params.wit.json +++ b/testdata/example/non-flat-params.wit.json @@ -4,12 +4,16 @@ "name": "imports", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "exports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "package": 0 diff --git a/testdata/example/post-return.wit.json b/testdata/example/post-return.wit.json index 7fda8a95..832d1bdc 100644 --- a/testdata/example/post-return.wit.json +++ b/testdata/example/post-return.wit.json @@ -5,7 +5,9 @@ "imports": {}, "exports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "package": 0 diff --git a/testdata/example/resource-in-world.wit.json b/testdata/example/resource-in-world.wit.json index 5b7fb67a..1f4f7fc8 100644 --- a/testdata/example/resource-in-world.wit.json +++ b/testdata/example/resource-in-world.wit.json @@ -4,12 +4,16 @@ "name": "imports", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "exports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "package": 0 diff --git a/testdata/example/use-of-export.wit.json b/testdata/example/use-of-export.wit.json index f0f1a2bd..49f81d74 100644 --- a/testdata/example/use-of-export.wit.json +++ b/testdata/example/use-of-export.wit.json @@ -5,10 +5,14 @@ "imports": {}, "exports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "package": 0 @@ -20,13 +24,27 @@ "types": { "res": 0 }, - "functions": {}, + "functions": { + "[method]res.do": { + "name": "[method]res.do", + "kind": { + "method": 0 + }, + "params": [ + { + "name": "self", + "type": 1 + } + ], + "results": [] + } + }, "package": 0 }, { "name": "f", "types": { - "res": 1 + "res": 2 }, "functions": { "report": { @@ -35,7 +53,7 @@ "params": [ { "name": "r", - "type": 2 + "type": 3 } ], "results": [] @@ -52,6 +70,15 @@ "interface": 0 } }, + { + "name": null, + "kind": { + "handle": { + "borrow": 0 + } + }, + "owner": null + }, { "name": "res", "kind": { @@ -65,7 +92,7 @@ "name": null, "kind": { "handle": { - "own": 1 + "own": 2 } }, "owner": null diff --git a/testdata/example/use-of-export.wit.json.golden.wit b/testdata/example/use-of-export.wit.json.golden.wit index c54de6a5..78aa0309 100644 --- a/testdata/example/use-of-export.wit.json.golden.wit +++ b/testdata/example/use-of-export.wit.json.golden.wit @@ -1,7 +1,9 @@ package example:uses; interface a { - resource res; + resource res { + do: func(); + } } interface f { diff --git a/testdata/example/use-of-import-or-export.wit.json b/testdata/example/use-of-import-or-export.wit.json index 04ad04d2..91f89d03 100644 --- a/testdata/example/use-of-import-or-export.wit.json +++ b/testdata/example/use-of-import-or-export.wit.json @@ -4,21 +4,31 @@ "name": "default", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "exports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } } }, "package": 0 diff --git a/testdata/example/use-of-import.wit.json b/testdata/example/use-of-import.wit.json index ed23a70a..b5117714 100644 --- a/testdata/example/use-of-import.wit.json +++ b/testdata/example/use-of-import.wit.json @@ -4,12 +4,16 @@ "name": "default", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "exports": { "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "package": 0 @@ -21,13 +25,27 @@ "types": { "res": 0 }, - "functions": {}, + "functions": { + "[method]res.do": { + "name": "[method]res.do", + "kind": { + "method": 0 + }, + "params": [ + { + "name": "self", + "type": 1 + } + ], + "results": [] + } + }, "package": 0 }, { "name": "f", "types": { - "res": 1 + "res": 2 }, "functions": { "report": { @@ -36,7 +54,7 @@ "params": [ { "name": "r", - "type": 2 + "type": 3 } ], "results": [] @@ -53,6 +71,15 @@ "interface": 0 } }, + { + "name": null, + "kind": { + "handle": { + "borrow": 0 + } + }, + "owner": null + }, { "name": "res", "kind": { @@ -66,7 +93,7 @@ "name": null, "kind": { "handle": { - "own": 1 + "own": 2 } }, "owner": null diff --git a/testdata/example/use-of-import.wit.json.golden.wit b/testdata/example/use-of-import.wit.json.golden.wit index 8a012144..8d399743 100644 --- a/testdata/example/use-of-import.wit.json.golden.wit +++ b/testdata/example/use-of-import.wit.json.golden.wit @@ -1,7 +1,9 @@ package example:uses; interface a { - resource res; + resource res { + do: func(); + } } interface f { diff --git a/testdata/wasi/cli.wit.json b/testdata/wasi/cli.wit.json index f7ddff7c..1e46bcb2 100644 --- a/testdata/wasi/cli.wit.json +++ b/testdata/wasi/cli.wit.json @@ -4,13 +4,19 @@ "name": "imports", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } } }, "exports": {}, @@ -20,13 +26,19 @@ "name": "imports", "imports": { "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } }, "interface-4": { - "interface": 4 + "interface": { + "id": 4 + } } }, "exports": {}, @@ -36,22 +48,34 @@ "name": "imports", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } }, "interface-4": { - "interface": 4 + "interface": { + "id": 4 + } }, "interface-5": { - "interface": 5 + "interface": { + "id": 5 + } }, "interface-6": { - "interface": 6 + "interface": { + "id": 6 + } } }, "exports": {}, @@ -61,13 +85,19 @@ "name": "imports", "imports": { "interface-9": { - "interface": 9 + "interface": { + "id": 9 + } }, "interface-8": { - "interface": 8 + "interface": { + "id": 8 + } }, "interface-7": { - "interface": 7 + "interface": { + "id": 7 + } } }, "exports": {}, @@ -77,37 +107,59 @@ "name": "imports", "imports": { "interface-10": { - "interface": 10 + "interface": { + "id": 10 + } }, "interface-11": { - "interface": 11 + "interface": { + "id": 11 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-15": { - "interface": 15 + "interface": { + "id": 15 + } }, "interface-16": { - "interface": 16 + "interface": { + "id": 16 + } }, "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } }, "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } }, "interface-13": { - "interface": 13 + "interface": { + "id": 13 + } }, "interface-14": { - "interface": 14 + "interface": { + "id": 14 + } }, "interface-12": { - "interface": 12 + "interface": { + "id": 12 + } } }, "exports": {}, @@ -117,85 +169,139 @@ "name": "imports", "imports": { "interface-17": { - "interface": 17 + "interface": { + "id": 17 + } }, "interface-18": { - "interface": 18 + "interface": { + "id": 18 + } }, "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } }, "interface-20": { - "interface": 20 + "interface": { + "id": 20 + } }, "interface-21": { - "interface": 21 + "interface": { + "id": 21 + } }, "interface-22": { - "interface": 22 + "interface": { + "id": 22 + } }, "interface-23": { - "interface": 23 + "interface": { + "id": 23 + } }, "interface-24": { - "interface": 24 + "interface": { + "id": 24 + } }, "interface-25": { - "interface": 25 + "interface": { + "id": 25 + } }, "interface-26": { - "interface": 26 + "interface": { + "id": 26 + } }, "interface-27": { - "interface": 27 + "interface": { + "id": 27 + } }, "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } }, "interface-4": { - "interface": 4 + "interface": { + "id": 4 + } }, "interface-5": { - "interface": 5 + "interface": { + "id": 5 + } }, "interface-6": { - "interface": 6 + "interface": { + "id": 6 + } }, "interface-10": { - "interface": 10 + "interface": { + "id": 10 + } }, "interface-11": { - "interface": 11 + "interface": { + "id": 11 + } }, "interface-15": { - "interface": 15 + "interface": { + "id": 15 + } }, "interface-16": { - "interface": 16 + "interface": { + "id": 16 + } }, "interface-13": { - "interface": 13 + "interface": { + "id": 13 + } }, "interface-14": { - "interface": 14 + "interface": { + "id": 14 + } }, "interface-12": { - "interface": 12 + "interface": { + "id": 12 + } }, "interface-9": { - "interface": 9 + "interface": { + "id": 9 + } }, "interface-8": { - "interface": 8 + "interface": { + "id": 8 + } }, "interface-7": { - "interface": 7 + "interface": { + "id": 7 + } } }, "exports": {}, @@ -205,90 +311,146 @@ "name": "command", "imports": { "interface-17": { - "interface": 17 + "interface": { + "id": 17 + } }, "interface-18": { - "interface": 18 + "interface": { + "id": 18 + } }, "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } }, "interface-20": { - "interface": 20 + "interface": { + "id": 20 + } }, "interface-21": { - "interface": 21 + "interface": { + "id": 21 + } }, "interface-22": { - "interface": 22 + "interface": { + "id": 22 + } }, "interface-23": { - "interface": 23 + "interface": { + "id": 23 + } }, "interface-24": { - "interface": 24 + "interface": { + "id": 24 + } }, "interface-25": { - "interface": 25 + "interface": { + "id": 25 + } }, "interface-26": { - "interface": 26 + "interface": { + "id": 26 + } }, "interface-27": { - "interface": 27 + "interface": { + "id": 27 + } }, "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } }, "interface-4": { - "interface": 4 + "interface": { + "id": 4 + } }, "interface-5": { - "interface": 5 + "interface": { + "id": 5 + } }, "interface-6": { - "interface": 6 + "interface": { + "id": 6 + } }, "interface-10": { - "interface": 10 + "interface": { + "id": 10 + } }, "interface-11": { - "interface": 11 + "interface": { + "id": 11 + } }, "interface-15": { - "interface": 15 + "interface": { + "id": 15 + } }, "interface-16": { - "interface": 16 + "interface": { + "id": 16 + } }, "interface-13": { - "interface": 13 + "interface": { + "id": 13 + } }, "interface-14": { - "interface": 14 + "interface": { + "id": 14 + } }, "interface-12": { - "interface": 12 + "interface": { + "id": 12 + } }, "interface-9": { - "interface": 9 + "interface": { + "id": 9 + } }, "interface-8": { - "interface": 8 + "interface": { + "id": 8 + } }, "interface-7": { - "interface": 7 + "interface": { + "id": 7 + } } }, "exports": { "interface-19": { - "interface": 19 + "interface": { + "id": 19 + } } }, "package": 5 diff --git a/testdata/wasi/http.wit.json b/testdata/wasi/http.wit.json index bd032386..838304fe 100644 --- a/testdata/wasi/http.wit.json +++ b/testdata/wasi/http.wit.json @@ -4,13 +4,19 @@ "name": "imports", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } } }, "exports": {}, @@ -20,13 +26,19 @@ "name": "imports", "imports": { "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } }, "interface-4": { - "interface": 4 + "interface": { + "id": 4 + } } }, "exports": {}, @@ -36,22 +48,34 @@ "name": "imports", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } }, "interface-4": { - "interface": 4 + "interface": { + "id": 4 + } }, "interface-5": { - "interface": 5 + "interface": { + "id": 5 + } }, "interface-6": { - "interface": 6 + "interface": { + "id": 6 + } } }, "exports": {}, @@ -61,37 +85,59 @@ "name": "imports", "imports": { "interface-7": { - "interface": 7 + "interface": { + "id": 7 + } }, "interface-8": { - "interface": 8 + "interface": { + "id": 8 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-12": { - "interface": 12 + "interface": { + "id": 12 + } }, "interface-13": { - "interface": 13 + "interface": { + "id": 13 + } }, "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } }, "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } }, "interface-10": { - "interface": 10 + "interface": { + "id": 10 + } }, "interface-11": { - "interface": 11 + "interface": { + "id": 11 + } }, "interface-9": { - "interface": 9 + "interface": { + "id": 9 + } } }, "exports": {}, @@ -101,13 +147,19 @@ "name": "imports", "imports": { "interface-16": { - "interface": 16 + "interface": { + "id": 16 + } }, "interface-15": { - "interface": 15 + "interface": { + "id": 15 + } }, "interface-14": { - "interface": 14 + "interface": { + "id": 14 + } } }, "exports": {}, @@ -117,85 +169,139 @@ "name": "imports", "imports": { "interface-17": { - "interface": 17 + "interface": { + "id": 17 + } }, "interface-18": { - "interface": 18 + "interface": { + "id": 18 + } }, "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } }, "interface-20": { - "interface": 20 + "interface": { + "id": 20 + } }, "interface-21": { - "interface": 21 + "interface": { + "id": 21 + } }, "interface-22": { - "interface": 22 + "interface": { + "id": 22 + } }, "interface-23": { - "interface": 23 + "interface": { + "id": 23 + } }, "interface-24": { - "interface": 24 + "interface": { + "id": 24 + } }, "interface-25": { - "interface": 25 + "interface": { + "id": 25 + } }, "interface-26": { - "interface": 26 + "interface": { + "id": 26 + } }, "interface-27": { - "interface": 27 + "interface": { + "id": 27 + } }, "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } }, "interface-4": { - "interface": 4 + "interface": { + "id": 4 + } }, "interface-5": { - "interface": 5 + "interface": { + "id": 5 + } }, "interface-6": { - "interface": 6 + "interface": { + "id": 6 + } }, "interface-7": { - "interface": 7 + "interface": { + "id": 7 + } }, "interface-8": { - "interface": 8 + "interface": { + "id": 8 + } }, "interface-12": { - "interface": 12 + "interface": { + "id": 12 + } }, "interface-13": { - "interface": 13 + "interface": { + "id": 13 + } }, "interface-10": { - "interface": 10 + "interface": { + "id": 10 + } }, "interface-11": { - "interface": 11 + "interface": { + "id": 11 + } }, "interface-9": { - "interface": 9 + "interface": { + "id": 9 + } }, "interface-16": { - "interface": 16 + "interface": { + "id": 16 + } }, "interface-15": { - "interface": 15 + "interface": { + "id": 15 + } }, "interface-14": { - "interface": 14 + "interface": { + "id": 14 + } } }, "exports": {}, @@ -205,90 +311,146 @@ "name": "command", "imports": { "interface-17": { - "interface": 17 + "interface": { + "id": 17 + } }, "interface-18": { - "interface": 18 + "interface": { + "id": 18 + } }, "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } }, "interface-20": { - "interface": 20 + "interface": { + "id": 20 + } }, "interface-21": { - "interface": 21 + "interface": { + "id": 21 + } }, "interface-22": { - "interface": 22 + "interface": { + "id": 22 + } }, "interface-23": { - "interface": 23 + "interface": { + "id": 23 + } }, "interface-24": { - "interface": 24 + "interface": { + "id": 24 + } }, "interface-25": { - "interface": 25 + "interface": { + "id": 25 + } }, "interface-26": { - "interface": 26 + "interface": { + "id": 26 + } }, "interface-27": { - "interface": 27 + "interface": { + "id": 27 + } }, "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } }, "interface-4": { - "interface": 4 + "interface": { + "id": 4 + } }, "interface-5": { - "interface": 5 + "interface": { + "id": 5 + } }, "interface-6": { - "interface": 6 + "interface": { + "id": 6 + } }, "interface-7": { - "interface": 7 + "interface": { + "id": 7 + } }, "interface-8": { - "interface": 8 + "interface": { + "id": 8 + } }, "interface-12": { - "interface": 12 + "interface": { + "id": 12 + } }, "interface-13": { - "interface": 13 + "interface": { + "id": 13 + } }, "interface-10": { - "interface": 10 + "interface": { + "id": 10 + } }, "interface-11": { - "interface": 11 + "interface": { + "id": 11 + } }, "interface-9": { - "interface": 9 + "interface": { + "id": 9 + } }, "interface-16": { - "interface": 16 + "interface": { + "id": 16 + } }, "interface-15": { - "interface": 15 + "interface": { + "id": 15 + } }, "interface-14": { - "interface": 14 + "interface": { + "id": 14 + } } }, "exports": { "interface-19": { - "interface": 19 + "interface": { + "id": 19 + } } }, "package": 5 @@ -297,37 +459,59 @@ "name": "imports", "imports": { "interface-16": { - "interface": 16 + "interface": { + "id": 16 + } }, "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } }, "interface-21": { - "interface": 21 + "interface": { + "id": 21 + } }, "interface-22": { - "interface": 22 + "interface": { + "id": 22 + } }, "interface-20": { - "interface": 20 + "interface": { + "id": 20 + } }, "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } }, "interface-28": { - "interface": 28 + "interface": { + "id": 28 + } }, "interface-30": { - "interface": 30 + "interface": { + "id": 30 + } }, "interface-4": { - "interface": 4 + "interface": { + "id": 4 + } } }, "exports": {}, @@ -340,42 +524,66 @@ "name": "proxy", "imports": { "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } }, "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } }, "interface-28": { - "interface": 28 + "interface": { + "id": 28 + } }, "interface-16": { - "interface": 16 + "interface": { + "id": 16 + } }, "interface-21": { - "interface": 21 + "interface": { + "id": 21 + } }, "interface-22": { - "interface": 22 + "interface": { + "id": 22 + } }, "interface-20": { - "interface": 20 + "interface": { + "id": 20 + } }, "interface-30": { - "interface": 30 + "interface": { + "id": 30 + } }, "interface-4": { - "interface": 4 + "interface": { + "id": 4 + } } }, "exports": { "interface-29": { - "interface": 29 + "interface": { + "id": 29 + } } }, "package": 6, diff --git a/testdata/wit-parser/complex-include.wit.json b/testdata/wit-parser/complex-include.wit.json index 2c657bea..f9b17f27 100644 --- a/testdata/wit-parser/complex-include.wit.json +++ b/testdata/wit-parser/complex-include.wit.json @@ -4,10 +4,14 @@ "name": "bar-a", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "exports": {}, @@ -17,10 +21,14 @@ "name": "baz-a", "imports": { "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } }, "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } } }, "exports": {}, @@ -30,10 +38,14 @@ "name": "a", "imports": { "interface-4": { - "interface": 4 + "interface": { + "id": 4 + } }, "interface-5": { - "interface": 5 + "interface": { + "id": 5 + } } }, "exports": {}, @@ -43,10 +55,14 @@ "name": "b", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "exports": {}, @@ -56,10 +72,14 @@ "name": "c", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "exports": {}, @@ -69,22 +89,34 @@ "name": "union-world", "imports": { "interface-4": { - "interface": 4 + "interface": { + "id": 4 + } }, "interface-5": { - "interface": 5 + "interface": { + "id": 5 + } }, "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } }, "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } } }, "exports": {}, diff --git a/testdata/wit-parser/diamond1.wit.json b/testdata/wit-parser/diamond1.wit.json index 8d7ce082..54e87e7b 100644 --- a/testdata/wit-parser/diamond1.wit.json +++ b/testdata/wit-parser/diamond1.wit.json @@ -4,10 +4,14 @@ "name": "foo", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "exports": {}, diff --git a/testdata/wit-parser/disambiguate-diamond.wit.json b/testdata/wit-parser/disambiguate-diamond.wit.json index 0e3ebea3..34f675da 100644 --- a/testdata/wit-parser/disambiguate-diamond.wit.json +++ b/testdata/wit-parser/disambiguate-diamond.wit.json @@ -4,16 +4,24 @@ "name": "foo", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "foo": { - "interface": 2 + "interface": { + "id": 2 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "bar": { - "interface": 3 + "interface": { + "id": 3 + } } }, "exports": {}, diff --git a/testdata/wit-parser/feature-gates.wit.json b/testdata/wit-parser/feature-gates.wit.json new file mode 100644 index 00000000..e211f045 --- /dev/null +++ b/testdata/wit-parser/feature-gates.wit.json @@ -0,0 +1,288 @@ +{ + "worlds": [ + { + "name": "ungated-world", + "imports": { + "interface-4": { + "interface": { + "id": 4, + "stability": { + "type": "unstable", + "feature": "active" + } + } + } + }, + "exports": { + "interface-4": { + "interface": { + "id": 4, + "stability": { + "type": "unstable", + "feature": "active" + } + } + } + }, + "package": 0, + "stability": { + "type": "unstable", + "feature": "active" + } + }, + { + "name": "mixed-world", + "imports": { + "interface-4": { + "interface": { + "id": 4, + "stability": { + "type": "unstable", + "feature": "active" + } + } + } + }, + "exports": { + "interface-4": { + "interface": { + "id": 4, + "stability": { + "type": "unstable", + "feature": "active" + } + } + } + }, + "package": 0 + } + ], + "interfaces": [ + { + "name": "ungated", + "types": {}, + "functions": { + "ungated": { + "name": "ungated", + "kind": "freestanding", + "params": [], + "results": [], + "stability": { + "type": "unstable", + "feature": "active" + } + } + }, + "stability": { + "type": "unstable", + "feature": "active" + }, + "package": 0 + }, + { + "name": "ungated2", + "types": { + "ungated": 0, + "ungated2": 1 + }, + "functions": {}, + "stability": { + "type": "unstable", + "feature": "active" + }, + "package": 0 + }, + { + "name": "ungated-use-target", + "types": { + "t": 2 + }, + "functions": {}, + "stability": { + "type": "unstable", + "feature": "active" + }, + "package": 0 + }, + { + "name": "ungated-use", + "types": { + "t": 3 + }, + "functions": {}, + "stability": { + "type": "unstable", + "feature": "active" + }, + "package": 0 + }, + { + "name": "ungated-for-world", + "types": {}, + "functions": {}, + "stability": { + "type": "unstable", + "feature": "active" + }, + "package": 0 + }, + { + "name": "with-resources", + "types": { + "ungated": 4 + }, + "functions": { + "[constructor]ungated": { + "name": "[constructor]ungated", + "kind": { + "constructor": 4 + }, + "params": [], + "results": [ + { + "type": 6 + } + ], + "stability": { + "type": "unstable", + "feature": "active" + } + }, + "[static]ungated.x": { + "name": "[static]ungated.x", + "kind": { + "static": 4 + }, + "params": [], + "results": [], + "stability": { + "type": "unstable", + "feature": "active" + } + }, + "[method]ungated.y": { + "name": "[method]ungated.y", + "kind": { + "method": 4 + }, + "params": [ + { + "name": "self", + "type": 5 + } + ], + "results": [], + "stability": { + "type": "unstable", + "feature": "active" + } + } + }, + "package": 0 + } + ], + "types": [ + { + "name": "ungated", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 1 + }, + "stability": { + "type": "unstable", + "feature": "active" + } + }, + { + "name": "ungated2", + "kind": { + "type": 0 + }, + "owner": { + "interface": 1 + }, + "stability": { + "type": "unstable", + "feature": "active" + } + }, + { + "name": "t", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 2 + }, + "stability": { + "type": "unstable", + "feature": "active" + } + }, + { + "name": "t", + "kind": { + "type": 2 + }, + "owner": { + "interface": 3 + }, + "stability": { + "type": "unstable", + "feature": "active" + } + }, + { + "name": "ungated", + "kind": "resource", + "owner": { + "interface": 5 + }, + "stability": { + "type": "unstable", + "feature": "active" + } + }, + { + "name": null, + "kind": { + "handle": { + "borrow": 4 + } + }, + "owner": null, + "stability": { + "type": "unstable", + "feature": "active" + } + }, + { + "name": null, + "kind": { + "handle": { + "own": 4 + } + }, + "owner": null + } + ], + "packages": [ + { + "name": "a:b", + "interfaces": { + "ungated": 0, + "ungated2": 1, + "ungated-use-target": 2, + "ungated-use": 3, + "ungated-for-world": 4, + "with-resources": 5 + }, + "worlds": { + "ungated-world": 0, + "mixed-world": 1 + } + } + ] +} \ No newline at end of file diff --git a/testdata/wit-parser/feature-gates.wit.json.golden.wit b/testdata/wit-parser/feature-gates.wit.json.golden.wit new file mode 100644 index 00000000..8f205413 --- /dev/null +++ b/testdata/wit-parser/feature-gates.wit.json.golden.wit @@ -0,0 +1,37 @@ +package a:b; + +interface ungated { + ungated: func(); +} + +interface ungated2 { + type ungated = u32; + type ungated2 = ungated; +} + +interface ungated-use-target { + type t = u32; +} + +interface ungated-use { + use ungated-use-target.{t}; +} + +interface ungated-for-world {} + +interface with-resources { + resource ungated { + constructor(); + y: func(); + x: static func(); + } +} + +world ungated-world { + import ungated-for-world; + export ungated-for-world; +} +world mixed-world { + import ungated-for-world; + export ungated-for-world; +} diff --git a/testdata/wit-parser/foreign-deps-union.wit.json b/testdata/wit-parser/foreign-deps-union.wit.json index 3c1e2fb4..419d62e7 100644 --- a/testdata/wit-parser/foreign-deps-union.wit.json +++ b/testdata/wit-parser/foreign-deps-union.wit.json @@ -4,10 +4,14 @@ "name": "wasi", "imports": { "interface-8": { - "interface": 8 + "interface": { + "id": 8 + } }, "interface-7": { - "interface": 7 + "interface": { + "id": 7 + } } }, "exports": {}, @@ -17,15 +21,21 @@ "name": "my-world", "imports": { "interface-8": { - "interface": 8 + "interface": { + "id": 8 + } }, "interface-7": { - "interface": 7 + "interface": { + "id": 7 + } } }, "exports": { "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "package": 6 @@ -34,18 +44,26 @@ "name": "my-world2", "imports": { "interface-8": { - "interface": 8 + "interface": { + "id": 8 + } }, "interface-7": { - "interface": 7 + "interface": { + "id": 7 + } } }, "exports": { "interface-9": { - "interface": 9 + "interface": { + "id": 9 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "package": 6 @@ -54,10 +72,14 @@ "name": "bars-world", "imports": { "interface-4": { - "interface": 4 + "interface": { + "id": 4 + } }, "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "exports": {}, @@ -67,18 +89,26 @@ "name": "unionw-world", "imports": { "interface-8": { - "interface": 8 + "interface": { + "id": 8 + } }, "interface-7": { - "interface": 7 + "interface": { + "id": 7 + } } }, "exports": { "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-9": { - "interface": 9 + "interface": { + "id": 9 + } } }, "package": 6 diff --git a/testdata/wit-parser/foreign-deps.wit.json b/testdata/wit-parser/foreign-deps.wit.json index da5020f0..503c1b32 100644 --- a/testdata/wit-parser/foreign-deps.wit.json +++ b/testdata/wit-parser/foreign-deps.wit.json @@ -4,15 +4,21 @@ "name": "my-world", "imports": { "interface-8": { - "interface": 8 + "interface": { + "id": 8 + } }, "interface-7": { - "interface": 7 + "interface": { + "id": 7 + } } }, "exports": { "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "package": 6 @@ -21,18 +27,26 @@ "name": "my-world2", "imports": { "interface-8": { - "interface": 8 + "interface": { + "id": 8 + } }, "interface-7": { - "interface": 7 + "interface": { + "id": 7 + } } }, "exports": { "interface-9": { - "interface": 9 + "interface": { + "id": 9 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "package": 6 @@ -41,10 +55,14 @@ "name": "bars-world", "imports": { "interface-4": { - "interface": 4 + "interface": { + "id": 4 + } }, "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "exports": {}, diff --git a/testdata/wit-parser/ignore-files-deps.wit.json b/testdata/wit-parser/ignore-files-deps.wit.json index 1b9d00c3..7a812dbe 100644 --- a/testdata/wit-parser/ignore-files-deps.wit.json +++ b/testdata/wit-parser/ignore-files-deps.wit.json @@ -4,7 +4,9 @@ "name": "foo", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "exports": {}, diff --git a/testdata/wit-parser/import-export-overlap2.wit.json b/testdata/wit-parser/import-export-overlap2.wit.json index fff5bfd3..0c991269 100644 --- a/testdata/wit-parser/import-export-overlap2.wit.json +++ b/testdata/wit-parser/import-export-overlap2.wit.json @@ -14,7 +14,9 @@ }, "exports": { "a": { - "interface": 0 + "interface": { + "id": 0 + } } }, "package": 0 diff --git a/testdata/wit-parser/include-reps.wit.json b/testdata/wit-parser/include-reps.wit.json index 8217e2ee..21debef2 100644 --- a/testdata/wit-parser/include-reps.wit.json +++ b/testdata/wit-parser/include-reps.wit.json @@ -4,12 +4,16 @@ "name": "bar", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "exports": { "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "package": 0 @@ -18,12 +22,16 @@ "name": "foo", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "exports": { "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "package": 0 diff --git a/testdata/wit-parser/kinds-of-deps.wit.json b/testdata/wit-parser/kinds-of-deps.wit.json index 07f01a7c..f13afe63 100644 --- a/testdata/wit-parser/kinds-of-deps.wit.json +++ b/testdata/wit-parser/kinds-of-deps.wit.json @@ -4,16 +4,24 @@ "name": "a", "imports": { "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } }, "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } }, "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "exports": {}, diff --git a/testdata/wit-parser/many-names.wit.json b/testdata/wit-parser/many-names.wit.json index 87d111bd..590c6847 100644 --- a/testdata/wit-parser/many-names.wit.json +++ b/testdata/wit-parser/many-names.wit.json @@ -4,7 +4,9 @@ "name": "name", "imports": { "name": { - "interface": 1 + "interface": { + "id": 1 + } } }, "exports": {}, diff --git a/testdata/wit-parser/multi-file-multi-package.wit.json b/testdata/wit-parser/multi-file-multi-package.wit.json new file mode 100644 index 00000000..a2dea317 --- /dev/null +++ b/testdata/wit-parser/multi-file-multi-package.wit.json @@ -0,0 +1,250 @@ +{ + "worlds": [ + { + "name": "w2", + "imports": { + "interface-0": { + "interface": { + "id": 0 + } + }, + "imp2": { + "interface": { + "id": 1 + } + } + }, + "exports": {}, + "package": 0 + }, + { + "name": "w3", + "imports": { + "interface-2": { + "interface": { + "id": 2 + } + }, + "imp3": { + "interface": { + "id": 3 + } + } + }, + "exports": {}, + "package": 1 + }, + { + "name": "w1", + "imports": { + "interface-4": { + "interface": { + "id": 4 + } + }, + "imp1": { + "interface": { + "id": 5 + } + } + }, + "exports": {}, + "package": 2 + }, + { + "name": "w4", + "imports": { + "interface-6": { + "interface": { + "id": 6 + } + }, + "imp4": { + "interface": { + "id": 7 + } + } + }, + "exports": {}, + "package": 3 + } + ], + "interfaces": [ + { + "name": "i2", + "types": { + "b": 0 + }, + "functions": {}, + "package": 0 + }, + { + "name": null, + "types": { + "b": 1 + }, + "functions": {}, + "package": 0 + }, + { + "name": "i3", + "types": { + "a": 2 + }, + "functions": {}, + "package": 1 + }, + { + "name": null, + "types": { + "a": 3 + }, + "functions": {}, + "package": 1 + }, + { + "name": "i1", + "types": { + "a": 4 + }, + "functions": {}, + "package": 2 + }, + { + "name": null, + "types": { + "a": 5 + }, + "functions": {}, + "package": 2 + }, + { + "name": "i4", + "types": { + "b": 6 + }, + "functions": {}, + "package": 3 + }, + { + "name": null, + "types": { + "b": 7 + }, + "functions": {}, + "package": 3 + } + ], + "types": [ + { + "name": "b", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 0 + } + }, + { + "name": "b", + "kind": { + "type": 0 + }, + "owner": { + "interface": 1 + } + }, + { + "name": "a", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 2 + } + }, + { + "name": "a", + "kind": { + "type": 2 + }, + "owner": { + "interface": 3 + } + }, + { + "name": "a", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 4 + } + }, + { + "name": "a", + "kind": { + "type": 4 + }, + "owner": { + "interface": 5 + } + }, + { + "name": "b", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 6 + } + }, + { + "name": "b", + "kind": { + "type": 6 + }, + "owner": { + "interface": 7 + } + } + ], + "packages": [ + { + "name": "bar:name", + "interfaces": { + "i2": 0 + }, + "worlds": { + "w2": 0 + } + }, + { + "name": "baz:name", + "interfaces": { + "i3": 2 + }, + "worlds": { + "w3": 1 + } + }, + { + "name": "foo:name", + "interfaces": { + "i1": 4 + }, + "worlds": { + "w1": 2 + } + }, + { + "name": "qux:name", + "interfaces": { + "i4": 6 + }, + "worlds": { + "w4": 3 + } + } + ] +} \ No newline at end of file diff --git a/testdata/wit-parser/multi-file-multi-package.wit.json.golden.wit b/testdata/wit-parser/multi-file-multi-package.wit.json.golden.wit new file mode 100644 index 00000000..5fbd23ff --- /dev/null +++ b/testdata/wit-parser/multi-file-multi-package.wit.json.golden.wit @@ -0,0 +1,54 @@ +package bar:name; + +interface i2 { + type b = u32; +} + +world w2 { + import i2; + import imp2: interface { + use i2.{b}; + } +} + + +package baz:name; + +interface i3 { + type a = u32; +} + +world w3 { + import i3; + import imp3: interface { + use i3.{a}; + } +} + + +package foo:name; + +interface i1 { + type a = u32; +} + +world w1 { + import i1; + import imp1: interface { + use i1.{a}; + } +} + + +package qux:name; + +interface i4 { + type b = u32; +} + +world w4 { + import i4; + import imp4: interface { + use i4.{b}; + } +} diff --git a/testdata/wit-parser/multi-file.wit.json b/testdata/wit-parser/multi-file.wit.json index ef6171bc..45af0f5d 100644 --- a/testdata/wit-parser/multi-file.wit.json +++ b/testdata/wit-parser/multi-file.wit.json @@ -4,12 +4,16 @@ "name": "more-depends-on-later-things", "imports": { "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } } }, "exports": { "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } } }, "package": 0 @@ -18,7 +22,9 @@ "name": "the-world", "imports": { "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "x": { "type": 15 diff --git a/testdata/wit-parser/multi-package-shared-deps.wit.json b/testdata/wit-parser/multi-package-shared-deps.wit.json new file mode 100644 index 00000000..d3434da0 --- /dev/null +++ b/testdata/wit-parser/multi-package-shared-deps.wit.json @@ -0,0 +1,83 @@ +{ + "worlds": [ + { + "name": "w-bar", + "imports": { + "interface-0": { + "interface": { + "id": 0 + } + }, + "interface-1": { + "interface": { + "id": 1 + } + } + }, + "exports": {}, + "package": 2 + }, + { + "name": "w-qux", + "imports": { + "interface-0": { + "interface": { + "id": 0 + } + }, + "interface-1": { + "interface": { + "id": 1 + } + } + }, + "exports": {}, + "package": 3 + } + ], + "interfaces": [ + { + "name": "types", + "types": {}, + "functions": {}, + "package": 0 + }, + { + "name": "types", + "types": {}, + "functions": {}, + "package": 1 + } + ], + "types": [], + "packages": [ + { + "name": "foo:dep1", + "interfaces": { + "types": 0 + }, + "worlds": {} + }, + { + "name": "foo:dep2", + "interfaces": { + "types": 1 + }, + "worlds": {} + }, + { + "name": "foo:bar", + "interfaces": {}, + "worlds": { + "w-bar": 0 + } + }, + { + "name": "foo:qux", + "interfaces": {}, + "worlds": { + "w-qux": 1 + } + } + ] +} \ No newline at end of file diff --git a/testdata/wit-parser/multi-package-shared-deps.wit.json.golden.wit b/testdata/wit-parser/multi-package-shared-deps.wit.json.golden.wit new file mode 100644 index 00000000..fb7e4266 --- /dev/null +++ b/testdata/wit-parser/multi-package-shared-deps.wit.json.golden.wit @@ -0,0 +1,24 @@ +package foo:dep1; + +interface types {} + + +package foo:dep2; + +interface types {} + + +package foo:bar; + +world w-bar { + import foo:dep1/types; + import foo:dep2/types; +} + + +package foo:qux; + +world w-qux { + import foo:dep1/types; + import foo:dep2/types; +} diff --git a/testdata/wit-parser/multi-package-transitive-deps.wit.json b/testdata/wit-parser/multi-package-transitive-deps.wit.json new file mode 100644 index 00000000..ece4cdad --- /dev/null +++ b/testdata/wit-parser/multi-package-transitive-deps.wit.json @@ -0,0 +1,116 @@ +{ + "worlds": [ + { + "name": "w-bar", + "imports": { + "interface-0": { + "interface": { + "id": 0 + } + }, + "interface-1": { + "interface": { + "id": 1 + } + } + }, + "exports": {}, + "package": 2 + }, + { + "name": "w-qux", + "imports": { + "interface-0": { + "interface": { + "id": 0 + } + } + }, + "exports": {}, + "package": 3 + } + ], + "interfaces": [ + { + "name": "types", + "types": { + "a": 0 + }, + "functions": {}, + "package": 0 + }, + { + "name": "types", + "types": { + "a": 1, + "r": 2 + }, + "functions": {}, + "package": 1 + } + ], + "types": [ + { + "name": "a", + "kind": "resource", + "owner": { + "interface": 0 + } + }, + { + "name": "a", + "kind": { + "type": 0 + }, + "owner": { + "interface": 1 + } + }, + { + "name": "r", + "kind": { + "record": { + "fields": [ + { + "name": "f", + "type": "u8" + } + ] + } + }, + "owner": { + "interface": 1 + } + } + ], + "packages": [ + { + "name": "foo:dep2", + "interfaces": { + "types": 0 + }, + "worlds": {} + }, + { + "name": "foo:dep1", + "interfaces": { + "types": 1 + }, + "worlds": {} + }, + { + "name": "foo:bar", + "interfaces": {}, + "worlds": { + "w-bar": 0 + } + }, + { + "name": "foo:qux", + "interfaces": {}, + "worlds": { + "w-qux": 1 + } + } + ] +} \ No newline at end of file diff --git a/testdata/wit-parser/multi-package-transitive-deps.wit.json.golden.wit b/testdata/wit-parser/multi-package-transitive-deps.wit.json.golden.wit new file mode 100644 index 00000000..ab5f5e97 --- /dev/null +++ b/testdata/wit-parser/multi-package-transitive-deps.wit.json.golden.wit @@ -0,0 +1,28 @@ +package foo:dep2; + +interface types { + resource a; +} + + +package foo:dep1; + +interface types { + use foo:dep2/types.{a}; + record r { f: u8 } +} + + +package foo:bar; + +world w-bar { + import foo:dep2/types; + import foo:dep1/types; +} + + +package foo:qux; + +world w-qux { + import foo:dep2/types; +} diff --git a/testdata/wit-parser/packages-explicit-colliding-decl-names.wit.json b/testdata/wit-parser/packages-explicit-colliding-decl-names.wit.json new file mode 100644 index 00000000..f4c9e705 --- /dev/null +++ b/testdata/wit-parser/packages-explicit-colliding-decl-names.wit.json @@ -0,0 +1,130 @@ +{ + "worlds": [ + { + "name": "w", + "imports": { + "interface-0": { + "interface": { + "id": 0 + } + }, + "imp": { + "interface": { + "id": 1 + } + } + }, + "exports": {}, + "package": 0 + }, + { + "name": "w", + "imports": { + "interface-2": { + "interface": { + "id": 2 + } + }, + "imp": { + "interface": { + "id": 3 + } + } + }, + "exports": {}, + "package": 1 + } + ], + "interfaces": [ + { + "name": "i", + "types": { + "a": 0 + }, + "functions": {}, + "package": 0 + }, + { + "name": null, + "types": { + "a": 1 + }, + "functions": {}, + "package": 0 + }, + { + "name": "i", + "types": { + "a": 2 + }, + "functions": {}, + "package": 1 + }, + { + "name": null, + "types": { + "a": 3 + }, + "functions": {}, + "package": 1 + } + ], + "types": [ + { + "name": "a", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 0 + } + }, + { + "name": "a", + "kind": { + "type": 0 + }, + "owner": { + "interface": 1 + } + }, + { + "name": "a", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 2 + } + }, + { + "name": "a", + "kind": { + "type": 2 + }, + "owner": { + "interface": 3 + } + } + ], + "packages": [ + { + "name": "bar:name", + "interfaces": { + "i": 0 + }, + "worlds": { + "w": 0 + } + }, + { + "name": "foo:name", + "interfaces": { + "i": 2 + }, + "worlds": { + "w": 1 + } + } + ] +} \ No newline at end of file diff --git a/testdata/wit-parser/packages-explicit-colliding-decl-names.wit.json.golden.wit b/testdata/wit-parser/packages-explicit-colliding-decl-names.wit.json.golden.wit new file mode 100644 index 00000000..068bbfb0 --- /dev/null +++ b/testdata/wit-parser/packages-explicit-colliding-decl-names.wit.json.golden.wit @@ -0,0 +1,26 @@ +package bar:name; + +interface i { + type a = u32; +} + +world w { + import i; + import imp: interface { + use i.{a}; + } +} + + +package foo:name; + +interface i { + type a = u32; +} + +world w { + import i; + import imp: interface { + use i.{a}; + } +} diff --git a/testdata/wit-parser/packages-explicit-internal-references.wit.json b/testdata/wit-parser/packages-explicit-internal-references.wit.json new file mode 100644 index 00000000..1d59a07e --- /dev/null +++ b/testdata/wit-parser/packages-explicit-internal-references.wit.json @@ -0,0 +1,87 @@ +{ + "worlds": [ + { + "name": "w1", + "imports": { + "interface-0": { + "interface": { + "id": 0 + } + }, + "imp1": { + "interface": { + "id": 1 + } + } + }, + "exports": {}, + "package": 1 + } + ], + "interfaces": [ + { + "name": "i1", + "types": { + "a": 0 + }, + "functions": {}, + "package": 0 + }, + { + "name": null, + "types": { + "a": 1 + }, + "functions": { + "fn": { + "name": "fn", + "kind": "freestanding", + "params": [ + { + "name": "a", + "type": 1 + } + ], + "results": [] + } + }, + "package": 1 + } + ], + "types": [ + { + "name": "a", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 0 + } + }, + { + "name": "a", + "kind": { + "type": 0 + }, + "owner": { + "interface": 1 + } + } + ], + "packages": [ + { + "name": "foo:name", + "interfaces": { + "i1": 0 + }, + "worlds": {} + }, + { + "name": "bar:name", + "interfaces": {}, + "worlds": { + "w1": 0 + } + } + ] +} \ No newline at end of file diff --git a/testdata/wit-parser/packages-explicit-internal-references.wit.json.golden.wit b/testdata/wit-parser/packages-explicit-internal-references.wit.json.golden.wit new file mode 100644 index 00000000..af2aaba7 --- /dev/null +++ b/testdata/wit-parser/packages-explicit-internal-references.wit.json.golden.wit @@ -0,0 +1,16 @@ +package foo:name; + +interface i1 { + type a = u32; +} + + +package bar:name; + +world w1 { + import foo:name/i1; + import imp1: interface { + use foo:name/i1.{a}; + fn: func(a: a); + } +} diff --git a/testdata/wit-parser/packages-explicit-with-semver.wit.json b/testdata/wit-parser/packages-explicit-with-semver.wit.json new file mode 100644 index 00000000..23a10462 --- /dev/null +++ b/testdata/wit-parser/packages-explicit-with-semver.wit.json @@ -0,0 +1,130 @@ +{ + "worlds": [ + { + "name": "w1", + "imports": { + "interface-0": { + "interface": { + "id": 0 + } + }, + "imp1": { + "interface": { + "id": 1 + } + } + }, + "exports": {}, + "package": 0 + }, + { + "name": "w1", + "imports": { + "interface-2": { + "interface": { + "id": 2 + } + }, + "imp1": { + "interface": { + "id": 3 + } + } + }, + "exports": {}, + "package": 1 + } + ], + "interfaces": [ + { + "name": "i1", + "types": { + "a": 0 + }, + "functions": {}, + "package": 0 + }, + { + "name": null, + "types": { + "a": 1 + }, + "functions": {}, + "package": 0 + }, + { + "name": "i1", + "types": { + "a": 2 + }, + "functions": {}, + "package": 1 + }, + { + "name": null, + "types": { + "a": 3 + }, + "functions": {}, + "package": 1 + } + ], + "types": [ + { + "name": "a", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 0 + } + }, + { + "name": "a", + "kind": { + "type": 0 + }, + "owner": { + "interface": 1 + } + }, + { + "name": "a", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 2 + } + }, + { + "name": "a", + "kind": { + "type": 2 + }, + "owner": { + "interface": 3 + } + } + ], + "packages": [ + { + "name": "foo:name@1.0.0", + "interfaces": { + "i1": 0 + }, + "worlds": { + "w1": 0 + } + }, + { + "name": "foo:name@1.0.1", + "interfaces": { + "i1": 2 + }, + "worlds": { + "w1": 1 + } + } + ] +} \ No newline at end of file diff --git a/testdata/wit-parser/packages-explicit-with-semver.wit.json.golden.wit b/testdata/wit-parser/packages-explicit-with-semver.wit.json.golden.wit new file mode 100644 index 00000000..4ead32cd --- /dev/null +++ b/testdata/wit-parser/packages-explicit-with-semver.wit.json.golden.wit @@ -0,0 +1,26 @@ +package foo:name@1.0.0; + +interface i1 { + type a = u32; +} + +world w1 { + import i1; + import imp1: interface { + use i1.{a}; + } +} + + +package foo:name@1.0.1; + +interface i1 { + type a = u32; +} + +world w1 { + import i1; + import imp1: interface { + use i1.{a}; + } +} diff --git a/testdata/wit-parser/packages-multiple-explicit.wit.json b/testdata/wit-parser/packages-multiple-explicit.wit.json new file mode 100644 index 00000000..de73c51d --- /dev/null +++ b/testdata/wit-parser/packages-multiple-explicit.wit.json @@ -0,0 +1,130 @@ +{ + "worlds": [ + { + "name": "w2", + "imports": { + "interface-0": { + "interface": { + "id": 0 + } + }, + "imp2": { + "interface": { + "id": 1 + } + } + }, + "exports": {}, + "package": 0 + }, + { + "name": "w1", + "imports": { + "interface-2": { + "interface": { + "id": 2 + } + }, + "imp1": { + "interface": { + "id": 3 + } + } + }, + "exports": {}, + "package": 1 + } + ], + "interfaces": [ + { + "name": "i2", + "types": { + "b": 0 + }, + "functions": {}, + "package": 0 + }, + { + "name": null, + "types": { + "b": 1 + }, + "functions": {}, + "package": 0 + }, + { + "name": "i1", + "types": { + "a": 2 + }, + "functions": {}, + "package": 1 + }, + { + "name": null, + "types": { + "a": 3 + }, + "functions": {}, + "package": 1 + } + ], + "types": [ + { + "name": "b", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 0 + } + }, + { + "name": "b", + "kind": { + "type": 0 + }, + "owner": { + "interface": 1 + } + }, + { + "name": "a", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 2 + } + }, + { + "name": "a", + "kind": { + "type": 2 + }, + "owner": { + "interface": 3 + } + } + ], + "packages": [ + { + "name": "bar:name", + "interfaces": { + "i2": 0 + }, + "worlds": { + "w2": 0 + } + }, + { + "name": "foo:name", + "interfaces": { + "i1": 2 + }, + "worlds": { + "w1": 1 + } + } + ] +} \ No newline at end of file diff --git a/testdata/wit-parser/packages-multiple-explicit.wit.json.golden.wit b/testdata/wit-parser/packages-multiple-explicit.wit.json.golden.wit new file mode 100644 index 00000000..33e4be44 --- /dev/null +++ b/testdata/wit-parser/packages-multiple-explicit.wit.json.golden.wit @@ -0,0 +1,26 @@ +package bar:name; + +interface i2 { + type b = u32; +} + +world w2 { + import i2; + import imp2: interface { + use i2.{b}; + } +} + + +package foo:name; + +interface i1 { + type a = u32; +} + +world w1 { + import i1; + import imp1: interface { + use i1.{a}; + } +} diff --git a/testdata/wit-parser/packages-single-explicit.wit.json b/testdata/wit-parser/packages-single-explicit.wit.json new file mode 100644 index 00000000..3eefde28 --- /dev/null +++ b/testdata/wit-parser/packages-single-explicit.wit.json @@ -0,0 +1,70 @@ +{ + "worlds": [ + { + "name": "w1", + "imports": { + "interface-0": { + "interface": { + "id": 0 + } + }, + "imp1": { + "interface": { + "id": 1 + } + } + }, + "exports": {}, + "package": 0 + } + ], + "interfaces": [ + { + "name": "i1", + "types": { + "a": 0 + }, + "functions": {}, + "package": 0 + }, + { + "name": null, + "types": { + "a": 1 + }, + "functions": {}, + "package": 0 + } + ], + "types": [ + { + "name": "a", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 0 + } + }, + { + "name": "a", + "kind": { + "type": 0 + }, + "owner": { + "interface": 1 + } + } + ], + "packages": [ + { + "name": "foo:name", + "interfaces": { + "i1": 0 + }, + "worlds": { + "w1": 0 + } + } + ] +} \ No newline at end of file diff --git a/testdata/wit-parser/packages-single-explicit.wit.json.golden.wit b/testdata/wit-parser/packages-single-explicit.wit.json.golden.wit new file mode 100644 index 00000000..42c70f91 --- /dev/null +++ b/testdata/wit-parser/packages-single-explicit.wit.json.golden.wit @@ -0,0 +1,12 @@ +package foo:name; + +interface i1 { + type a = u32; +} + +world w1 { + import i1; + import imp1: interface { + use i1.{a}; + } +} diff --git a/testdata/wit-parser/shared-types.wit.json b/testdata/wit-parser/shared-types.wit.json index d0dba659..569f6d38 100644 --- a/testdata/wit-parser/shared-types.wit.json +++ b/testdata/wit-parser/shared-types.wit.json @@ -4,12 +4,16 @@ "name": "foo", "imports": { "foo": { - "interface": 0 + "interface": { + "id": 0 + } } }, "exports": { "bar": { - "interface": 1 + "interface": { + "id": 1 + } } }, "package": 0 diff --git a/testdata/wit-parser/since-and-unstable.wit.json b/testdata/wit-parser/since-and-unstable.wit.json new file mode 100644 index 00000000..c5efc938 --- /dev/null +++ b/testdata/wit-parser/since-and-unstable.wit.json @@ -0,0 +1,549 @@ +{ + "worlds": [ + { + "name": "w1", + "imports": {}, + "exports": {}, + "package": 0, + "stability": { + "type": "stable", + "since": "1.0.1" + } + }, + { + "name": "w2", + "imports": {}, + "exports": {}, + "package": 0, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": "in-a-world", + "imports": { + "y": { + "interface": { + "id": 5, + "stability": { + "type": "stable", + "since": "1.0.0" + } + } + }, + "interface-4": { + "interface": { + "id": 4, + "stability": { + "type": "stable", + "since": "1.0.0" + } + } + }, + "t1": { + "type": 9 + }, + "t2": { + "type": 10 + }, + "t3": { + "type": 11 + }, + "t4": { + "type": 12 + }, + "t5": { + "type": 13 + }, + "t6": { + "type": 14 + }, + "t7": { + "type": 15 + }, + "x": { + "function": { + "name": "x", + "kind": "freestanding", + "params": [], + "results": [], + "stability": { + "type": "stable", + "since": "1.0.0" + } + } + }, + "[constructor]t7": { + "function": { + "name": "[constructor]t7", + "kind": { + "constructor": 15 + }, + "params": [], + "results": [ + { + "type": 17 + } + ], + "stability": { + "type": "stable", + "since": "1.0.0" + } + } + } + }, + "exports": { + "x": { + "function": { + "name": "x", + "kind": "freestanding", + "params": [], + "results": [], + "stability": { + "type": "stable", + "since": "1.0.0" + } + } + }, + "y": { + "interface": { + "id": 6, + "stability": { + "type": "stable", + "since": "1.0.0" + } + } + }, + "interface-4": { + "interface": { + "id": 4, + "stability": { + "type": "stable", + "since": "1.0.0" + } + } + } + }, + "package": 0 + } + ], + "interfaces": [ + { + "name": "foo1", + "types": {}, + "functions": {}, + "stability": { + "type": "stable", + "since": "1.0.0" + }, + "package": 0 + }, + { + "name": "foo2", + "types": {}, + "functions": {}, + "stability": { + "type": "stable", + "since": "1.0.0", + "feature": "foo" + }, + "package": 0 + }, + { + "name": "foo3", + "types": {}, + "functions": {}, + "stability": { + "type": "stable", + "since": "1.0.0", + "feature": "foo-bar" + }, + "package": 0 + }, + { + "name": "in-an-interface", + "types": { + "r1": 0, + "r2": 1, + "t1": 2, + "t2": 3, + "t3": 4, + "t4": 5, + "t5": 6, + "r3": 7 + }, + "functions": { + "foo": { + "name": "foo", + "kind": "freestanding", + "params": [], + "results": [], + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + "[constructor]r3": { + "name": "[constructor]r3", + "kind": { + "constructor": 7 + }, + "params": [], + "results": [ + { + "type": 16 + } + ], + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + "[static]r3.x1": { + "name": "[static]r3.x1", + "kind": { + "static": 7 + }, + "params": [], + "results": [], + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + "[method]r3.x2": { + "name": "[method]r3.x2", + "kind": { + "method": 7 + }, + "params": [ + { + "name": "self", + "type": 8 + } + ], + "results": [], + "stability": { + "type": "stable", + "since": "1.0.0" + } + } + }, + "package": 0 + }, + { + "name": "z", + "types": {}, + "functions": {}, + "package": 0 + }, + { + "name": null, + "types": {}, + "functions": {}, + "stability": { + "type": "stable", + "since": "1.0.0" + }, + "package": 0 + }, + { + "name": null, + "types": {}, + "functions": {}, + "stability": { + "type": "stable", + "since": "1.0.0" + }, + "package": 0 + } + ], + "types": [ + { + "name": "r1", + "kind": "resource", + "owner": { + "interface": 3 + }, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": "r2", + "kind": "resource", + "owner": { + "interface": 3 + }, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": "t1", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 3 + }, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": "t2", + "kind": { + "record": { + "fields": [ + { + "name": "a", + "type": "u32" + } + ] + } + }, + "owner": { + "interface": 3 + }, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": "t3", + "kind": { + "enum": { + "cases": [ + { + "name": "a" + } + ] + } + }, + "owner": { + "interface": 3 + }, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": "t4", + "kind": { + "flags": { + "flags": [ + { + "name": "a" + } + ] + } + }, + "owner": { + "interface": 3 + }, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": "t5", + "kind": { + "variant": { + "cases": [ + { + "name": "a", + "type": null + } + ] + } + }, + "owner": { + "interface": 3 + }, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": "r3", + "kind": "resource", + "owner": { + "interface": 3 + }, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": null, + "kind": { + "handle": { + "borrow": 7 + } + }, + "owner": null, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": "t1", + "kind": { + "record": { + "fields": [ + { + "name": "x", + "type": "u32" + } + ] + } + }, + "owner": { + "world": 2 + }, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": "t2", + "kind": { + "enum": { + "cases": [ + { + "name": "a" + } + ] + } + }, + "owner": { + "world": 2 + }, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": "t3", + "kind": { + "variant": { + "cases": [ + { + "name": "a", + "type": null + } + ] + } + }, + "owner": { + "world": 2 + }, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": "t4", + "kind": { + "flags": { + "flags": [ + { + "name": "a" + } + ] + } + }, + "owner": { + "world": 2 + }, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": "t5", + "kind": { + "type": "u32" + }, + "owner": { + "world": 2 + }, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": "t6", + "kind": "resource", + "owner": { + "world": 2 + }, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": "t7", + "kind": "resource", + "owner": { + "world": 2 + }, + "stability": { + "type": "stable", + "since": "1.0.0" + } + }, + { + "name": null, + "kind": { + "handle": { + "own": 7 + } + }, + "owner": null + }, + { + "name": null, + "kind": { + "handle": { + "own": 15 + } + }, + "owner": null + } + ], + "packages": [ + { + "name": "a:b@1.0.1", + "interfaces": { + "foo1": 0, + "foo2": 1, + "foo3": 2, + "in-an-interface": 3, + "z": 4 + }, + "worlds": { + "w1": 0, + "w2": 1, + "in-a-world": 2 + } + } + ] +} \ No newline at end of file diff --git a/testdata/wit-parser/since-and-unstable.wit.json.golden.wit b/testdata/wit-parser/since-and-unstable.wit.json.golden.wit new file mode 100644 index 00000000..4232ec16 --- /dev/null +++ b/testdata/wit-parser/since-and-unstable.wit.json.golden.wit @@ -0,0 +1,45 @@ +package a:b@1.0.1; + +interface foo1 {} + +interface foo2 {} + +interface foo3 {} + +interface in-an-interface { + resource r1; + resource r2; + type t1 = u32; + record t2 { a: u32 } + enum t3 { a } + flags t4 { a } + variant t5 { a } + resource r3 { + constructor(); + x2: func(); + x1: static func(); + } + foo: func(); +} + +interface z {} + +world w1 {} +world w2 {} +world in-a-world { + import y: interface {} + import z; + record t1 { x: u32 } + enum t2 { a } + variant t3 { a } + flags t4 { a } + type t5 = u32; + resource t6; + resource t7 { + constructor(); + } + import x: func(); + export x: func(); + export y: interface {} + export z; +} diff --git a/testdata/wit-parser/stress-export-elaborate.wit.json b/testdata/wit-parser/stress-export-elaborate.wit.json index bd8fdfce..d7781277 100644 --- a/testdata/wit-parser/stress-export-elaborate.wit.json +++ b/testdata/wit-parser/stress-export-elaborate.wit.json @@ -4,36 +4,56 @@ "name": "foo", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } }, "interface-3": { - "interface": 3 + "interface": { + "id": 3 + } }, "interface-4": { - "interface": 4 + "interface": { + "id": 4 + } }, "interface-5": { - "interface": 5 + "interface": { + "id": 5 + } }, "interface-6": { - "interface": 6 + "interface": { + "id": 6 + } }, "interface-7": { - "interface": 7 + "interface": { + "id": 7 + } }, "interface-8": { - "interface": 8 + "interface": { + "id": 8 + } } }, "exports": { "interface-9": { - "interface": 9 + "interface": { + "id": 9 + } } }, "package": 0 diff --git a/testdata/wit-parser/world-diamond.wit.json b/testdata/wit-parser/world-diamond.wit.json index 037141c3..6dba45ed 100644 --- a/testdata/wit-parser/world-diamond.wit.json +++ b/testdata/wit-parser/world-diamond.wit.json @@ -4,13 +4,19 @@ "name": "the-world", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } }, "a": { "function": { diff --git a/testdata/wit-parser/world-iface-no-collide.wit.json b/testdata/wit-parser/world-iface-no-collide.wit.json index b9ad0846..fffc1155 100644 --- a/testdata/wit-parser/world-iface-no-collide.wit.json +++ b/testdata/wit-parser/world-iface-no-collide.wit.json @@ -4,7 +4,9 @@ "name": "bar", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "t": { "type": 1 diff --git a/testdata/wit-parser/world-implicit-import1.wit.json b/testdata/wit-parser/world-implicit-import1.wit.json index f0b550b7..708c339b 100644 --- a/testdata/wit-parser/world-implicit-import1.wit.json +++ b/testdata/wit-parser/world-implicit-import1.wit.json @@ -4,13 +4,19 @@ "name": "the-world", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "bar": { - "interface": 1 + "interface": { + "id": 1 + } }, "foo": { - "interface": 2 + "interface": { + "id": 2 + } } }, "exports": {}, diff --git a/testdata/wit-parser/world-implicit-import2.wit.json b/testdata/wit-parser/world-implicit-import2.wit.json index 84e150e4..97049842 100644 --- a/testdata/wit-parser/world-implicit-import2.wit.json +++ b/testdata/wit-parser/world-implicit-import2.wit.json @@ -4,7 +4,9 @@ "name": "w", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "g": { "type": 1 diff --git a/testdata/wit-parser/world-implicit-import3.wit.json b/testdata/wit-parser/world-implicit-import3.wit.json index 3e716e41..159bee77 100644 --- a/testdata/wit-parser/world-implicit-import3.wit.json +++ b/testdata/wit-parser/world-implicit-import3.wit.json @@ -4,7 +4,9 @@ "name": "w", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "g": { "type": 1 diff --git a/testdata/wit-parser/world-same-fields4.wit.json b/testdata/wit-parser/world-same-fields4.wit.json index c15baf18..3be16acf 100644 --- a/testdata/wit-parser/world-same-fields4.wit.json +++ b/testdata/wit-parser/world-same-fields4.wit.json @@ -4,15 +4,21 @@ "name": "foo", "imports": { "shared-items": { - "interface": 1 + "interface": { + "id": 1 + } }, "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } } }, "exports": { "a-name": { - "interface": 2 + "interface": { + "id": 2 + } } }, "package": 0 diff --git a/testdata/wit-parser/world-top-level-resources.wit.json b/testdata/wit-parser/world-top-level-resources.wit.json index a4ef0416..794e699f 100644 --- a/testdata/wit-parser/world-top-level-resources.wit.json +++ b/testdata/wit-parser/world-top-level-resources.wit.json @@ -4,15 +4,21 @@ "name": "proxy", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "exports": { "interface-1": { - "interface": 1 + "interface": { + "id": 1 + } } }, "package": 0 diff --git a/testdata/wit-parser/worlds-union-dedup.wit.json b/testdata/wit-parser/worlds-union-dedup.wit.json index 7fb8ff0a..d1e24e97 100644 --- a/testdata/wit-parser/worlds-union-dedup.wit.json +++ b/testdata/wit-parser/worlds-union-dedup.wit.json @@ -4,10 +4,14 @@ "name": "my-world-a", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } } }, "exports": {}, @@ -17,10 +21,14 @@ "name": "my-world-b", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } } }, "exports": {}, @@ -30,10 +38,14 @@ "name": "union-my-world", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "interface-2": { - "interface": 2 + "interface": { + "id": 2 + } } }, "exports": {}, diff --git a/testdata/wit-parser/worlds-with-types.wit.json b/testdata/wit-parser/worlds-with-types.wit.json index 9ca60c0a..70e857d6 100644 --- a/testdata/wit-parser/worlds-with-types.wit.json +++ b/testdata/wit-parser/worlds-with-types.wit.json @@ -35,7 +35,9 @@ "name": "bar", "imports": { "interface-0": { - "interface": 0 + "interface": { + "id": 0 + } }, "t": { "type": 3 From 8c862aee5315a7907bd8e64568b88b73072ca00c Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Sat, 8 Jun 2024 14:56:45 -0400 Subject: [PATCH 05/15] wit: rename InterfaceStability to InterfaceRef --- wit/bindgen/generator.go | 4 ++-- wit/codec.go | 4 ++-- wit/docs.go | 2 +- wit/resolve.go | 6 +++--- wit/wit.go | 14 +++++++------- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/wit/bindgen/generator.go b/wit/bindgen/generator.go index 45cec74b..037716bd 100644 --- a/wit/bindgen/generator.go +++ b/wit/bindgen/generator.go @@ -251,7 +251,7 @@ func (g *generator) defineWorld(w *wit.World) error { var err error w.Imports.All()(func(name string, v wit.WorldItem) bool { switch v := v.(type) { - case *wit.InterfaceStability: + case *wit.InterfaceRef: // TODO: handle Stability err = g.defineInterface(wit.Imported, v.Interface, name) case *wit.TypeDef: @@ -269,7 +269,7 @@ func (g *generator) defineWorld(w *wit.World) error { w.Exports.All()(func(name string, v wit.WorldItem) bool { switch v := v.(type) { - case *wit.InterfaceStability: + case *wit.InterfaceRef: // TODO: handle Stability err = g.defineInterface(wit.Exported, v.Interface, name) case *wit.TypeDef: diff --git a/wit/codec.go b/wit/codec.go index 43515573..d3f934f5 100644 --- a/wit/codec.go +++ b/wit/codec.go @@ -204,7 +204,7 @@ func (pn *Ident) DecodeString(s string) error { // DecodeField implements the [codec.FieldDecoder] interface // to decode a struct or JSON object. -func (i *InterfaceStability) DecodeField(dec codec.Decoder, name string) error { +func (i *InterfaceRef) DecodeField(dec codec.Decoder, name string) error { switch name { case "id": return dec.Decode(&i.Interface) @@ -234,7 +234,7 @@ func (c *worldItemCodec) DecodeField(dec codec.Decoder, name string) error { var err error switch name { case "interface": - v := &InterfaceStability{} + v := &InterfaceRef{} err = dec.Decode(v) *c.v = v case "function": diff --git a/wit/docs.go b/wit/docs.go index c77927db..c286a604 100644 --- a/wit/docs.go +++ b/wit/docs.go @@ -10,7 +10,7 @@ // // Types that are represented as Rust enums are implemented via sealed Go interfaces, implemented by other types // in this package. An example is [WorldItem], which is the set of types that a [World] may -// import or export, currently [InterfaceStability], [TypeDef], and [Function]. +// import or export, currently [InterfaceRef], [TypeDef], and [Function]. // // # JSON // diff --git a/wit/resolve.go b/wit/resolve.go index 1f0466b7..9ba406b0 100644 --- a/wit/resolve.go +++ b/wit/resolve.go @@ -90,7 +90,7 @@ func (w *World) AllFunctions() iterate.Seq[*Function] { } // A WorldItem is any item that can be exported from or imported into a [World], -// currently either an [InterfaceStability], [TypeDef], or [Function]. +// currently either an [InterfaceRef], [TypeDef], or [Function]. // Any WorldItem is also a [Node]. type WorldItem interface { Node @@ -102,9 +102,9 @@ type _worldItem struct{} func (_worldItem) isWorldItem() {} -// An InterfaceStability represents a reference to an [Interface] with a [Stability] attribute. +// An InterfaceRef represents a reference to an [Interface] with a [Stability] attribute. // It implements the [Node] and [WorldItem] interfaces. -type InterfaceStability struct { +type InterfaceRef struct { _worldItem Interface *Interface diff --git a/wit/wit.go b/wit/wit.go index 0683c97b..e5864df8 100644 --- a/wit/wit.go +++ b/wit/wit.go @@ -195,7 +195,7 @@ func (w *World) WIT(ctx Node, name string) string { func (w *World) itemWIT(motion, name string, v WorldItem) string { switch v := v.(type) { - case *InterfaceStability, *Function: + case *InterfaceRef, *Function: return motion + " " + v.WIT(w, name) // TODO: handle resource methods? case *TypeDef: return v.WIT(w, name) // no motion, in Imports only @@ -204,16 +204,16 @@ func (w *World) itemWIT(motion, name string, v WorldItem) string { } // WITKind returns the WIT kind. -func (*InterfaceStability) WITKind() string { return "interface stability" } +func (*InterfaceRef) WITKind() string { return "interface ref" } -// WIT returns the [WIT] text format for [InterfaceStability] i. +// WIT returns the [WIT] text format for [InterfaceRef] i. // // [WIT]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md -func (i *InterfaceStability) WIT(ctx Node, name string) string { - if i.Stability == nil { - return i.Interface.WIT(ctx, name) +func (ref *InterfaceRef) WIT(ctx Node, name string) string { + if ref.Stability == nil { + return ref.Interface.WIT(ctx, name) } - return i.Stability.WIT(ctx, "") + "\n" + i.Interface.WIT(ctx, name) + return ref.Stability.WIT(ctx, "") + "\n" + ref.Interface.WIT(ctx, name) } // WITKind returns the WIT kind. From c3d2cf332045cafaf5c11ee650f833ae780f4ed7 Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Sat, 8 Jun 2024 15:09:11 -0400 Subject: [PATCH 06/15] wit: add Stability attribute to Interface, TypeDef, and Function --- wit/resolve.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/wit/resolve.go b/wit/resolve.go index 9ba406b0..a7ab8834 100644 --- a/wit/resolve.go +++ b/wit/resolve.go @@ -123,10 +123,9 @@ type Interface struct { Name *string TypeDefs ordered.Map[string, *TypeDef] Functions ordered.Map[string, *Function] - - // The [Package] that this Interface belongs to. It must be non-nil when fully resolved. - Package *Package - Docs Docs + Package *Package // the Package this Interface belongs to + Stability Stability // WIT @since or @unstable (nil if unknown) + Docs Docs } // AllFunctions returns a [sequence] that yields each [Function] in an [Interface]. @@ -147,10 +146,11 @@ func (i *Interface) AllFunctions() iterate.Seq[*Function] { type TypeDef struct { _type _worldItem - Name *string - Kind TypeDefKind - Owner TypeOwner - Docs Docs + Name *string + Kind TypeDefKind + Owner TypeOwner + Stability Stability // WIT @since or @unstable (nil if unknown) + Docs Docs } // TypeName returns the [WIT] type name for t. @@ -1289,11 +1289,12 @@ type String struct{ _primitive[string] } // [function]: https://component-model.bytecodealliance.org/design/wit.html#functions type Function struct { _worldItem - Name string - Kind FunctionKind - Params []Param // arguments to the function - Results []Param // a function can have a single anonymous result, or > 1 named results - Docs Docs + Name string + Kind FunctionKind + Params []Param // arguments to the function + Results []Param // a function can have a single anonymous result, or > 1 named results + Stability Stability // WIT @since or @unstable (nil if unknown) + Docs Docs } // BaseName returns the base name of [Function] f. From fda64e7461316e8af2a0b49947d2070de8a8477e Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Sat, 8 Jun 2024 15:09:47 -0400 Subject: [PATCH 07/15] wit: remove bracketed links to [Type] in struct field doc comments --- wit/resolve.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wit/resolve.go b/wit/resolve.go index a7ab8834..629bb5a3 100644 --- a/wit/resolve.go +++ b/wit/resolve.go @@ -736,7 +736,7 @@ func (v *Variant) hasResource() bool { // It implements the [Node] interface. type Case struct { Name string - Type Type // optional associated Type (can be nil) + Type Type // optional associated [Type] (can be nil) Docs Docs } @@ -855,8 +855,8 @@ func (o *Option) Flat() []Type { // [result type]: https://component-model.bytecodealliance.org/design/wit.html#results type Result struct { _typeDefKind - OK Type // optional associated Type (can be nil) - Err Type // optional associated Type (can be nil) + OK Type // optional associated [Type] (can be nil) + Err Type // optional associated [Type] (can be nil) } // Despecialize despecializes [Result] o into a [Variant] with two cases, "ok" and "error". From 1e2de1233579d7a8c6cda81303b3c0e0d1e83422 Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Sat, 8 Jun 2024 15:32:23 -0400 Subject: [PATCH 08/15] Makefile, testdata: mimic the tests/ui harness in wasm-parser Enable --features active to follow wasm-tools tests. See https://github.com/alexcrichton/wasm-tools/commit/8ebbf90d55b6a91466114d705780586f01e16292 and https://github.com/bytecodealliance/wasm-tools/pull/1508. --- Makefile | 2 +- testdata/wit-parser/feature-gates.wit | 118 +++++++++++++++++++++ testdata/wit-parser/since-and-unstable.wit | 89 ++++++++++++++++ 3 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 testdata/wit-parser/feature-gates.wit create mode 100644 testdata/wit-parser/since-and-unstable.wit diff --git a/Makefile b/Makefile index c249cd80..cf09fc88 100644 --- a/Makefile +++ b/Makefile @@ -5,4 +5,4 @@ json: $(wit_files) .PHONY: $(wit_files) $(wit_files): - wasm-tools component wit -j $@ > $@.json + wasm-tools component wit -j --features active $@ > $@.json diff --git a/testdata/wit-parser/feature-gates.wit b/testdata/wit-parser/feature-gates.wit new file mode 100644 index 00000000..019471d3 --- /dev/null +++ b/testdata/wit-parser/feature-gates.wit @@ -0,0 +1,118 @@ +package a:b; + +@unstable(feature = not-active) +interface gated { +} + +@unstable(feature = active) +interface ungated { + @unstable(feature = not-active) + gated: func(); + + @unstable(feature = active) + ungated: func(); +} + +@unstable(feature = active) +interface ungated2 { + @unstable(feature = not-active) + type gated = u32; + @unstable(feature = not-active) + type gated2 = gated; + + @unstable(feature = not-active) + type gated-with-anonymous-type = option>; + + @unstable(feature = active) + type ungated = u32; + @unstable(feature = active) + type ungated2 = ungated; +} + +@unstable(feature = inactive) +interface gated-use-target { + @unstable(feature = inactive) + type t = u32; +} + +@unstable(feature = inactive) +interface gated-use { + @unstable(feature = inactive) + use gated-use-target.{t}; +} + +@unstable(feature = active) +interface ungated-use-target { + @unstable(feature = active) + type t = u32; +} + +@unstable(feature = active) +interface ungated-use { + @unstable(feature = active) + use ungated-use-target.{t}; +} + +@unstable(feature = inactive) +interface gated-for-world {} + +@unstable(feature = inactive) +world gated-world { + @unstable(feature = inactive) + import gated-for-world; + @unstable(feature = inactive) + export gated-for-world; +} + +@unstable(feature = active) +interface ungated-for-world {} + +@unstable(feature = active) +world ungated-world { + @unstable(feature = active) + import ungated-for-world; + @unstable(feature = active) + export ungated-for-world; +} + +world mixed-world { + @unstable(feature = inactive) + import gated-for-world; + @unstable(feature = inactive) + export gated-for-world; + @unstable(feature = inactive) + include gated-world; + + @unstable(feature = active) + import ungated-for-world; + @unstable(feature = active) + export ungated-for-world; + @unstable(feature = inactive) + include ungated-world; +} + +interface with-resources { + @unstable(feature = inactive) + resource gated { + @unstable(feature = inactive) + constructor(); + + @unstable(feature = inactive) + x: static func(); + + @unstable(feature = inactive) + y: func(); + } + + @unstable(feature = active) + resource ungated { + @unstable(feature = active) + constructor(); + + @unstable(feature = active) + x: static func(); + + @unstable(feature = active) + y: func(); + } +} diff --git a/testdata/wit-parser/since-and-unstable.wit b/testdata/wit-parser/since-and-unstable.wit new file mode 100644 index 00000000..130d34a6 --- /dev/null +++ b/testdata/wit-parser/since-and-unstable.wit @@ -0,0 +1,89 @@ +package a:b@1.0.1; + +@since(version = 1.0.0) +interface foo1 {} + +@since(version = 1.0.0, feature = foo) +interface foo2 {} + +@since(version = 1.0.0, feature = foo-bar) +interface foo3 {} + +@unstable(feature = foo2) +interface foo4 {} + +@since(version = 1.0.1) +world w1 {} + +@since(version = 1.0.0) +world w2 {} + +interface in-an-interface { + @since(version = 1.0.0) + foo: func(); + @since(version = 1.0.0) + resource r1; + @since(version = 1.0.0) + resource r2 {} + + @since(version = 1.0.0) + type t1 = u32; + @since(version = 1.0.0) + record t2 { a: u32 } + @since(version = 1.0.0) + enum t3 { a } + @since(version = 1.0.0) + flags t4 { a } + @since(version = 1.0.0) + variant t5 { a } + + @since(version = 1.0.0) + resource r3 { + @since(version = 1.0.0) + constructor(); + + @since(version = 1.0.0) + x1: static func(); + + @since(version = 1.0.0) + x2: func(); + } +} + +interface z {} + +world in-a-world { + @since(version = 1.0.0) + import x: func(); + @since(version = 1.0.0) + export x: func(); + + @since(version = 1.0.0) + import y: interface {} + @since(version = 1.0.0) + export y: interface {} + + @since(version = 1.0.0) + import z; + @since(version = 1.0.0) + export z; + + + @since(version = 1.0.0) + record t1 { x: u32 } + @since(version = 1.0.0) + enum t2 { a } + @since(version = 1.0.0) + variant t3 { a } + @since(version = 1.0.0) + flags t4 { a } + @since(version = 1.0.0) + type t5 = u32; + @since(version = 1.0.0) + resource t6; + @since(version = 1.0.0) + resource t7 { + @since(version = 1.0.0) + constructor(); + } +} From ee0fdb87de3e706f6d54c5463a77d788428a4d6f Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Sat, 8 Jun 2024 17:49:24 -0400 Subject: [PATCH 09/15] wit: support encoding and decoding of WIT @since and @unstable directives --- .../feature-gates.wit.json.golden.wit | 20 +++++++ .../since-and-unstable.wit.json.golden.wit | 33 +++++++++++ wit/codec.go | 56 +++++++++++++++++- wit/load.go | 2 +- wit/resolve.go | 13 ++-- wit/testdata_test.go | 4 +- wit/wit.go | 59 +++++++++++++++++-- 7 files changed, 170 insertions(+), 17 deletions(-) diff --git a/testdata/wit-parser/feature-gates.wit.json.golden.wit b/testdata/wit-parser/feature-gates.wit.json.golden.wit index 8f205413..24100060 100644 --- a/testdata/wit-parser/feature-gates.wit.json.golden.wit +++ b/testdata/wit-parser/feature-gates.wit.json.golden.wit @@ -1,37 +1,57 @@ package a:b; +@unstable(feature = active) interface ungated { + @unstable(feature = active) ungated: func(); } +@unstable(feature = active) interface ungated2 { + @unstable(feature = active) type ungated = u32; + @unstable(feature = active) type ungated2 = ungated; } +@unstable(feature = active) interface ungated-use-target { + @unstable(feature = active) type t = u32; } +@unstable(feature = active) interface ungated-use { + @unstable(feature = active) use ungated-use-target.{t}; } +@unstable(feature = active) interface ungated-for-world {} interface with-resources { + @unstable(feature = active) resource ungated { + @unstable(feature = active) constructor(); + @unstable(feature = active) y: func(); + @unstable(feature = active) x: static func(); } } +@unstable(feature = active) world ungated-world { + @unstable(feature = active) import ungated-for-world; + @unstable(feature = active) export ungated-for-world; } + world mixed-world { + @unstable(feature = active) import ungated-for-world; + @unstable(feature = active) export ungated-for-world; } diff --git a/testdata/wit-parser/since-and-unstable.wit.json.golden.wit b/testdata/wit-parser/since-and-unstable.wit.json.golden.wit index 4232ec16..a4423a7a 100644 --- a/testdata/wit-parser/since-and-unstable.wit.json.golden.wit +++ b/testdata/wit-parser/since-and-unstable.wit.json.golden.wit @@ -1,45 +1,78 @@ package a:b@1.0.1; +@since(version = 1.0.0) interface foo1 {} +@since(version = 1.0.0, feature = foo) interface foo2 {} +@since(version = 1.0.0, feature = foo-bar) interface foo3 {} interface in-an-interface { + @since(version = 1.0.0) resource r1; + @since(version = 1.0.0) resource r2; + @since(version = 1.0.0) type t1 = u32; + @since(version = 1.0.0) record t2 { a: u32 } + @since(version = 1.0.0) enum t3 { a } + @since(version = 1.0.0) flags t4 { a } + @since(version = 1.0.0) variant t5 { a } + @since(version = 1.0.0) resource r3 { + @since(version = 1.0.0) constructor(); + @since(version = 1.0.0) x2: func(); + @since(version = 1.0.0) x1: static func(); } + @since(version = 1.0.0) foo: func(); } interface z {} +@since(version = 1.0.1) world w1 {} + +@since(version = 1.0.0) world w2 {} + world in-a-world { + @since(version = 1.0.0) import y: interface {} + @since(version = 1.0.0) import z; + @since(version = 1.0.0) record t1 { x: u32 } + @since(version = 1.0.0) enum t2 { a } + @since(version = 1.0.0) variant t3 { a } + @since(version = 1.0.0) flags t4 { a } + @since(version = 1.0.0) type t5 = u32; + @since(version = 1.0.0) resource t6; + @since(version = 1.0.0) resource t7 { + @since(version = 1.0.0) constructor(); } + @since(version = 1.0.0) import x: func(); + @since(version = 1.0.0) export x: func(); + @since(version = 1.0.0) export y: interface {} + @since(version = 1.0.0) export z; } diff --git a/wit/codec.go b/wit/codec.go index d3f934f5..1b99b156 100644 --- a/wit/codec.go +++ b/wit/codec.go @@ -3,6 +3,7 @@ package wit import ( "io" + "github.com/coreos/go-semver/semver" "github.com/ydnar/wasm-tools-go/internal/codec" "github.com/ydnar/wasm-tools-go/internal/codec/json" ) @@ -40,7 +41,7 @@ func (res *Resolve) ResolveCodec(v any) codec.Codec { case *Handle: return &handleCodec{v} case *Stability: - return &stabilityCodec{v} + return &stabilityCodec{v: v} case *Type: return &typeCodec{v, res} case *TypeDefKind: @@ -49,6 +50,12 @@ func (res *Resolve) ResolveCodec(v any) codec.Codec { return &typeOwnerCodec{v} case *WorldItem: return &worldItemCodec{v} + + // Imported + case *semver.Version: + return &semverCodec{&v} + case **semver.Version: + return &semverCodec{v} } return nil @@ -108,6 +115,8 @@ func (c *worldCodec) DecodeField(dec codec.Decoder, name string) error { return dec.Decode(&w.Exports) case "package": return dec.Decode(&w.Package) + case "stability": + return dec.Decode(&w.Stability) case "docs": return dec.Decode(&w.Docs) } @@ -136,6 +145,8 @@ func (c *interfaceCodec) DecodeField(dec codec.Decoder, name string) error { return dec.Decode(&i.Functions) case "package": return dec.Decode(&i.Package) + case "stability": + return dec.Decode(&i.Stability) case "docs": return dec.Decode(&i.Docs) } @@ -162,6 +173,8 @@ func (c *typeDefCodec) DecodeField(dec codec.Decoder, name string) error { return dec.Decode(&t.Name) case "owner": return dec.Decode(&t.Owner) + case "stability": + return dec.Decode(&t.Stability) case "docs": return dec.Decode(&t.Docs) } @@ -459,7 +472,9 @@ func (c *handleCodec) DecodeField(dec codec.Decoder, name string) error { } type stabilityCodec struct { - v *Stability + v *Stability + since semver.Version + feature string } func (c *stabilityCodec) DecodeField(dec codec.Decoder, name string) error { @@ -473,10 +488,45 @@ func (c *stabilityCodec) DecodeField(dec codec.Decoder, name string) error { v := &Unstable{} err = dec.Decode(v) *c.v = v + + // Additional fields for serde tag="type" representation. + // TODO: remove this if the JSON format changes. + case "type": + var typ string + err = dec.Decode(&typ) + switch typ { + case "stable": + *c.v = &Stable{Since: c.since, Feature: c.feature} + case "unstable": + *c.v = &Unstable{Feature: c.feature} + } + case "since": + err = dec.Decode(&c.since) + switch v := (*c.v).(type) { + case *Stable: + v.Since = c.since + } + case "feature": + err = dec.Decode(&c.feature) + switch v := (*c.v).(type) { + case *Stable: + v.Feature = c.feature + case *Unstable: + v.Feature = c.feature + } } return err } +type semverCodec struct { + v **semver.Version +} + +func (c *semverCodec) DecodeString(s string) error { + codec.Must(c.v) + return (*c.v).Set(s) +} + // DecodeField implements the [codec.FieldDecoder] interface // to decode a struct or JSON object. func (e *Enum) DecodeField(dec codec.Decoder, name string) error { @@ -535,6 +585,8 @@ func (f *Function) DecodeField(dec codec.Decoder, name string) error { return codec.DecodeSlice(dec, &f.Params) case "results": return codec.DecodeSlice(dec, &f.Results) + case "stability": + return dec.Decode(&f.Stability) case "docs": return dec.Decode(&f.Docs) } diff --git a/wit/load.go b/wit/load.go index 4db3ff50..5a58586c 100644 --- a/wit/load.go +++ b/wit/load.go @@ -39,7 +39,7 @@ func LoadWIT(path string) (*Resolve, error) { var stdout bytes.Buffer var stderr bytes.Buffer - cmd := exec.Command(wasmTools, "component", "wit", "-j") + cmd := exec.Command(wasmTools, "component", "wit", "-j", "--features", "active") cmd.Stdout = &stdout cmd.Stderr = &stderr if path == "" || path == "-" { diff --git a/wit/resolve.go b/wit/resolve.go index 629bb5a3..fdb4e5a4 100644 --- a/wit/resolve.go +++ b/wit/resolve.go @@ -54,13 +54,12 @@ func (r *Resolve) AllFunctions() iterate.Seq[*Function] { type World struct { _typeOwner - Name string - Imports ordered.Map[string, WorldItem] - Exports ordered.Map[string, WorldItem] - - // The [Package] that this World belongs to. It must be non-nil when fully resolved. - Package *Package - Docs Docs + Name string + Imports ordered.Map[string, WorldItem] + Exports ordered.Map[string, WorldItem] + Package *Package // the Package this World belongs to (must be non-nil) + Stability Stability // WIT @since or @unstable (nil if unknown) + Docs Docs } // AllFunctions returns a [sequence] that yields each [Function] in a [World]. diff --git a/wit/testdata_test.go b/wit/testdata_test.go index 398685b9..327faf58 100644 --- a/wit/testdata_test.go +++ b/wit/testdata_test.go @@ -63,7 +63,7 @@ func TestGoldenWITFiles(t *testing.T) { } var canWasmTools = sync.OnceValue[bool](func() bool { - err := exec.Command("wasm-tools", "--version").Run() + _, err := exec.LookPath("wasm-tools") return err == nil }) @@ -83,7 +83,7 @@ func TestGoldenWITRoundTrip(t *testing.T) { } t.Run(path, func(t *testing.T) { // Run the generated WIT through wasm-tools to generate JSON. - cmd := exec.Command("wasm-tools", "component", "wit", "-j") + cmd := exec.Command("wasm-tools", "component", "wit", "-j", "--features", "active") cmd.Stdin = strings.NewReader(data) stdout := &bytes.Buffer{} stderr := &bytes.Buffer{} diff --git a/wit/wit.go b/wit/wit.go index e5864df8..1303e3ac 100644 --- a/wit/wit.go +++ b/wit/wit.go @@ -162,6 +162,10 @@ func (w *World) WIT(ctx Node, name string) string { } var b strings.Builder b.WriteString(w.Docs.WIT(ctx, "")) + if w.Stability != nil { + b.WriteString(w.Stability.WIT(ctx, "")) + b.WriteRune('\n') + } b.WriteString("world ") b.WriteString(escape(name)) // TODO: compare to w.Name? b.WriteString(" {") @@ -175,7 +179,8 @@ func (w *World) WIT(ctx Node, name string) string { if n == 0 { b.WriteRune('\n') } - b.WriteString(indent(w.itemWIT("import", name, i))) + // b.WriteString(indent(w.itemWIT("import", name, i))) + b.WriteString(indent(i.WIT(worldImport{w}, name))) b.WriteRune('\n') n++ return true @@ -184,7 +189,8 @@ func (w *World) WIT(ctx Node, name string) string { if n == 0 { b.WriteRune('\n') } - b.WriteString(indent(w.itemWIT("export", name, i))) + // b.WriteString(indent(w.itemWIT("export", name, i))) + b.WriteString(indent(i.WIT(worldExport{w}, name))) b.WriteRune('\n') n++ return true @@ -193,6 +199,11 @@ func (w *World) WIT(ctx Node, name string) string { return b.String() } +type ( + worldImport struct{ *World } + worldExport struct{ *World } +) + func (w *World) itemWIT(motion, name string, v WorldItem) string { switch v := v.(type) { case *InterfaceRef, *Function: @@ -232,17 +243,35 @@ func (i *Interface) WIT(ctx Node, name string) string { switch ctx := ctx.(type) { case *Package: b.WriteString(i.Docs.WIT(ctx, "")) + if i.Stability != nil { + b.WriteString(i.Stability.WIT(ctx, "")) + b.WriteRune('\n') + } b.WriteString("interface ") b.WriteString(escape(name)) b.WriteRune(' ') - case *World: + + case worldImport: rname := relativeName(i, ctx.Package) if rname != "" { - return escape(rname) + ";" + return "import " + escape(rname) + ";" } // Otherwise, this is an inline interface decl. b.WriteString(i.Docs.WIT(ctx, "")) + b.WriteString("import ") + b.WriteString(escape(name)) + b.WriteString(": interface ") + + case worldExport: + rname := relativeName(i, ctx.Package) + if rname != "" { + return "export " + escape(rname) + ";" + } + + // Otherwise, this is an inline interface decl. + b.WriteString(i.Docs.WIT(ctx, "")) + b.WriteString("export ") b.WriteString(escape(name)) b.WriteString(": interface ") } @@ -325,9 +354,19 @@ func (t *TypeDef) WIT(ctx Node, name string) string { } return fmt.Sprintf("use %s.{%s};", ownerName, escape(name)) - case *World, *Interface: + case worldImport, worldExport, *Interface: var b strings.Builder b.WriteString(t.Docs.WIT(ctx, "")) + if t.Stability != nil { + b.WriteString(t.Stability.WIT(ctx, "")) + b.WriteRune('\n') + } + switch ctx.(type) { + case *worldImport: + b.WriteString("import ") + case *worldExport: + b.WriteString("import ") + } b.WriteString(t.Kind.WIT(t, name)) constructor := t.Constructor() methods := t.Methods() @@ -852,6 +891,16 @@ func (f *Function) WIT(ctx Node, name string) string { var b strings.Builder if ctx != nil { b.WriteString(f.Docs.WIT(ctx, "")) + if f.Stability != nil { + b.WriteString(f.Stability.WIT(ctx, "")) + b.WriteRune('\n') + } + } + switch ctx.(type) { + case worldImport: + b.WriteString("import ") + case worldExport: + b.WriteString("export ") } b.WriteString(escape(name)) var isConstructor, isMethod bool From 9f8f462e52bbb7e9340a2c9a28b7f9b5d1e70a4c Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Sat, 8 Jun 2024 18:07:06 -0400 Subject: [PATCH 10/15] wit: sort Packages in a Resolve when serializing to WIT --- wit/wit.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/wit/wit.go b/wit/wit.go index 1303e3ac..cfecccd6 100644 --- a/wit/wit.go +++ b/wit/wit.go @@ -55,8 +55,12 @@ func (*Resolve) WITKind() string { return "resolve" } // // [WIT]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md func (r *Resolve) WIT(_ Node, _ string) string { + packages := slices.Clone(r.Packages) + slices.SortFunc(packages, func(a, b *Package) int { + return strings.Compare(a.Name.String(), b.Name.String()) + }) var b strings.Builder - for i, p := range r.Packages { + for i, p := range packages { if i > 0 { b.WriteRune('\n') b.WriteRune('\n') From a5669f77a576358eaffd3dc84c4c5d9e163a331b Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Sat, 8 Jun 2024 18:20:17 -0400 Subject: [PATCH 11/15] testdata: sort multi-package golden files by package name --- testdata/wasi/cli.wit.json.golden.wit | 1336 +++--- testdata/wasi/http.wit.json.golden.wit | 3730 ++++++++--------- ...cross-package-resource.wit.json.golden.wit | 10 +- .../foreign-deps-union.wit.json.golden.wit | 62 +- .../foreign-deps.wit.json.golden.wit | 52 +- .../kinds-of-deps.wit.json.golden.wit | 28 +- ...ti-package-shared-deps.wit.json.golden.wit | 16 +- ...ackage-transitive-deps.wit.json.golden.wit | 14 +- ...both-resource-and-type.wit.json.golden.wit | 14 +- ...it-internal-references.wit.json.golden.wit | 14 +- 10 files changed, 2638 insertions(+), 2638 deletions(-) diff --git a/testdata/wasi/cli.wit.json.golden.wit b/testdata/wasi/cli.wit.json.golden.wit index e496f5a1..5c5b4c92 100644 --- a/testdata/wasi/cli.wit.json.golden.wit +++ b/testdata/wasi/cli.wit.json.golden.wit @@ -1,312 +1,158 @@ -package wasi:io@0.2.0; +package wasi:cli@0.2.0; -interface error { - /// A resource which represents some error information. - /// - /// The only method provided by this resource is `to-debug-string`, - /// which provides some human-readable information about the error. - /// - /// In the `wasi:io` package, this resource is returned through the - /// `wasi:io/streams/stream-error` type. +interface environment { + /// Get the POSIX-style environment variables. /// - /// To provide more specific error information, other interfaces may - /// provide functions to further "downcast" this error into more specific - /// error information. For example, `error`s returned in streams derived - /// from filesystem types to be described using the filesystem's own - /// error-code type, using the function - /// `wasi:filesystem/types/filesystem-error-code`, which takes a parameter - /// `borrow` and returns - /// `option`. + /// Each environment variable is provided as a pair of string variable names + /// and string value. /// - /// The set of functions which can "downcast" an `error` into a more - /// concrete type is open. - resource error { - - /// Returns a string that is suitable to assist humans in debugging - /// this error. - /// - /// WARNING: The returned string should not be consumed mechanically! - /// It may change across platforms, hosts, or other implementation - /// details. Parsing this string is a major platform-compatibility - /// hazard. - to-debug-string: func() -> string; - } -} - -/// A poll API intended to let users wait for I/O events on multiple handles -/// at once. -interface poll { - /// `pollable` represents a single I/O event which may be ready, or not. - resource pollable { - - /// `block` returns immediately if the pollable is ready, and otherwise - /// blocks until ready. - /// - /// This function is equivalent to calling `poll.poll` on a list - /// containing only this pollable. - block: func(); + /// Morally, these are a value import, but until value imports are available + /// in the component model, this import function should return the same + /// values each time it is called. + get-environment: func() -> list>; - /// Return the readiness of a pollable. This function never blocks. - /// - /// Returns `true` when the pollable is ready, and `false` otherwise. - ready: func() -> bool; - } + /// Get the POSIX-style arguments to the program. + get-arguments: func() -> list; - /// Poll for completion on a set of pollables. - /// - /// This function takes a list of pollables, which identify I/O sources of - /// interest, and waits until one or more of the events is ready for I/O. - /// - /// The result `list` contains one or more indices of handles in the - /// argument list that is ready for I/O. - /// - /// If the list contains more elements than can be indexed with a `u32` - /// value, this function traps. - /// - /// A timeout can be implemented by adding a pollable from the - /// wasi-clocks API to the list. - /// - /// This function does not return a `result`; polling in itself does not - /// do any I/O so it doesn't fail. If any of the I/O sources identified by - /// the pollables has an error, it is indicated by marking the source as - /// being reaedy for I/O. - poll: func(in: list>) -> list; + /// Return a path that programs should use as their initial current working + /// directory, interpreting `.` as shorthand for this. + initial-cwd: func() -> option; } -/// WASI I/O is an I/O abstraction API which is currently focused on providing -/// stream types. -/// -/// In the future, the component model is expected to add built-in stream types; -/// when it does, they are expected to subsume this API. -interface streams { - use error.{error}; - use poll.{pollable}; - - /// An error for input-stream and output-stream operations. - variant stream-error { - /// The last operation (a write or flush) failed before completion. - /// - /// More information is available in the `error` payload. - last-operation-failed(error), - /// The stream is closed: no more input will be accepted by the - /// stream. A closed output-stream will return this error on all - /// future operations. - closed, - } - - /// An input bytestream. - /// - /// `input-stream`s are *non-blocking* to the extent practical on underlying - /// platforms. I/O operations always return promptly; if fewer bytes are - /// promptly available than requested, they return the number of bytes promptly - /// available, which could even be zero. To wait for data to be available, - /// use the `subscribe` function to obtain a `pollable` which can be polled - /// for using `wasi:io/poll`. - resource input-stream { - - /// Read bytes from a stream, after blocking until at least one byte can - /// be read. Except for blocking, behavior is identical to `read`. - blocking-read: func(len: u64) -> result, stream-error>; - - /// Skip bytes from a stream, after blocking until at least one byte - /// can be skipped. Except for blocking behavior, identical to `skip`. - blocking-skip: func(len: u64) -> result; - - /// Perform a non-blocking read from the stream. - /// - /// When the source of a `read` is binary data, the bytes from the source - /// are returned verbatim. When the source of a `read` is known to the - /// implementation to be text, bytes containing the UTF-8 encoding of the - /// text are returned. - /// - /// This function returns a list of bytes containing the read data, - /// when successful. The returned list will contain up to `len` bytes; - /// it may return fewer than requested, but not more. The list is - /// empty when no bytes are available for reading at this time. The - /// pollable given by `subscribe` will be ready when more bytes are - /// available. - /// - /// This function fails with a `stream-error` when the operation - /// encounters an error, giving `last-operation-failed`, or when the - /// stream is closed, giving `closed`. - /// - /// When the caller gives a `len` of 0, it represents a request to - /// read 0 bytes. If the stream is still open, this call should - /// succeed and return an empty list, or otherwise fail with `closed`. - /// - /// The `len` parameter is a `u64`, which could represent a list of u8 which - /// is not possible to allocate in wasm32, or not desirable to allocate as - /// as a return value by the callee. The callee may return a list of bytes - /// less than `len` in size while more bytes are available for reading. - read: func(len: u64) -> result, stream-error>; - - /// Skip bytes from a stream. Returns number of bytes skipped. - /// - /// Behaves identical to `read`, except instead of returning a list - /// of bytes, returns the number of bytes consumed from the stream. - skip: func(len: u64) -> result; - - /// Create a `pollable` which will resolve once either the specified stream - /// has bytes available to read or the other end of the stream has been - /// closed. - /// The created `pollable` is a child resource of the `input-stream`. - /// Implementations may trap if the `input-stream` is dropped before - /// all derived `pollable`s created with this function are dropped. - subscribe: func() -> pollable; - } +interface exit { + /// Exit the current instance and any linked instances. + exit: func(status: result); +} - /// An output bytestream. - /// - /// `output-stream`s are *non-blocking* to the extent practical on - /// underlying platforms. Except where specified otherwise, I/O operations also - /// always return promptly, after the number of bytes that can be written - /// promptly, which could even be zero. To wait for the stream to be ready to - /// accept data, the `subscribe` function to obtain a `pollable` which can be - /// polled for using `wasi:io/poll`. - resource output-stream { +interface run { + /// Run the program. + run: func() -> result; +} - /// Request to flush buffered output, and block until flush completes - /// and stream is ready for writing again. - blocking-flush: func() -> result<_, stream-error>; +interface stdin { + use wasi:io/streams@0.2.0.{input-stream}; + get-stdin: func() -> input-stream; +} - /// Read from one stream and write to another, with blocking. - /// - /// This is similar to `splice`, except that it blocks until the - /// `output-stream` is ready for writing, and the `input-stream` - /// is ready for reading, before performing the `splice`. - blocking-splice: func(src: borrow, len: u64) -> result; +interface stdout { + use wasi:io/streams@0.2.0.{output-stream}; + get-stdout: func() -> output-stream; +} - /// Perform a write of up to 4096 bytes, and then flush the stream. Block - /// until all of these operations are complete, or an error occurs. - /// - /// This is a convenience wrapper around the use of `check-write`, - /// `subscribe`, `write`, and `flush`, and is implemented with the - /// following pseudo-code: - /// - /// ```text - /// let pollable = this.subscribe(); - /// while !contents.is_empty() { - /// // Wait for the stream to become writable - /// pollable.block(); - /// let Ok(n) = this.check-write(); // eliding error handling - /// let len = min(n, contents.len()); - /// let (chunk, rest) = contents.split_at(len); - /// this.write(chunk ); // eliding error handling - /// contents = rest; - /// } - /// this.flush(); - /// // Wait for completion of `flush` - /// pollable.block(); - /// // Check for any errors that arose during `flush` - /// let _ = this.check-write(); // eliding error handling - /// ``` - blocking-write-and-flush: func(contents: list) -> result<_, stream-error>; +interface stderr { + use wasi:io/streams@0.2.0.{output-stream}; + get-stderr: func() -> output-stream; +} - /// Perform a write of up to 4096 zeroes, and then flush the stream. - /// Block until all of these operations are complete, or an error - /// occurs. - /// - /// This is a convenience wrapper around the use of `check-write`, - /// `subscribe`, `write-zeroes`, and `flush`, and is implemented with - /// the following pseudo-code: - /// - /// ```text - /// let pollable = this.subscribe(); - /// while num_zeroes != 0 { - /// // Wait for the stream to become writable - /// pollable.block(); - /// let Ok(n) = this.check-write(); // eliding error handling - /// let len = min(n, num_zeroes); - /// this.write-zeroes(len); // eliding error handling - /// num_zeroes -= len; - /// } - /// this.flush(); - /// // Wait for completion of `flush` - /// pollable.block(); - /// // Check for any errors that arose during `flush` - /// let _ = this.check-write(); // eliding error handling - /// ``` - blocking-write-zeroes-and-flush: func(len: u64) -> result<_, stream-error>; +/// Terminal input. +/// +/// In the future, this may include functions for disabling echoing, +/// disabling input buffering so that keyboard events are sent through +/// immediately, querying supported features, and so on. +interface terminal-input { + /// The input side of a terminal. + resource terminal-input; +} - /// Check readiness for writing. This function never blocks. - /// - /// Returns the number of bytes permitted for the next call to `write`, - /// or an error. Calling `write` with more bytes than this function has - /// permitted will trap. - /// - /// When this function returns 0 bytes, the `subscribe` pollable will - /// become ready when this function will report at least 1 byte, or an - /// error. - check-write: func() -> result; +/// Terminal output. +/// +/// In the future, this may include functions for querying the terminal +/// size, being notified of terminal size changes, querying supported +/// features, and so on. +interface terminal-output { + /// The output side of a terminal. + resource terminal-output; +} - /// Request to flush buffered output. This function never blocks. - /// - /// This tells the output-stream that the caller intends any buffered - /// output to be flushed. the output which is expected to be flushed - /// is all that has been passed to `write` prior to this call. - /// - /// Upon calling this function, the `output-stream` will not accept any - /// writes (`check-write` will return `ok(0)`) until the flush has - /// completed. The `subscribe` pollable will become ready when the - /// flush has completed and the stream can accept more writes. - flush: func() -> result<_, stream-error>; +/// An interface providing an optional `terminal-input` for stdin as a +/// link-time authority. +interface terminal-stdin { + use terminal-input.{terminal-input}; - /// Read from one stream and write to another. - /// - /// The behavior of splice is equivelant to: - /// 1. calling `check-write` on the `output-stream` - /// 2. calling `read` on the `input-stream` with the smaller of the - /// `check-write` permitted length and the `len` provided to `splice` - /// 3. calling `write` on the `output-stream` with that read data. - /// - /// Any error reported by the call to `check-write`, `read`, or - /// `write` ends the splice and reports that error. - /// - /// This function returns the number of bytes transferred; it may be less - /// than `len`. - splice: func(src: borrow, len: u64) -> result; + /// If stdin is connected to a terminal, return a `terminal-input` handle + /// allowing further interaction with it. + get-terminal-stdin: func() -> option; +} - /// Create a `pollable` which will resolve once the output-stream - /// is ready for more writing, or an error has occured. When this - /// pollable is ready, `check-write` will return `ok(n)` with n>0, or an - /// error. - /// - /// If the stream is closed, this pollable is always ready immediately. - /// - /// The created `pollable` is a child resource of the `output-stream`. - /// Implementations may trap if the `output-stream` is dropped before - /// all derived `pollable`s created with this function are dropped. - subscribe: func() -> pollable; +/// An interface providing an optional `terminal-output` for stdout as a +/// link-time authority. +interface terminal-stdout { + use terminal-output.{terminal-output}; - /// Perform a write. This function never blocks. - /// - /// When the destination of a `write` is binary data, the bytes from - /// `contents` are written verbatim. When the destination of a `write` is - /// known to the implementation to be text, the bytes of `contents` are - /// transcoded from UTF-8 into the encoding of the destination and then - /// written. - /// - /// Precondition: check-write gave permit of Ok(n) and contents has a - /// length of less than or equal to n. Otherwise, this function will trap. - /// - /// returns Err(closed) without writing if the stream has closed since - /// the last call to check-write provided a permit. - write: func(contents: list) -> result<_, stream-error>; + /// If stdout is connected to a terminal, return a `terminal-output` handle + /// allowing further interaction with it. + get-terminal-stdout: func() -> option; +} - /// Write zeroes to a stream. - /// - /// This should be used precisely like `write` with the exact same - /// preconditions (must use check-write first), but instead of - /// passing a list of bytes, you simply pass the number of zero-bytes - /// that should be written. - write-zeroes: func(len: u64) -> result<_, stream-error>; - } +/// An interface providing an optional `terminal-output` for stderr as a +/// link-time authority. +interface terminal-stderr { + use terminal-output.{terminal-output}; + + /// If stderr is connected to a terminal, return a `terminal-output` handle + /// allowing further interaction with it. + get-terminal-stderr: func() -> option; } world imports { - import error; - import poll; - import streams; + import environment; + import exit; + import wasi:io/error@0.2.0; + import wasi:io/poll@0.2.0; + import wasi:io/streams@0.2.0; + import stdin; + import stdout; + import stderr; + import terminal-input; + import terminal-output; + import terminal-stdin; + import terminal-stdout; + import terminal-stderr; + import wasi:clocks/monotonic-clock@0.2.0; + import wasi:clocks/wall-clock@0.2.0; + import wasi:filesystem/types@0.2.0; + import wasi:filesystem/preopens@0.2.0; + import wasi:sockets/network@0.2.0; + import wasi:sockets/instance-network@0.2.0; + import wasi:sockets/udp@0.2.0; + import wasi:sockets/udp-create-socket@0.2.0; + import wasi:sockets/tcp@0.2.0; + import wasi:sockets/tcp-create-socket@0.2.0; + import wasi:sockets/ip-name-lookup@0.2.0; + import wasi:random/random@0.2.0; + import wasi:random/insecure@0.2.0; + import wasi:random/insecure-seed@0.2.0; +} + +world command { + import environment; + import exit; + import wasi:io/error@0.2.0; + import wasi:io/poll@0.2.0; + import wasi:io/streams@0.2.0; + import stdin; + import stdout; + import stderr; + import terminal-input; + import terminal-output; + import terminal-stdin; + import terminal-stdout; + import terminal-stderr; + import wasi:clocks/monotonic-clock@0.2.0; + import wasi:clocks/wall-clock@0.2.0; + import wasi:filesystem/types@0.2.0; + import wasi:filesystem/preopens@0.2.0; + import wasi:sockets/network@0.2.0; + import wasi:sockets/instance-network@0.2.0; + import wasi:sockets/udp@0.2.0; + import wasi:sockets/udp-create-socket@0.2.0; + import wasi:sockets/tcp@0.2.0; + import wasi:sockets/tcp-create-socket@0.2.0; + import wasi:sockets/ip-name-lookup@0.2.0; + import wasi:random/random@0.2.0; + import wasi:random/insecure@0.2.0; + import wasi:random/insecure-seed@0.2.0; + export run; } @@ -676,297 +522,609 @@ interface types { no-reuse } - /// A 128-bit hash value, split into parts because wasm doesn't have a - /// 128-bit integer type. - record metadata-hash-value { - /// 64 bits of a 128-bit hash value. - lower: u64, - /// Another 64 bits of a 128-bit hash value. - upper: u64, - } + /// A 128-bit hash value, split into parts because wasm doesn't have a + /// 128-bit integer type. + record metadata-hash-value { + /// 64 bits of a 128-bit hash value. + lower: u64, + /// Another 64 bits of a 128-bit hash value. + upper: u64, + } + + /// A descriptor is a reference to a filesystem object, which may be a file, + /// directory, named pipe, special file, or other object on which filesystem + /// calls may be made. + resource descriptor { + + /// Provide file advisory information on a descriptor. + /// + /// This is similar to `posix_fadvise` in POSIX. + advise: func(offset: filesize, length: filesize, advice: advice) -> result<_, error-code>; + + /// Return a stream for appending to a file, if available. + /// + /// May fail with an error-code describing why the file cannot be appended. + /// + /// Note: This allows using `write-stream`, which is similar to `write` with + /// `O_APPEND` in in POSIX. + append-via-stream: func() -> result; + + /// Create a directory. + /// + /// Note: This is similar to `mkdirat` in POSIX. + create-directory-at: func(path: string) -> result<_, error-code>; + + /// Get flags associated with a descriptor. + /// + /// Note: This returns similar flags to `fcntl(fd, F_GETFL)` in POSIX. + /// + /// Note: This returns the value that was the `fs_flags` value returned + /// from `fdstat_get` in earlier versions of WASI. + get-flags: func() -> result; + + /// Get the dynamic type of a descriptor. + /// + /// Note: This returns the same value as the `type` field of the `fd-stat` + /// returned by `stat`, `stat-at` and similar. + /// + /// Note: This returns similar flags to the `st_mode & S_IFMT` value provided + /// by `fstat` in POSIX. + /// + /// Note: This returns the value that was the `fs_filetype` value returned + /// from `fdstat_get` in earlier versions of WASI. + get-type: func() -> result; + + /// Test whether two descriptors refer to the same filesystem object. + /// + /// In POSIX, this corresponds to testing whether the two descriptors have the + /// same device (`st_dev`) and inode (`st_ino` or `d_ino`) numbers. + /// wasi-filesystem does not expose device and inode numbers, so this function + /// may be used instead. + is-same-object: func(other: borrow) -> bool; + + /// Create a hard link. + /// + /// Note: This is similar to `linkat` in POSIX. + link-at: func(old-path-flags: path-flags, old-path: string, new-descriptor: borrow, new-path: string) -> result<_, error-code>; + + /// Return a hash of the metadata associated with a filesystem object referred + /// to by a descriptor. + /// + /// This returns a hash of the last-modification timestamp and file size, and + /// may also include the inode number, device number, birth timestamp, and + /// other metadata fields that may change when the file is modified or + /// replaced. It may also include a secret value chosen by the + /// implementation and not otherwise exposed. + /// + /// Implementations are encourated to provide the following properties: + /// + /// - If the file is not modified or replaced, the computed hash value should + /// usually not change. + /// - If the object is modified or replaced, the computed hash value should + /// usually change. + /// - The inputs to the hash should not be easily computable from the + /// computed hash. + /// + /// However, none of these is required. + metadata-hash: func() -> result; + + /// Return a hash of the metadata associated with a filesystem object referred + /// to by a directory descriptor and a relative path. + /// + /// This performs the same hash computation as `metadata-hash`. + metadata-hash-at: func(path-flags: path-flags, path: string) -> result; + + /// Open a file or directory. + /// + /// The returned descriptor is not guaranteed to be the lowest-numbered + /// descriptor not currently open/ it is randomized to prevent applications + /// from depending on making assumptions about indexes, since this is + /// error-prone in multi-threaded contexts. The returned descriptor is + /// guaranteed to be less than 2**31. + /// + /// If `flags` contains `descriptor-flags::mutate-directory`, and the base + /// descriptor doesn't have `descriptor-flags::mutate-directory` set, + /// `open-at` fails with `error-code::read-only`. + /// + /// If `flags` contains `write` or `mutate-directory`, or `open-flags` + /// contains `truncate` or `create`, and the base descriptor doesn't have + /// `descriptor-flags::mutate-directory` set, `open-at` fails with + /// `error-code::read-only`. + /// + /// Note: This is similar to `openat` in POSIX. + open-at: func(path-flags: path-flags, path: string, open-flags: open-flags, %flags: descriptor-flags) -> result; + + /// Read from a descriptor, without using and updating the descriptor's offset. + /// + /// This function returns a list of bytes containing the data that was + /// read, along with a bool which, when true, indicates that the end of the + /// file was reached. The returned list will contain up to `length` bytes; it + /// may return fewer than requested, if the end of the file is reached or + /// if the I/O operation is interrupted. + /// + /// In the future, this may change to return a `stream`. + /// + /// Note: This is similar to `pread` in POSIX. + read: func(length: filesize, offset: filesize) -> result, bool>, error-code>; + + /// Read directory entries from a directory. + /// + /// On filesystems where directories contain entries referring to themselves + /// and their parents, often named `.` and `..` respectively, these entries + /// are omitted. + /// + /// This always returns a new stream which starts at the beginning of the + /// directory. Multiple streams may be active on the same directory, and they + /// do not interfere with each other. + read-directory: func() -> result; + + /// Return a stream for reading from a file, if available. + /// + /// May fail with an error-code describing why the file cannot be read. + /// + /// Multiple read, write, and append streams may be active on the same open + /// file and they do not interfere with each other. + /// + /// Note: This allows using `read-stream`, which is similar to `read` in POSIX. + read-via-stream: func(offset: filesize) -> result; + + /// Read the contents of a symbolic link. + /// + /// If the contents contain an absolute or rooted path in the underlying + /// filesystem, this function fails with `error-code::not-permitted`. + /// + /// Note: This is similar to `readlinkat` in POSIX. + readlink-at: func(path: string) -> result; - /// A descriptor is a reference to a filesystem object, which may be a file, - /// directory, named pipe, special file, or other object on which filesystem - /// calls may be made. - resource descriptor { + /// Remove a directory. + /// + /// Return `error-code::not-empty` if the directory is not empty. + /// + /// Note: This is similar to `unlinkat(fd, path, AT_REMOVEDIR)` in POSIX. + remove-directory-at: func(path: string) -> result<_, error-code>; - /// Provide file advisory information on a descriptor. + /// Rename a filesystem object. /// - /// This is similar to `posix_fadvise` in POSIX. - advise: func(offset: filesize, length: filesize, advice: advice) -> result<_, error-code>; + /// Note: This is similar to `renameat` in POSIX. + rename-at: func(old-path: string, new-descriptor: borrow, new-path: string) -> result<_, error-code>; - /// Return a stream for appending to a file, if available. + /// Adjust the size of an open file. If this increases the file's size, the + /// extra bytes are filled with zeros. /// - /// May fail with an error-code describing why the file cannot be appended. + /// Note: This was called `fd_filestat_set_size` in earlier versions of WASI. + set-size: func(size: filesize) -> result<_, error-code>; + + /// Adjust the timestamps of an open file or directory. /// - /// Note: This allows using `write-stream`, which is similar to `write` with - /// `O_APPEND` in in POSIX. - append-via-stream: func() -> result; + /// Note: This is similar to `futimens` in POSIX. + /// + /// Note: This was called `fd_filestat_set_times` in earlier versions of WASI. + set-times: func(data-access-timestamp: new-timestamp, data-modification-timestamp: new-timestamp) -> result<_, error-code>; - /// Create a directory. + /// Adjust the timestamps of a file or directory. /// - /// Note: This is similar to `mkdirat` in POSIX. - create-directory-at: func(path: string) -> result<_, error-code>; + /// Note: This is similar to `utimensat` in POSIX. + /// + /// Note: This was called `path_filestat_set_times` in earlier versions of + /// WASI. + set-times-at: func(path-flags: path-flags, path: string, data-access-timestamp: new-timestamp, data-modification-timestamp: new-timestamp) -> result<_, error-code>; - /// Get flags associated with a descriptor. + /// Return the attributes of an open file or directory. /// - /// Note: This returns similar flags to `fcntl(fd, F_GETFL)` in POSIX. + /// Note: This is similar to `fstat` in POSIX, except that it does not return + /// device and inode information. For testing whether two descriptors refer to + /// the same underlying filesystem object, use `is-same-object`. To obtain + /// additional data that can be used do determine whether a file has been + /// modified, use `metadata-hash`. /// - /// Note: This returns the value that was the `fs_flags` value returned - /// from `fdstat_get` in earlier versions of WASI. - get-flags: func() -> result; + /// Note: This was called `fd_filestat_get` in earlier versions of WASI. + stat: func() -> result; - /// Get the dynamic type of a descriptor. + /// Return the attributes of a file or directory. /// - /// Note: This returns the same value as the `type` field of the `fd-stat` - /// returned by `stat`, `stat-at` and similar. + /// Note: This is similar to `fstatat` in POSIX, except that it does not + /// return device and inode information. See the `stat` description for a + /// discussion of alternatives. /// - /// Note: This returns similar flags to the `st_mode & S_IFMT` value provided - /// by `fstat` in POSIX. + /// Note: This was called `path_filestat_get` in earlier versions of WASI. + stat-at: func(path-flags: path-flags, path: string) -> result; + + /// Create a symbolic link (also known as a "symlink"). /// - /// Note: This returns the value that was the `fs_filetype` value returned - /// from `fdstat_get` in earlier versions of WASI. - get-type: func() -> result; + /// If `old-path` starts with `/`, the function fails with + /// `error-code::not-permitted`. + /// + /// Note: This is similar to `symlinkat` in POSIX. + symlink-at: func(old-path: string, new-path: string) -> result<_, error-code>; - /// Test whether two descriptors refer to the same filesystem object. + /// Synchronize the data and metadata of a file to disk. /// - /// In POSIX, this corresponds to testing whether the two descriptors have the - /// same device (`st_dev`) and inode (`st_ino` or `d_ino`) numbers. - /// wasi-filesystem does not expose device and inode numbers, so this function - /// may be used instead. - is-same-object: func(other: borrow) -> bool; + /// This function succeeds with no effect if the file descriptor is not + /// opened for writing. + /// + /// Note: This is similar to `fsync` in POSIX. + sync: func() -> result<_, error-code>; - /// Create a hard link. + /// Synchronize the data of a file to disk. /// - /// Note: This is similar to `linkat` in POSIX. - link-at: func(old-path-flags: path-flags, old-path: string, new-descriptor: borrow, new-path: string) -> result<_, error-code>; + /// This function succeeds with no effect if the file descriptor is not + /// opened for writing. + /// + /// Note: This is similar to `fdatasync` in POSIX. + sync-data: func() -> result<_, error-code>; - /// Return a hash of the metadata associated with a filesystem object referred - /// to by a descriptor. + /// Unlink a filesystem object that is not a directory. /// - /// This returns a hash of the last-modification timestamp and file size, and - /// may also include the inode number, device number, birth timestamp, and - /// other metadata fields that may change when the file is modified or - /// replaced. It may also include a secret value chosen by the - /// implementation and not otherwise exposed. + /// Return `error-code::is-directory` if the path refers to a directory. + /// Note: This is similar to `unlinkat(fd, path, 0)` in POSIX. + unlink-file-at: func(path: string) -> result<_, error-code>; + + /// Write to a descriptor, without using and updating the descriptor's offset. /// - /// Implementations are encourated to provide the following properties: + /// It is valid to write past the end of a file; the file is extended to the + /// extent of the write, with bytes between the previous end and the start of + /// the write set to zero. /// - /// - If the file is not modified or replaced, the computed hash value should - /// usually not change. - /// - If the object is modified or replaced, the computed hash value should - /// usually change. - /// - The inputs to the hash should not be easily computable from the - /// computed hash. + /// In the future, this may change to take a `stream`. /// - /// However, none of these is required. - metadata-hash: func() -> result; + /// Note: This is similar to `pwrite` in POSIX. + write: func(buffer: list, offset: filesize) -> result; + + /// Return a stream for writing to a file, if available. + /// + /// May fail with an error-code describing why the file cannot be written. + /// + /// Note: This allows using `write-stream`, which is similar to `write` in + /// POSIX. + write-via-stream: func(offset: filesize) -> result; + } + + /// A stream of directory entries. + resource directory-entry-stream { + + /// Read a single directory entry from a `directory-entry-stream`. + read-directory-entry: func() -> result, error-code>; + } + + /// Attempts to extract a filesystem-related `error-code` from the stream + /// `error` provided. + /// + /// Stream operations which return `stream-error::last-operation-failed` + /// have a payload with more information about the operation that failed. + /// This payload can be passed through to this function to see if there's + /// filesystem-related information about the error to return. + /// + /// Note that this function is fallible because not all stream-related + /// errors are filesystem-related errors. + filesystem-error-code: func(err: borrow) -> option; +} + +interface preopens { + use types.{descriptor}; + + /// Return the set of preopened directories, and their path. + get-directories: func() -> list>; +} + +world imports { + import wasi:io/error@0.2.0; + import wasi:io/poll@0.2.0; + import wasi:io/streams@0.2.0; + import wasi:clocks/wall-clock@0.2.0; + import types; + import preopens; +} + + +package wasi:io@0.2.0; + +interface error { + /// A resource which represents some error information. + /// + /// The only method provided by this resource is `to-debug-string`, + /// which provides some human-readable information about the error. + /// + /// In the `wasi:io` package, this resource is returned through the + /// `wasi:io/streams/stream-error` type. + /// + /// To provide more specific error information, other interfaces may + /// provide functions to further "downcast" this error into more specific + /// error information. For example, `error`s returned in streams derived + /// from filesystem types to be described using the filesystem's own + /// error-code type, using the function + /// `wasi:filesystem/types/filesystem-error-code`, which takes a parameter + /// `borrow` and returns + /// `option`. + /// + /// The set of functions which can "downcast" an `error` into a more + /// concrete type is open. + resource error { + + /// Returns a string that is suitable to assist humans in debugging + /// this error. + /// + /// WARNING: The returned string should not be consumed mechanically! + /// It may change across platforms, hosts, or other implementation + /// details. Parsing this string is a major platform-compatibility + /// hazard. + to-debug-string: func() -> string; + } +} + +/// A poll API intended to let users wait for I/O events on multiple handles +/// at once. +interface poll { + /// `pollable` represents a single I/O event which may be ready, or not. + resource pollable { + + /// `block` returns immediately if the pollable is ready, and otherwise + /// blocks until ready. + /// + /// This function is equivalent to calling `poll.poll` on a list + /// containing only this pollable. + block: func(); + + /// Return the readiness of a pollable. This function never blocks. + /// + /// Returns `true` when the pollable is ready, and `false` otherwise. + ready: func() -> bool; + } + + /// Poll for completion on a set of pollables. + /// + /// This function takes a list of pollables, which identify I/O sources of + /// interest, and waits until one or more of the events is ready for I/O. + /// + /// The result `list` contains one or more indices of handles in the + /// argument list that is ready for I/O. + /// + /// If the list contains more elements than can be indexed with a `u32` + /// value, this function traps. + /// + /// A timeout can be implemented by adding a pollable from the + /// wasi-clocks API to the list. + /// + /// This function does not return a `result`; polling in itself does not + /// do any I/O so it doesn't fail. If any of the I/O sources identified by + /// the pollables has an error, it is indicated by marking the source as + /// being reaedy for I/O. + poll: func(in: list>) -> list; +} - /// Return a hash of the metadata associated with a filesystem object referred - /// to by a directory descriptor and a relative path. - /// - /// This performs the same hash computation as `metadata-hash`. - metadata-hash-at: func(path-flags: path-flags, path: string) -> result; +/// WASI I/O is an I/O abstraction API which is currently focused on providing +/// stream types. +/// +/// In the future, the component model is expected to add built-in stream types; +/// when it does, they are expected to subsume this API. +interface streams { + use error.{error}; + use poll.{pollable}; - /// Open a file or directory. - /// - /// The returned descriptor is not guaranteed to be the lowest-numbered - /// descriptor not currently open/ it is randomized to prevent applications - /// from depending on making assumptions about indexes, since this is - /// error-prone in multi-threaded contexts. The returned descriptor is - /// guaranteed to be less than 2**31. - /// - /// If `flags` contains `descriptor-flags::mutate-directory`, and the base - /// descriptor doesn't have `descriptor-flags::mutate-directory` set, - /// `open-at` fails with `error-code::read-only`. - /// - /// If `flags` contains `write` or `mutate-directory`, or `open-flags` - /// contains `truncate` or `create`, and the base descriptor doesn't have - /// `descriptor-flags::mutate-directory` set, `open-at` fails with - /// `error-code::read-only`. + /// An error for input-stream and output-stream operations. + variant stream-error { + /// The last operation (a write or flush) failed before completion. /// - /// Note: This is similar to `openat` in POSIX. - open-at: func(path-flags: path-flags, path: string, open-flags: open-flags, %flags: descriptor-flags) -> result; + /// More information is available in the `error` payload. + last-operation-failed(error), + /// The stream is closed: no more input will be accepted by the + /// stream. A closed output-stream will return this error on all + /// future operations. + closed, + } - /// Read from a descriptor, without using and updating the descriptor's offset. - /// - /// This function returns a list of bytes containing the data that was - /// read, along with a bool which, when true, indicates that the end of the - /// file was reached. The returned list will contain up to `length` bytes; it - /// may return fewer than requested, if the end of the file is reached or - /// if the I/O operation is interrupted. - /// - /// In the future, this may change to return a `stream`. - /// - /// Note: This is similar to `pread` in POSIX. - read: func(length: filesize, offset: filesize) -> result, bool>, error-code>; + /// An input bytestream. + /// + /// `input-stream`s are *non-blocking* to the extent practical on underlying + /// platforms. I/O operations always return promptly; if fewer bytes are + /// promptly available than requested, they return the number of bytes promptly + /// available, which could even be zero. To wait for data to be available, + /// use the `subscribe` function to obtain a `pollable` which can be polled + /// for using `wasi:io/poll`. + resource input-stream { - /// Read directory entries from a directory. - /// - /// On filesystems where directories contain entries referring to themselves - /// and their parents, often named `.` and `..` respectively, these entries - /// are omitted. - /// - /// This always returns a new stream which starts at the beginning of the - /// directory. Multiple streams may be active on the same directory, and they - /// do not interfere with each other. - read-directory: func() -> result; + /// Read bytes from a stream, after blocking until at least one byte can + /// be read. Except for blocking, behavior is identical to `read`. + blocking-read: func(len: u64) -> result, stream-error>; - /// Return a stream for reading from a file, if available. + /// Skip bytes from a stream, after blocking until at least one byte + /// can be skipped. Except for blocking behavior, identical to `skip`. + blocking-skip: func(len: u64) -> result; + + /// Perform a non-blocking read from the stream. /// - /// May fail with an error-code describing why the file cannot be read. + /// When the source of a `read` is binary data, the bytes from the source + /// are returned verbatim. When the source of a `read` is known to the + /// implementation to be text, bytes containing the UTF-8 encoding of the + /// text are returned. /// - /// Multiple read, write, and append streams may be active on the same open - /// file and they do not interfere with each other. + /// This function returns a list of bytes containing the read data, + /// when successful. The returned list will contain up to `len` bytes; + /// it may return fewer than requested, but not more. The list is + /// empty when no bytes are available for reading at this time. The + /// pollable given by `subscribe` will be ready when more bytes are + /// available. /// - /// Note: This allows using `read-stream`, which is similar to `read` in POSIX. - read-via-stream: func(offset: filesize) -> result; - - /// Read the contents of a symbolic link. + /// This function fails with a `stream-error` when the operation + /// encounters an error, giving `last-operation-failed`, or when the + /// stream is closed, giving `closed`. /// - /// If the contents contain an absolute or rooted path in the underlying - /// filesystem, this function fails with `error-code::not-permitted`. + /// When the caller gives a `len` of 0, it represents a request to + /// read 0 bytes. If the stream is still open, this call should + /// succeed and return an empty list, or otherwise fail with `closed`. /// - /// Note: This is similar to `readlinkat` in POSIX. - readlink-at: func(path: string) -> result; + /// The `len` parameter is a `u64`, which could represent a list of u8 which + /// is not possible to allocate in wasm32, or not desirable to allocate as + /// as a return value by the callee. The callee may return a list of bytes + /// less than `len` in size while more bytes are available for reading. + read: func(len: u64) -> result, stream-error>; - /// Remove a directory. - /// - /// Return `error-code::not-empty` if the directory is not empty. + /// Skip bytes from a stream. Returns number of bytes skipped. /// - /// Note: This is similar to `unlinkat(fd, path, AT_REMOVEDIR)` in POSIX. - remove-directory-at: func(path: string) -> result<_, error-code>; + /// Behaves identical to `read`, except instead of returning a list + /// of bytes, returns the number of bytes consumed from the stream. + skip: func(len: u64) -> result; - /// Rename a filesystem object. - /// - /// Note: This is similar to `renameat` in POSIX. - rename-at: func(old-path: string, new-descriptor: borrow, new-path: string) -> result<_, error-code>; + /// Create a `pollable` which will resolve once either the specified stream + /// has bytes available to read or the other end of the stream has been + /// closed. + /// The created `pollable` is a child resource of the `input-stream`. + /// Implementations may trap if the `input-stream` is dropped before + /// all derived `pollable`s created with this function are dropped. + subscribe: func() -> pollable; + } - /// Adjust the size of an open file. If this increases the file's size, the - /// extra bytes are filled with zeros. - /// - /// Note: This was called `fd_filestat_set_size` in earlier versions of WASI. - set-size: func(size: filesize) -> result<_, error-code>; + /// An output bytestream. + /// + /// `output-stream`s are *non-blocking* to the extent practical on + /// underlying platforms. Except where specified otherwise, I/O operations also + /// always return promptly, after the number of bytes that can be written + /// promptly, which could even be zero. To wait for the stream to be ready to + /// accept data, the `subscribe` function to obtain a `pollable` which can be + /// polled for using `wasi:io/poll`. + resource output-stream { - /// Adjust the timestamps of an open file or directory. - /// - /// Note: This is similar to `futimens` in POSIX. - /// - /// Note: This was called `fd_filestat_set_times` in earlier versions of WASI. - set-times: func(data-access-timestamp: new-timestamp, data-modification-timestamp: new-timestamp) -> result<_, error-code>; + /// Request to flush buffered output, and block until flush completes + /// and stream is ready for writing again. + blocking-flush: func() -> result<_, stream-error>; - /// Adjust the timestamps of a file or directory. - /// - /// Note: This is similar to `utimensat` in POSIX. + /// Read from one stream and write to another, with blocking. /// - /// Note: This was called `path_filestat_set_times` in earlier versions of - /// WASI. - set-times-at: func(path-flags: path-flags, path: string, data-access-timestamp: new-timestamp, data-modification-timestamp: new-timestamp) -> result<_, error-code>; + /// This is similar to `splice`, except that it blocks until the + /// `output-stream` is ready for writing, and the `input-stream` + /// is ready for reading, before performing the `splice`. + blocking-splice: func(src: borrow, len: u64) -> result; - /// Return the attributes of an open file or directory. + /// Perform a write of up to 4096 bytes, and then flush the stream. Block + /// until all of these operations are complete, or an error occurs. /// - /// Note: This is similar to `fstat` in POSIX, except that it does not return - /// device and inode information. For testing whether two descriptors refer to - /// the same underlying filesystem object, use `is-same-object`. To obtain - /// additional data that can be used do determine whether a file has been - /// modified, use `metadata-hash`. + /// This is a convenience wrapper around the use of `check-write`, + /// `subscribe`, `write`, and `flush`, and is implemented with the + /// following pseudo-code: /// - /// Note: This was called `fd_filestat_get` in earlier versions of WASI. - stat: func() -> result; + /// ```text + /// let pollable = this.subscribe(); + /// while !contents.is_empty() { + /// // Wait for the stream to become writable + /// pollable.block(); + /// let Ok(n) = this.check-write(); // eliding error handling + /// let len = min(n, contents.len()); + /// let (chunk, rest) = contents.split_at(len); + /// this.write(chunk ); // eliding error handling + /// contents = rest; + /// } + /// this.flush(); + /// // Wait for completion of `flush` + /// pollable.block(); + /// // Check for any errors that arose during `flush` + /// let _ = this.check-write(); // eliding error handling + /// ``` + blocking-write-and-flush: func(contents: list) -> result<_, stream-error>; - /// Return the attributes of a file or directory. + /// Perform a write of up to 4096 zeroes, and then flush the stream. + /// Block until all of these operations are complete, or an error + /// occurs. /// - /// Note: This is similar to `fstatat` in POSIX, except that it does not - /// return device and inode information. See the `stat` description for a - /// discussion of alternatives. + /// This is a convenience wrapper around the use of `check-write`, + /// `subscribe`, `write-zeroes`, and `flush`, and is implemented with + /// the following pseudo-code: /// - /// Note: This was called `path_filestat_get` in earlier versions of WASI. - stat-at: func(path-flags: path-flags, path: string) -> result; + /// ```text + /// let pollable = this.subscribe(); + /// while num_zeroes != 0 { + /// // Wait for the stream to become writable + /// pollable.block(); + /// let Ok(n) = this.check-write(); // eliding error handling + /// let len = min(n, num_zeroes); + /// this.write-zeroes(len); // eliding error handling + /// num_zeroes -= len; + /// } + /// this.flush(); + /// // Wait for completion of `flush` + /// pollable.block(); + /// // Check for any errors that arose during `flush` + /// let _ = this.check-write(); // eliding error handling + /// ``` + blocking-write-zeroes-and-flush: func(len: u64) -> result<_, stream-error>; - /// Create a symbolic link (also known as a "symlink"). + /// Check readiness for writing. This function never blocks. /// - /// If `old-path` starts with `/`, the function fails with - /// `error-code::not-permitted`. + /// Returns the number of bytes permitted for the next call to `write`, + /// or an error. Calling `write` with more bytes than this function has + /// permitted will trap. /// - /// Note: This is similar to `symlinkat` in POSIX. - symlink-at: func(old-path: string, new-path: string) -> result<_, error-code>; + /// When this function returns 0 bytes, the `subscribe` pollable will + /// become ready when this function will report at least 1 byte, or an + /// error. + check-write: func() -> result; - /// Synchronize the data and metadata of a file to disk. + /// Request to flush buffered output. This function never blocks. /// - /// This function succeeds with no effect if the file descriptor is not - /// opened for writing. + /// This tells the output-stream that the caller intends any buffered + /// output to be flushed. the output which is expected to be flushed + /// is all that has been passed to `write` prior to this call. /// - /// Note: This is similar to `fsync` in POSIX. - sync: func() -> result<_, error-code>; + /// Upon calling this function, the `output-stream` will not accept any + /// writes (`check-write` will return `ok(0)`) until the flush has + /// completed. The `subscribe` pollable will become ready when the + /// flush has completed and the stream can accept more writes. + flush: func() -> result<_, stream-error>; - /// Synchronize the data of a file to disk. + /// Read from one stream and write to another. /// - /// This function succeeds with no effect if the file descriptor is not - /// opened for writing. + /// The behavior of splice is equivelant to: + /// 1. calling `check-write` on the `output-stream` + /// 2. calling `read` on the `input-stream` with the smaller of the + /// `check-write` permitted length and the `len` provided to `splice` + /// 3. calling `write` on the `output-stream` with that read data. /// - /// Note: This is similar to `fdatasync` in POSIX. - sync-data: func() -> result<_, error-code>; - - /// Unlink a filesystem object that is not a directory. + /// Any error reported by the call to `check-write`, `read`, or + /// `write` ends the splice and reports that error. /// - /// Return `error-code::is-directory` if the path refers to a directory. - /// Note: This is similar to `unlinkat(fd, path, 0)` in POSIX. - unlink-file-at: func(path: string) -> result<_, error-code>; + /// This function returns the number of bytes transferred; it may be less + /// than `len`. + splice: func(src: borrow, len: u64) -> result; - /// Write to a descriptor, without using and updating the descriptor's offset. - /// - /// It is valid to write past the end of a file; the file is extended to the - /// extent of the write, with bytes between the previous end and the start of - /// the write set to zero. + /// Create a `pollable` which will resolve once the output-stream + /// is ready for more writing, or an error has occured. When this + /// pollable is ready, `check-write` will return `ok(n)` with n>0, or an + /// error. /// - /// In the future, this may change to take a `stream`. + /// If the stream is closed, this pollable is always ready immediately. /// - /// Note: This is similar to `pwrite` in POSIX. - write: func(buffer: list, offset: filesize) -> result; + /// The created `pollable` is a child resource of the `output-stream`. + /// Implementations may trap if the `output-stream` is dropped before + /// all derived `pollable`s created with this function are dropped. + subscribe: func() -> pollable; - /// Return a stream for writing to a file, if available. + /// Perform a write. This function never blocks. /// - /// May fail with an error-code describing why the file cannot be written. + /// When the destination of a `write` is binary data, the bytes from + /// `contents` are written verbatim. When the destination of a `write` is + /// known to the implementation to be text, the bytes of `contents` are + /// transcoded from UTF-8 into the encoding of the destination and then + /// written. /// - /// Note: This allows using `write-stream`, which is similar to `write` in - /// POSIX. - write-via-stream: func(offset: filesize) -> result; - } - - /// A stream of directory entries. - resource directory-entry-stream { + /// Precondition: check-write gave permit of Ok(n) and contents has a + /// length of less than or equal to n. Otherwise, this function will trap. + /// + /// returns Err(closed) without writing if the stream has closed since + /// the last call to check-write provided a permit. + write: func(contents: list) -> result<_, stream-error>; - /// Read a single directory entry from a `directory-entry-stream`. - read-directory-entry: func() -> result, error-code>; + /// Write zeroes to a stream. + /// + /// This should be used precisely like `write` with the exact same + /// preconditions (must use check-write first), but instead of + /// passing a list of bytes, you simply pass the number of zero-bytes + /// that should be written. + write-zeroes: func(len: u64) -> result<_, stream-error>; } - - /// Attempts to extract a filesystem-related `error-code` from the stream - /// `error` provided. - /// - /// Stream operations which return `stream-error::last-operation-failed` - /// have a payload with more information about the operation that failed. - /// This payload can be passed through to this function to see if there's - /// filesystem-related information about the error to return. - /// - /// Note that this function is fallible because not all stream-related - /// errors are filesystem-related errors. - filesystem-error-code: func(err: borrow) -> option; -} - -interface preopens { - use types.{descriptor}; - - /// Return the set of preopened directories, and their path. - get-directories: func() -> list>; } world imports { - import wasi:io/error@0.2.0; - import wasi:io/poll@0.2.0; - import wasi:io/streams@0.2.0; - import wasi:clocks/wall-clock@0.2.0; - import types; - import preopens; + import error; + import poll; + import streams; } @@ -2015,161 +2173,3 @@ world imports { import tcp-create-socket; import ip-name-lookup; } - - -package wasi:cli@0.2.0; - -interface environment { - /// Get the POSIX-style environment variables. - /// - /// Each environment variable is provided as a pair of string variable names - /// and string value. - /// - /// Morally, these are a value import, but until value imports are available - /// in the component model, this import function should return the same - /// values each time it is called. - get-environment: func() -> list>; - - /// Get the POSIX-style arguments to the program. - get-arguments: func() -> list; - - /// Return a path that programs should use as their initial current working - /// directory, interpreting `.` as shorthand for this. - initial-cwd: func() -> option; -} - -interface exit { - /// Exit the current instance and any linked instances. - exit: func(status: result); -} - -interface run { - /// Run the program. - run: func() -> result; -} - -interface stdin { - use wasi:io/streams@0.2.0.{input-stream}; - get-stdin: func() -> input-stream; -} - -interface stdout { - use wasi:io/streams@0.2.0.{output-stream}; - get-stdout: func() -> output-stream; -} - -interface stderr { - use wasi:io/streams@0.2.0.{output-stream}; - get-stderr: func() -> output-stream; -} - -/// Terminal input. -/// -/// In the future, this may include functions for disabling echoing, -/// disabling input buffering so that keyboard events are sent through -/// immediately, querying supported features, and so on. -interface terminal-input { - /// The input side of a terminal. - resource terminal-input; -} - -/// Terminal output. -/// -/// In the future, this may include functions for querying the terminal -/// size, being notified of terminal size changes, querying supported -/// features, and so on. -interface terminal-output { - /// The output side of a terminal. - resource terminal-output; -} - -/// An interface providing an optional `terminal-input` for stdin as a -/// link-time authority. -interface terminal-stdin { - use terminal-input.{terminal-input}; - - /// If stdin is connected to a terminal, return a `terminal-input` handle - /// allowing further interaction with it. - get-terminal-stdin: func() -> option; -} - -/// An interface providing an optional `terminal-output` for stdout as a -/// link-time authority. -interface terminal-stdout { - use terminal-output.{terminal-output}; - - /// If stdout is connected to a terminal, return a `terminal-output` handle - /// allowing further interaction with it. - get-terminal-stdout: func() -> option; -} - -/// An interface providing an optional `terminal-output` for stderr as a -/// link-time authority. -interface terminal-stderr { - use terminal-output.{terminal-output}; - - /// If stderr is connected to a terminal, return a `terminal-output` handle - /// allowing further interaction with it. - get-terminal-stderr: func() -> option; -} - -world imports { - import environment; - import exit; - import wasi:io/error@0.2.0; - import wasi:io/poll@0.2.0; - import wasi:io/streams@0.2.0; - import stdin; - import stdout; - import stderr; - import terminal-input; - import terminal-output; - import terminal-stdin; - import terminal-stdout; - import terminal-stderr; - import wasi:clocks/monotonic-clock@0.2.0; - import wasi:clocks/wall-clock@0.2.0; - import wasi:filesystem/types@0.2.0; - import wasi:filesystem/preopens@0.2.0; - import wasi:sockets/network@0.2.0; - import wasi:sockets/instance-network@0.2.0; - import wasi:sockets/udp@0.2.0; - import wasi:sockets/udp-create-socket@0.2.0; - import wasi:sockets/tcp@0.2.0; - import wasi:sockets/tcp-create-socket@0.2.0; - import wasi:sockets/ip-name-lookup@0.2.0; - import wasi:random/random@0.2.0; - import wasi:random/insecure@0.2.0; - import wasi:random/insecure-seed@0.2.0; -} - -world command { - import environment; - import exit; - import wasi:io/error@0.2.0; - import wasi:io/poll@0.2.0; - import wasi:io/streams@0.2.0; - import stdin; - import stdout; - import stderr; - import terminal-input; - import terminal-output; - import terminal-stdin; - import terminal-stdout; - import terminal-stderr; - import wasi:clocks/monotonic-clock@0.2.0; - import wasi:clocks/wall-clock@0.2.0; - import wasi:filesystem/types@0.2.0; - import wasi:filesystem/preopens@0.2.0; - import wasi:sockets/network@0.2.0; - import wasi:sockets/instance-network@0.2.0; - import wasi:sockets/udp@0.2.0; - import wasi:sockets/udp-create-socket@0.2.0; - import wasi:sockets/tcp@0.2.0; - import wasi:sockets/tcp-create-socket@0.2.0; - import wasi:sockets/ip-name-lookup@0.2.0; - import wasi:random/random@0.2.0; - import wasi:random/insecure@0.2.0; - import wasi:random/insecure-seed@0.2.0; - export run; -} diff --git a/testdata/wasi/http.wit.json.golden.wit b/testdata/wasi/http.wit.json.golden.wit index 2ec3c925..5fe5ebec 100644 --- a/testdata/wasi/http.wit.json.golden.wit +++ b/testdata/wasi/http.wit.json.golden.wit @@ -1,312 +1,158 @@ -package wasi:io@0.2.0; +package wasi:cli@0.2.0; -interface error { - /// A resource which represents some error information. - /// - /// The only method provided by this resource is `to-debug-string`, - /// which provides some human-readable information about the error. - /// - /// In the `wasi:io` package, this resource is returned through the - /// `wasi:io/streams/stream-error` type. +interface environment { + /// Get the POSIX-style environment variables. /// - /// To provide more specific error information, other interfaces may - /// provide functions to further "downcast" this error into more specific - /// error information. For example, `error`s returned in streams derived - /// from filesystem types to be described using the filesystem's own - /// error-code type, using the function - /// `wasi:filesystem/types/filesystem-error-code`, which takes a parameter - /// `borrow` and returns - /// `option`. + /// Each environment variable is provided as a pair of string variable names + /// and string value. /// - /// The set of functions which can "downcast" an `error` into a more - /// concrete type is open. - resource error { + /// Morally, these are a value import, but until value imports are available + /// in the component model, this import function should return the same + /// values each time it is called. + get-environment: func() -> list>; - /// Returns a string that is suitable to assist humans in debugging - /// this error. - /// - /// WARNING: The returned string should not be consumed mechanically! - /// It may change across platforms, hosts, or other implementation - /// details. Parsing this string is a major platform-compatibility - /// hazard. - to-debug-string: func() -> string; - } + /// Get the POSIX-style arguments to the program. + get-arguments: func() -> list; + + /// Return a path that programs should use as their initial current working + /// directory, interpreting `.` as shorthand for this. + initial-cwd: func() -> option; } -/// A poll API intended to let users wait for I/O events on multiple handles -/// at once. -interface poll { - /// `pollable` represents a single I/O event which may be ready, or not. - resource pollable { +interface exit { + /// Exit the current instance and any linked instances. + exit: func(status: result); +} - /// `block` returns immediately if the pollable is ready, and otherwise - /// blocks until ready. - /// - /// This function is equivalent to calling `poll.poll` on a list - /// containing only this pollable. - block: func(); +interface run { + /// Run the program. + run: func() -> result; +} - /// Return the readiness of a pollable. This function never blocks. - /// - /// Returns `true` when the pollable is ready, and `false` otherwise. - ready: func() -> bool; - } +interface stdin { + use wasi:io/streams@0.2.0.{input-stream}; + get-stdin: func() -> input-stream; +} - /// Poll for completion on a set of pollables. - /// - /// This function takes a list of pollables, which identify I/O sources of - /// interest, and waits until one or more of the events is ready for I/O. - /// - /// The result `list` contains one or more indices of handles in the - /// argument list that is ready for I/O. - /// - /// If the list contains more elements than can be indexed with a `u32` - /// value, this function traps. - /// - /// A timeout can be implemented by adding a pollable from the - /// wasi-clocks API to the list. - /// - /// This function does not return a `result`; polling in itself does not - /// do any I/O so it doesn't fail. If any of the I/O sources identified by - /// the pollables has an error, it is indicated by marking the source as - /// being reaedy for I/O. - poll: func(in: list>) -> list; +interface stdout { + use wasi:io/streams@0.2.0.{output-stream}; + get-stdout: func() -> output-stream; } -/// WASI I/O is an I/O abstraction API which is currently focused on providing -/// stream types. -/// -/// In the future, the component model is expected to add built-in stream types; -/// when it does, they are expected to subsume this API. -interface streams { - use error.{error}; - use poll.{pollable}; +interface stderr { + use wasi:io/streams@0.2.0.{output-stream}; + get-stderr: func() -> output-stream; +} - /// An error for input-stream and output-stream operations. - variant stream-error { - /// The last operation (a write or flush) failed before completion. - /// - /// More information is available in the `error` payload. - last-operation-failed(error), - /// The stream is closed: no more input will be accepted by the - /// stream. A closed output-stream will return this error on all - /// future operations. - closed, - } +/// Terminal input. +/// +/// In the future, this may include functions for disabling echoing, +/// disabling input buffering so that keyboard events are sent through +/// immediately, querying supported features, and so on. +interface terminal-input { + /// The input side of a terminal. + resource terminal-input; +} - /// An input bytestream. - /// - /// `input-stream`s are *non-blocking* to the extent practical on underlying - /// platforms. I/O operations always return promptly; if fewer bytes are - /// promptly available than requested, they return the number of bytes promptly - /// available, which could even be zero. To wait for data to be available, - /// use the `subscribe` function to obtain a `pollable` which can be polled - /// for using `wasi:io/poll`. - resource input-stream { +/// Terminal output. +/// +/// In the future, this may include functions for querying the terminal +/// size, being notified of terminal size changes, querying supported +/// features, and so on. +interface terminal-output { + /// The output side of a terminal. + resource terminal-output; +} - /// Read bytes from a stream, after blocking until at least one byte can - /// be read. Except for blocking, behavior is identical to `read`. - blocking-read: func(len: u64) -> result, stream-error>; +/// An interface providing an optional `terminal-input` for stdin as a +/// link-time authority. +interface terminal-stdin { + use terminal-input.{terminal-input}; - /// Skip bytes from a stream, after blocking until at least one byte - /// can be skipped. Except for blocking behavior, identical to `skip`. - blocking-skip: func(len: u64) -> result; + /// If stdin is connected to a terminal, return a `terminal-input` handle + /// allowing further interaction with it. + get-terminal-stdin: func() -> option; +} - /// Perform a non-blocking read from the stream. - /// - /// When the source of a `read` is binary data, the bytes from the source - /// are returned verbatim. When the source of a `read` is known to the - /// implementation to be text, bytes containing the UTF-8 encoding of the - /// text are returned. - /// - /// This function returns a list of bytes containing the read data, - /// when successful. The returned list will contain up to `len` bytes; - /// it may return fewer than requested, but not more. The list is - /// empty when no bytes are available for reading at this time. The - /// pollable given by `subscribe` will be ready when more bytes are - /// available. - /// - /// This function fails with a `stream-error` when the operation - /// encounters an error, giving `last-operation-failed`, or when the - /// stream is closed, giving `closed`. - /// - /// When the caller gives a `len` of 0, it represents a request to - /// read 0 bytes. If the stream is still open, this call should - /// succeed and return an empty list, or otherwise fail with `closed`. - /// - /// The `len` parameter is a `u64`, which could represent a list of u8 which - /// is not possible to allocate in wasm32, or not desirable to allocate as - /// as a return value by the callee. The callee may return a list of bytes - /// less than `len` in size while more bytes are available for reading. - read: func(len: u64) -> result, stream-error>; +/// An interface providing an optional `terminal-output` for stdout as a +/// link-time authority. +interface terminal-stdout { + use terminal-output.{terminal-output}; - /// Skip bytes from a stream. Returns number of bytes skipped. - /// - /// Behaves identical to `read`, except instead of returning a list - /// of bytes, returns the number of bytes consumed from the stream. - skip: func(len: u64) -> result; + /// If stdout is connected to a terminal, return a `terminal-output` handle + /// allowing further interaction with it. + get-terminal-stdout: func() -> option; +} - /// Create a `pollable` which will resolve once either the specified stream - /// has bytes available to read or the other end of the stream has been - /// closed. - /// The created `pollable` is a child resource of the `input-stream`. - /// Implementations may trap if the `input-stream` is dropped before - /// all derived `pollable`s created with this function are dropped. - subscribe: func() -> pollable; - } +/// An interface providing an optional `terminal-output` for stderr as a +/// link-time authority. +interface terminal-stderr { + use terminal-output.{terminal-output}; - /// An output bytestream. - /// - /// `output-stream`s are *non-blocking* to the extent practical on - /// underlying platforms. Except where specified otherwise, I/O operations also - /// always return promptly, after the number of bytes that can be written - /// promptly, which could even be zero. To wait for the stream to be ready to - /// accept data, the `subscribe` function to obtain a `pollable` which can be - /// polled for using `wasi:io/poll`. - resource output-stream { + /// If stderr is connected to a terminal, return a `terminal-output` handle + /// allowing further interaction with it. + get-terminal-stderr: func() -> option; +} - /// Request to flush buffered output, and block until flush completes - /// and stream is ready for writing again. - blocking-flush: func() -> result<_, stream-error>; +world imports { + import environment; + import exit; + import wasi:io/error@0.2.0; + import wasi:io/poll@0.2.0; + import wasi:io/streams@0.2.0; + import stdin; + import stdout; + import stderr; + import terminal-input; + import terminal-output; + import terminal-stdin; + import terminal-stdout; + import terminal-stderr; + import wasi:clocks/monotonic-clock@0.2.0; + import wasi:clocks/wall-clock@0.2.0; + import wasi:filesystem/types@0.2.0; + import wasi:filesystem/preopens@0.2.0; + import wasi:sockets/network@0.2.0; + import wasi:sockets/instance-network@0.2.0; + import wasi:sockets/udp@0.2.0; + import wasi:sockets/udp-create-socket@0.2.0; + import wasi:sockets/tcp@0.2.0; + import wasi:sockets/tcp-create-socket@0.2.0; + import wasi:sockets/ip-name-lookup@0.2.0; + import wasi:random/random@0.2.0; + import wasi:random/insecure@0.2.0; + import wasi:random/insecure-seed@0.2.0; +} - /// Read from one stream and write to another, with blocking. - /// - /// This is similar to `splice`, except that it blocks until the - /// `output-stream` is ready for writing, and the `input-stream` - /// is ready for reading, before performing the `splice`. - blocking-splice: func(src: borrow, len: u64) -> result; - - /// Perform a write of up to 4096 bytes, and then flush the stream. Block - /// until all of these operations are complete, or an error occurs. - /// - /// This is a convenience wrapper around the use of `check-write`, - /// `subscribe`, `write`, and `flush`, and is implemented with the - /// following pseudo-code: - /// - /// ```text - /// let pollable = this.subscribe(); - /// while !contents.is_empty() { - /// // Wait for the stream to become writable - /// pollable.block(); - /// let Ok(n) = this.check-write(); // eliding error handling - /// let len = min(n, contents.len()); - /// let (chunk, rest) = contents.split_at(len); - /// this.write(chunk ); // eliding error handling - /// contents = rest; - /// } - /// this.flush(); - /// // Wait for completion of `flush` - /// pollable.block(); - /// // Check for any errors that arose during `flush` - /// let _ = this.check-write(); // eliding error handling - /// ``` - blocking-write-and-flush: func(contents: list) -> result<_, stream-error>; - - /// Perform a write of up to 4096 zeroes, and then flush the stream. - /// Block until all of these operations are complete, or an error - /// occurs. - /// - /// This is a convenience wrapper around the use of `check-write`, - /// `subscribe`, `write-zeroes`, and `flush`, and is implemented with - /// the following pseudo-code: - /// - /// ```text - /// let pollable = this.subscribe(); - /// while num_zeroes != 0 { - /// // Wait for the stream to become writable - /// pollable.block(); - /// let Ok(n) = this.check-write(); // eliding error handling - /// let len = min(n, num_zeroes); - /// this.write-zeroes(len); // eliding error handling - /// num_zeroes -= len; - /// } - /// this.flush(); - /// // Wait for completion of `flush` - /// pollable.block(); - /// // Check for any errors that arose during `flush` - /// let _ = this.check-write(); // eliding error handling - /// ``` - blocking-write-zeroes-and-flush: func(len: u64) -> result<_, stream-error>; - - /// Check readiness for writing. This function never blocks. - /// - /// Returns the number of bytes permitted for the next call to `write`, - /// or an error. Calling `write` with more bytes than this function has - /// permitted will trap. - /// - /// When this function returns 0 bytes, the `subscribe` pollable will - /// become ready when this function will report at least 1 byte, or an - /// error. - check-write: func() -> result; - - /// Request to flush buffered output. This function never blocks. - /// - /// This tells the output-stream that the caller intends any buffered - /// output to be flushed. the output which is expected to be flushed - /// is all that has been passed to `write` prior to this call. - /// - /// Upon calling this function, the `output-stream` will not accept any - /// writes (`check-write` will return `ok(0)`) until the flush has - /// completed. The `subscribe` pollable will become ready when the - /// flush has completed and the stream can accept more writes. - flush: func() -> result<_, stream-error>; - - /// Read from one stream and write to another. - /// - /// The behavior of splice is equivelant to: - /// 1. calling `check-write` on the `output-stream` - /// 2. calling `read` on the `input-stream` with the smaller of the - /// `check-write` permitted length and the `len` provided to `splice` - /// 3. calling `write` on the `output-stream` with that read data. - /// - /// Any error reported by the call to `check-write`, `read`, or - /// `write` ends the splice and reports that error. - /// - /// This function returns the number of bytes transferred; it may be less - /// than `len`. - splice: func(src: borrow, len: u64) -> result; - - /// Create a `pollable` which will resolve once the output-stream - /// is ready for more writing, or an error has occured. When this - /// pollable is ready, `check-write` will return `ok(n)` with n>0, or an - /// error. - /// - /// If the stream is closed, this pollable is always ready immediately. - /// - /// The created `pollable` is a child resource of the `output-stream`. - /// Implementations may trap if the `output-stream` is dropped before - /// all derived `pollable`s created with this function are dropped. - subscribe: func() -> pollable; - - /// Perform a write. This function never blocks. - /// - /// When the destination of a `write` is binary data, the bytes from - /// `contents` are written verbatim. When the destination of a `write` is - /// known to the implementation to be text, the bytes of `contents` are - /// transcoded from UTF-8 into the encoding of the destination and then - /// written. - /// - /// Precondition: check-write gave permit of Ok(n) and contents has a - /// length of less than or equal to n. Otherwise, this function will trap. - /// - /// returns Err(closed) without writing if the stream has closed since - /// the last call to check-write provided a permit. - write: func(contents: list) -> result<_, stream-error>; - - /// Write zeroes to a stream. - /// - /// This should be used precisely like `write` with the exact same - /// preconditions (must use check-write first), but instead of - /// passing a list of bytes, you simply pass the number of zero-bytes - /// that should be written. - write-zeroes: func(len: u64) -> result<_, stream-error>; - } -} - -world imports { - import error; - import poll; - import streams; +world command { + import environment; + import exit; + import wasi:io/error@0.2.0; + import wasi:io/poll@0.2.0; + import wasi:io/streams@0.2.0; + import stdin; + import stdout; + import stderr; + import terminal-input; + import terminal-output; + import terminal-stdin; + import terminal-stdout; + import terminal-stderr; + import wasi:clocks/monotonic-clock@0.2.0; + import wasi:clocks/wall-clock@0.2.0; + import wasi:filesystem/types@0.2.0; + import wasi:filesystem/preopens@0.2.0; + import wasi:sockets/network@0.2.0; + import wasi:sockets/instance-network@0.2.0; + import wasi:sockets/udp@0.2.0; + import wasi:sockets/udp-create-socket@0.2.0; + import wasi:sockets/tcp@0.2.0; + import wasi:sockets/tcp-create-socket@0.2.0; + import wasi:sockets/ip-name-lookup@0.2.0; + import wasi:random/random@0.2.0; + import wasi:random/insecure@0.2.0; + import wasi:random/insecure-seed@0.2.0; + export run; } @@ -970,968 +816,957 @@ world imports { } -package wasi:sockets@0.2.0; +package wasi:http@0.2.0; -interface network { - /// An opaque resource that represents access to (a subset of) the network. - /// This enables context-based security for networking. - /// There is no need for this to map 1:1 to a physical network interface. - resource network; +/// This interface defines all of the types and methods for implementing +/// HTTP Requests and Responses, both incoming and outgoing, as well as +/// their headers, trailers, and bodies. +interface types { + use wasi:clocks/monotonic-clock@0.2.0.{duration}; + use wasi:io/streams@0.2.0.{input-stream}; + use wasi:io/streams@0.2.0.{output-stream}; + use wasi:io/error@0.2.0.{error as io-error}; + use wasi:io/poll@0.2.0.{pollable}; - /// Error codes. - /// - /// In theory, every API can return any error code. - /// In practice, API's typically only return the errors documented per API - /// combined with a couple of errors that are always possible: - /// - `unknown` - /// - `access-denied` - /// - `not-supported` - /// - `out-of-memory` - /// - `concurrency-conflict` - /// - /// See each individual API for what the POSIX equivalents are. They sometimes differ - /// per API. - enum error-code { - /// Unknown error - unknown, - /// Access denied. - /// - /// POSIX equivalent: EACCES, EPERM - access-denied, - /// The operation is not supported. - /// - /// POSIX equivalent: EOPNOTSUPP - not-supported, - /// One of the arguments is invalid. - /// - /// POSIX equivalent: EINVAL - invalid-argument, - /// Not enough memory to complete the operation. - /// - /// POSIX equivalent: ENOMEM, ENOBUFS, EAI_MEMORY - out-of-memory, - /// The operation timed out before it could finish completely. - timeout, - /// This operation is incompatible with another asynchronous operation that is already - /// in progress. - /// - /// POSIX equivalent: EALREADY - concurrency-conflict, - /// Trying to finish an asynchronous operation that: - /// - has not been started yet, or: - /// - was already finished by a previous `finish-*` call. - /// - /// Note: this is scheduled to be removed when `future`s are natively supported. - not-in-progress, - /// The operation has been aborted because it could not be completed immediately. - /// - /// Note: this is scheduled to be removed when `future`s are natively supported. - would-block, - /// The operation is not valid in the socket's current state. - invalid-state, - /// A new socket resource could not be created because of a system limit. - new-socket-limit, - /// A bind operation failed because the provided address is not an address that the - /// `network` can bind to. - address-not-bindable, - /// A bind operation failed because the provided address is already in use or because - /// there are no ephemeral ports available. - address-in-use, - /// The remote address is not reachable - remote-unreachable, - /// The TCP connection was forcefully rejected - connection-refused, - /// The TCP connection was reset. - connection-reset, - /// A TCP connection was aborted. - connection-aborted, - /// The size of a datagram sent to a UDP socket exceeded the maximum - /// supported size. - datagram-too-large, - /// Name does not exist or has no suitable associated IP addresses. - name-unresolvable, - /// A temporary failure in name resolution occurred. - temporary-resolver-failure, - /// A permanent failure in name resolution occurred. - permanent-resolver-failure + /// This type corresponds to HTTP standard Methods. + variant method { + get, + head, + post, + put, + delete, + connect, + options, + trace, + patch, + other(string), } - enum ip-address-family { - /// Similar to `AF_INET` in POSIX. - ipv4, - /// Similar to `AF_INET6` in POSIX. - ipv6 + + /// This type corresponds to HTTP standard Related Schemes. + variant scheme { HTTP, HTTPS, other(string) } + + /// Defines the case payload type for `DNS-error` above: + record DNS-error-payload { + rcode: option, + info-code: option, } - type ipv4-address = tuple; - type ipv6-address = tuple; - variant ip-address { - ipv4(ipv4-address), - ipv6(ipv6-address), + + /// Defines the case payload type for `TLS-alert-received` above: + record TLS-alert-received-payload { + alert-id: option, + alert-message: option, } - record ipv4-socket-address { - /// sin_port - port: u16, - /// sin_addr - address: ipv4-address, + + /// Defines the case payload type for `HTTP-response-{header,trailer}-size` above: + record field-size-payload { + field-name: option, + field-size: option, } - record ipv6-socket-address { - /// sin6_port - port: u16, - /// sin6_flowinfo - flow-info: u32, - /// sin6_addr - address: ipv6-address, - /// sin6_scope_id - scope-id: u32, + + /// These cases are inspired by the IANA HTTP Proxy Error Types: + /// https://www.iana.org/assignments/http-proxy-status/http-proxy-status.xhtml#table-http-proxy-error-types + variant error-code { + DNS-timeout, + DNS-error(DNS-error-payload), + destination-not-found, + destination-unavailable, + destination-IP-prohibited, + destination-IP-unroutable, + connection-refused, + connection-terminated, + connection-timeout, + connection-read-timeout, + connection-write-timeout, + connection-limit-reached, + TLS-protocol-error, + TLS-certificate-error, + TLS-alert-received(TLS-alert-received-payload), + HTTP-request-denied, + HTTP-request-length-required, + HTTP-request-body-size(option), + HTTP-request-method-invalid, + HTTP-request-URI-invalid, + HTTP-request-URI-too-long, + HTTP-request-header-section-size(option), + HTTP-request-header-size(option), + HTTP-request-trailer-section-size(option), + HTTP-request-trailer-size(field-size-payload), + HTTP-response-incomplete, + HTTP-response-header-section-size(option), + HTTP-response-header-size(field-size-payload), + HTTP-response-body-size(option), + HTTP-response-trailer-section-size(option), + HTTP-response-trailer-size(field-size-payload), + HTTP-response-transfer-coding(option), + HTTP-response-content-coding(option), + HTTP-response-timeout, + HTTP-upgrade-failed, + HTTP-protocol-error, + loop-detected, + configuration-error, + /// This is a catch-all error for anything that doesn't fit cleanly into a + /// more specific case. It also includes an optional string for an + /// unstructured description of the error. Users should not depend on the + /// string for diagnosing errors, as it's not required to be consistent + /// between implementations. + internal-error(option), } - variant ip-socket-address { - ipv4(ipv4-socket-address), - ipv6(ipv6-socket-address), + + /// This type enumerates the different kinds of errors that may occur when + /// setting or appending to a `fields` resource. + variant header-error { + /// This error indicates that a `field-key` or `field-value` was + /// syntactically invalid when used with an operation that sets headers in a + /// `fields`. + invalid-syntax, + /// This error indicates that a forbidden `field-key` was used when trying + /// to set a header in a `fields`. + forbidden, + /// This error indicates that the operation on the `fields` was not + /// permitted because the fields are immutable. + immutable, } -} -/// This interface provides a value-export of the default network handle.. -interface instance-network { - use network.{network}; + /// Field keys are always strings. + type field-key = string; - /// Get a handle to the default network. - instance-network: func() -> network; -} + /// Field values should always be ASCII strings. However, in + /// reality, HTTP implementations often have to interpret malformed values, + /// so they are provided as a list of bytes. + type field-value = list; -interface ip-name-lookup { - use wasi:io/poll@0.2.0.{pollable}; - use network.{network}; - use network.{error-code}; - use network.{ip-address}; - resource resolve-address-stream { + /// This following block defines the `fields` resource which corresponds to + /// HTTP standard Fields. Fields are a common representation used for both + /// Headers and Trailers. + /// + /// A `fields` may be mutable or immutable. A `fields` created using the + /// constructor, `from-list`, or `clone` will be mutable, but a `fields` + /// resource given by other means (including, but not limited to, + /// `incoming-request.headers`, `outgoing-request.headers`) might be be + /// immutable. In an immutable fields, the `set`, `append`, and `delete` + /// operations will fail with `header-error.immutable`. + resource fields { + /// Construct an empty HTTP Fields. + /// + /// The resulting `fields` is mutable. + constructor(); - /// Returns the next address from the resolver. + /// Append a value for a key. Does not change or delete any existing + /// values for that key. /// - /// This function should be called multiple times. On each call, it will - /// return the next address in connection order preference. If all - /// addresses have been exhausted, this function returns `none`. + /// Fails with `header-error.immutable` if the `fields` are immutable. /// - /// This function never returns IPv4-mapped IPv6 addresses. + /// Fails with `header-error.invalid-syntax` if the `field-key` or + /// `field-value` are syntactically invalid. + append: func(name: field-key, value: field-value) -> result<_, header-error>; + + /// Make a deep copy of the Fields. Equivelant in behavior to calling the + /// `fields` constructor on the return value of `entries`. The resulting + /// `fields` is mutable. + clone: func() -> fields; + + /// Delete all values for a key. Does nothing if no values for the key + /// exist. /// - /// # Typical errors - /// - `name-unresolvable`: Name does not exist or has no suitable associated - /// IP addresses. (EAI_NONAME, EAI_NODATA, EAI_ADDRFAMILY) - /// - `temporary-resolver-failure`: A temporary failure in name resolution occurred. - /// (EAI_AGAIN) - /// - `permanent-resolver-failure`: A permanent failure in name resolution occurred. - /// (EAI_FAIL) - /// - `would-block`: A result is not available yet. (EWOULDBLOCK, EAGAIN) - resolve-next-address: func() -> result, error-code>; + /// Fails with `header-error.immutable` if the `fields` are immutable. + /// + /// Fails with `header-error.invalid-syntax` if the `field-key` is + /// syntactically invalid. + delete: func(name: field-key) -> result<_, header-error>; - /// Create a `pollable` which will resolve once the stream is ready for I/O. + /// Retrieve the full set of keys and values in the Fields. Like the + /// constructor, the list represents each key-value pair. /// - /// Note: this function is here for WASI Preview2 only. - /// It's planned to be removed when `future` is natively supported in Preview3. - subscribe: func() -> pollable; - } + /// The outer list represents each key-value pair in the Fields. Keys + /// which have multiple values are represented by multiple entries in this + /// list with the same key. + entries: func() -> list>; - /// Resolve an internet host name to a list of IP addresses. - /// - /// Unicode domain names are automatically converted to ASCII using IDNA encoding. - /// If the input is an IP address string, the address is parsed and returned - /// as-is without making any external requests. - /// - /// See the wasi-socket proposal README.md for a comparison with getaddrinfo. - /// - /// This function never blocks. It either immediately fails or immediately - /// returns successfully with a `resolve-address-stream` that can be used - /// to (asynchronously) fetch the results. - /// - /// # Typical errors - /// - `invalid-argument`: `name` is a syntactically invalid domain name or IP address. - /// - /// # References: - /// - - /// - - /// - - /// - - resolve-addresses: func(network: borrow, name: string) -> result; -} + /// Get all of the values corresponding to a key. If the key is not present + /// in this `fields` or is syntactically invalid, an empty list is returned. + /// However, if the key is present but empty, this is represented by a list + /// with one or more empty field-values present. + get: func(name: field-key) -> list; -interface tcp { - use wasi:io/streams@0.2.0.{input-stream}; - use wasi:io/streams@0.2.0.{output-stream}; - use wasi:io/poll@0.2.0.{pollable}; - use wasi:clocks/monotonic-clock@0.2.0.{duration}; - use network.{network}; - use network.{error-code}; - use network.{ip-socket-address}; - use network.{ip-address-family}; - enum shutdown-type { - /// Similar to `SHUT_RD` in POSIX. - receive, - /// Similar to `SHUT_WR` in POSIX. - send, - /// Similar to `SHUT_RDWR` in POSIX. - both - } + /// Returns `true` when the key is present in this `fields`. If the key is + /// syntactically invalid, `false` is returned. + has: func(name: field-key) -> bool; - /// A TCP socket resource. - /// - /// The socket can be in one of the following states: - /// - `unbound` - /// - `bind-in-progress` - /// - `bound` (See note below) - /// - `listen-in-progress` - /// - `listening` - /// - `connect-in-progress` - /// - `connected` - /// - `closed` - /// See - /// for a more information. - /// - /// Note: Except where explicitly mentioned, whenever this documentation uses - /// the term "bound" without backticks it actually means: in the `bound` state *or - /// higher*. - /// (i.e. `bound`, `listen-in-progress`, `listening`, `connect-in-progress` or `connected`) - /// - /// In addition to the general error codes documented on the - /// `network::error-code` type, TCP socket methods may always return - /// `error(invalid-state)` when in the `closed` state. - resource tcp-socket { + /// Set all of the values for a key. Clears any existing values for that + /// key, if they have been set. + /// + /// Fails with `header-error.immutable` if the `fields` are immutable. + /// + /// Fails with `header-error.invalid-syntax` if the `field-key` or any of + /// the `field-value`s are syntactically invalid. + set: func(name: field-key, value: list) -> result<_, header-error>; - /// Accept a new client socket. + /// Construct an HTTP Fields. /// - /// The returned socket is bound and in the `connected` state. The following properties - /// are inherited from the listener socket: - /// - `address-family` - /// - `keep-alive-enabled` - /// - `keep-alive-idle-time` - /// - `keep-alive-interval` - /// - `keep-alive-count` - /// - `hop-limit` - /// - `receive-buffer-size` - /// - `send-buffer-size` + /// The resulting `fields` is mutable. /// - /// On success, this function returns the newly accepted client socket along with - /// a pair of streams that can be used to read & write to the connection. + /// The list represents each key-value pair in the Fields. Keys + /// which have multiple values are represented by multiple entries in this + /// list with the same key. /// - /// # Typical errors - /// - `invalid-state`: Socket is not in the `listening` state. (EINVAL) - /// - `would-block`: No pending connections at the moment. (EWOULDBLOCK, EAGAIN) - /// - `connection-aborted`: An incoming connection was pending, but was terminated - /// by the client before this listener could accept it. (ECONNABORTED) - /// - `new-socket-limit`: The new socket resource could not be created because of - /// a system limit. (EMFILE, ENFILE) + /// The tuple is a pair of the field key, represented as a string, and + /// Value, represented as a list of bytes. /// - /// # References - /// - - /// - - /// - - /// - - accept: func() -> result, error-code>; + /// An error result will be returned if any `field-key` or `field-value` is + /// syntactically invalid, or if a field is forbidden. + from-list: static func(entries: list>) -> result; + } - /// Whether this is a IPv4 or IPv6 socket. - /// - /// Equivalent to the SO_DOMAIN socket option. - address-family: func() -> ip-address-family; - finish-bind: func() -> result<_, error-code>; - finish-connect: func() -> result, error-code>; - finish-listen: func() -> result<_, error-code>; + /// Headers is an alias for Fields. + type headers = fields; - /// Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options. - /// - /// If the provided value is 0, an `invalid-argument` error is returned. - /// - /// # Typical errors - /// - `invalid-argument`: (set) The TTL value must be 1 or higher. - hop-limit: func() -> result; + /// Trailers is an alias for Fields. + type trailers = fields; - /// Whether the socket is in the `listening` state. - /// - /// Equivalent to the SO_ACCEPTCONN socket option. - is-listening: func() -> bool; + /// Represents an incoming HTTP Request. + resource incoming-request { - /// The maximum amount of keepalive packets TCP should send before aborting the connection. - /// - /// If the provided value is 0, an `invalid-argument` error is returned. - /// Any other value will never cause an error, but it might be silently clamped and/or - /// rounded. - /// I.e. after setting a value, reading the same setting back may return a different - /// value. - /// - /// Equivalent to the TCP_KEEPCNT socket option. - /// - /// # Typical errors - /// - `invalid-argument`: (set) The provided value was 0. - keep-alive-count: func() -> result; + /// Returns the authority from the request, if it was present. + authority: func() -> option; - /// Enables or disables keepalive. - /// - /// The keepalive behavior can be adjusted using: - /// - `keep-alive-idle-time` - /// - `keep-alive-interval` - /// - `keep-alive-count` - /// These properties can be configured while `keep-alive-enabled` is false, but only - /// come into effect when `keep-alive-enabled` is true. - /// - /// Equivalent to the SO_KEEPALIVE socket option. - keep-alive-enabled: func() -> result; + /// Gives the `incoming-body` associated with this request. Will only + /// return success at most once, and subsequent calls will return error. + consume: func() -> result; - /// Amount of time the connection has to be idle before TCP starts sending keepalive - /// packets. - /// - /// If the provided value is 0, an `invalid-argument` error is returned. - /// Any other value will never cause an error, but it might be silently clamped and/or - /// rounded. - /// I.e. after setting a value, reading the same setting back may return a different - /// value. + /// Get the `headers` associated with the request. /// - /// Equivalent to the TCP_KEEPIDLE socket option. (TCP_KEEPALIVE on MacOS) + /// The returned `headers` resource is immutable: `set`, `append`, and + /// `delete` operations will fail with `header-error.immutable`. /// - /// # Typical errors - /// - `invalid-argument`: (set) The provided value was 0. - keep-alive-idle-time: func() -> result; + /// The `headers` returned are a child resource: it must be dropped before + /// the parent `incoming-request` is dropped. Dropping this + /// `incoming-request` before all children are dropped will trap. + headers: func() -> headers; - /// The time between keepalive packets. - /// - /// If the provided value is 0, an `invalid-argument` error is returned. - /// Any other value will never cause an error, but it might be silently clamped and/or - /// rounded. - /// I.e. after setting a value, reading the same setting back may return a different - /// value. - /// - /// Equivalent to the TCP_KEEPINTVL socket option. - /// - /// # Typical errors - /// - `invalid-argument`: (set) The provided value was 0. - keep-alive-interval: func() -> result; + /// Returns the method of the incoming request. + method: func() -> method; - /// Get the bound local address. - /// - /// POSIX mentions: - /// > If the socket has not been bound to a local name, the value - /// > stored in the object pointed to by `address` is unspecified. - /// - /// WASI is stricter and requires `local-address` to return `invalid-state` when the - /// socket hasn't been bound yet. - /// - /// # Typical errors - /// - `invalid-state`: The socket is not bound to any local address. - /// - /// # References - /// - - /// - - /// - - /// - - local-address: func() -> result; + /// Returns the path with query parameters from the request, as a string. + path-with-query: func() -> option; - /// The kernel buffer space reserved for sends/receives on this socket. - /// - /// If the provided value is 0, an `invalid-argument` error is returned. - /// Any other value will never cause an error, but it might be silently clamped and/or - /// rounded. - /// I.e. after setting a value, reading the same setting back may return a different - /// value. - /// - /// Equivalent to the SO_RCVBUF and SO_SNDBUF socket options. - /// - /// # Typical errors - /// - `invalid-argument`: (set) The provided value was 0. - receive-buffer-size: func() -> result; + /// Returns the protocol scheme from the request. + scheme: func() -> option; + } - /// Get the remote address. + /// Represents an outgoing HTTP Request. + resource outgoing-request { + /// Construct a new `outgoing-request` with a default `method` of `GET`, and + /// `none` values for `path-with-query`, `scheme`, and `authority`. /// - /// # Typical errors - /// - `invalid-state`: The socket is not connected to a remote address. (ENOTCONN) + /// * `headers` is the HTTP Headers for the Request. /// - /// # References - /// - - /// - - /// - - /// - - remote-address: func() -> result; - send-buffer-size: func() -> result; - set-hop-limit: func(value: u8) -> result<_, error-code>; - set-keep-alive-count: func(value: u32) -> result<_, error-code>; - set-keep-alive-enabled: func(value: bool) -> result<_, error-code>; - set-keep-alive-idle-time: func(value: duration) -> result<_, error-code>; - set-keep-alive-interval: func(value: duration) -> result<_, error-code>; + /// It is possible to construct, or manipulate with the accessor functions + /// below, an `outgoing-request` with an invalid combination of `scheme` + /// and `authority`, or `headers` which are not permitted to be sent. + /// It is the obligation of the `outgoing-handler.handle` implementation + /// to reject invalid constructions of `outgoing-request`. + constructor(headers: headers); - /// Hints the desired listen queue size. Implementations are free to ignore this. - /// - /// If the provided value is 0, an `invalid-argument` error is returned. - /// Any other value will never cause an error, but it might be silently clamped and/or - /// rounded. - /// - /// # Typical errors - /// - `not-supported`: (set) The platform does not support changing the backlog - /// size after the initial listen. - /// - `invalid-argument`: (set) The provided value was 0. - /// - `invalid-state`: (set) The socket is in the `connect-in-progress` or - /// `connected` state. - set-listen-backlog-size: func(value: u64) -> result<_, error-code>; - set-receive-buffer-size: func(value: u64) -> result<_, error-code>; - set-send-buffer-size: func(value: u64) -> result<_, error-code>; + /// Get the HTTP Authority for the Request. A value of `none` may be used + /// with Related Schemes which do not require an Authority. The HTTP and + /// HTTPS schemes always require an authority. + authority: func() -> option; - /// Initiate a graceful shutdown. - /// - /// - `receive`: The socket is not expecting to receive any data from - /// the peer. The `input-stream` associated with this socket will be - /// closed. Any data still in the receive queue at time of calling - /// this method will be discarded. - /// - `send`: The socket has no more data to send to the peer. The `output-stream` - /// associated with this socket will be closed and a FIN packet will be sent. - /// - `both`: Same effect as `receive` & `send` combined. - /// - /// This function is idempotent. Shutting a down a direction more than once - /// has no effect and returns `ok`. + /// Returns the resource corresponding to the outgoing Body for this + /// Request. /// - /// The shutdown function does not close (drop) the socket. + /// Returns success on the first call: the `outgoing-body` resource for + /// this `outgoing-request` can be retrieved at most once. Subsequent + /// calls will return error. + body: func() -> result; + + /// Get the headers associated with the Request. /// - /// # Typical errors - /// - `invalid-state`: The socket is not in the `connected` state. (ENOTCONN) + /// The returned `headers` resource is immutable: `set`, `append`, and + /// `delete` operations will fail with `header-error.immutable`. /// - /// # References - /// - - /// - - /// - - /// - - shutdown: func(shutdown-type: shutdown-type) -> result<_, error-code>; + /// This headers resource is a child: it must be dropped before the parent + /// `outgoing-request` is dropped, or its ownership is transfered to + /// another component by e.g. `outgoing-handler.handle`. + headers: func() -> headers; - /// Bind the socket to a specific network on the provided IP address and port. + /// Get the Method for the Request. + method: func() -> method; + + /// Get the combination of the HTTP Path and Query for the Request. + /// When `none`, this represents an empty Path and empty Query. + path-with-query: func() -> option; + + /// Get the HTTP Related Scheme for the Request. When `none`, the + /// implementation may choose an appropriate default scheme. + scheme: func() -> option; + + /// Set the HTTP Authority for the Request. A value of `none` may be used + /// with Related Schemes which do not require an Authority. The HTTP and + /// HTTPS schemes always require an authority. Fails if the string given is + /// not a syntactically valid uri authority. + set-authority: func(authority: option) -> result; + + /// Set the Method for the Request. Fails if the string present in a + /// `method.other` argument is not a syntactically valid method. + set-method: func(method: method) -> result; + + /// Set the combination of the HTTP Path and Query for the Request. + /// When `none`, this represents an empty Path and empty Query. Fails is the + /// string given is not a syntactically valid path and query uri component. + set-path-with-query: func(path-with-query: option) -> result; + + /// Set the HTTP Related Scheme for the Request. When `none`, the + /// implementation may choose an appropriate default scheme. Fails if the + /// string given is not a syntactically valid uri scheme. + set-scheme: func(scheme: option) -> result; + } + + /// Parameters for making an HTTP Request. Each of these parameters is + /// currently an optional timeout applicable to the transport layer of the + /// HTTP protocol. + /// + /// These timeouts are separate from any the user may use to bound a + /// blocking call to `wasi:io/poll.poll`. + resource request-options { + /// Construct a default `request-options` value. + constructor(); + + /// The timeout for receiving subsequent chunks of bytes in the Response + /// body stream. + between-bytes-timeout: func() -> option; + + /// The timeout for the initial connect to the HTTP Server. + connect-timeout: func() -> option; + + /// The timeout for receiving the first byte of the Response body. + first-byte-timeout: func() -> option; + + /// Set the timeout for receiving subsequent chunks of bytes in the Response + /// body stream. An error return value indicates that this timeout is not + /// supported. + set-between-bytes-timeout: func(duration: option) -> result; + + /// Set the timeout for the initial connect to the HTTP Server. An error + /// return value indicates that this timeout is not supported. + set-connect-timeout: func(duration: option) -> result; + + /// Set the timeout for receiving the first byte of the Response body. An + /// error return value indicates that this timeout is not supported. + set-first-byte-timeout: func(duration: option) -> result; + } + + /// Represents the ability to send an HTTP Response. + /// + /// This resource is used by the `wasi:http/incoming-handler` interface to + /// allow a Response to be sent corresponding to the Request provided as the + /// other argument to `incoming-handler.handle`. + resource response-outparam { + + /// Set the value of the `response-outparam` to either send a response, + /// or indicate an error. /// - /// If the IP address is zero (`0.0.0.0` in IPv4, `::` in IPv6), it is left to the - /// implementation to decide which - /// network interface(s) to bind to. - /// If the TCP/UDP port is zero, the socket will be bound to a random free port. + /// This method consumes the `response-outparam` to ensure that it is + /// called at most once. If it is never called, the implementation + /// will respond with an error. /// - /// Bind can be attempted multiple times on the same socket, even with - /// different arguments on each iteration. But never concurrently and - /// only as long as the previous bind failed. Once a bind succeeds, the - /// binding can't be changed anymore. + /// The user may provide an `error` to `response` to allow the + /// implementation determine how to respond with an HTTP error response. + set: static func(param: response-outparam, response: result); + } + + /// This type corresponds to the HTTP standard Status Code. + type status-code = u16; + + /// Represents an incoming HTTP Response. + resource incoming-response { + + /// Returns the incoming body. May be called at most once. Returns error + /// if called additional times. + consume: func() -> result; + + /// Returns the headers from the incoming response. /// - /// # Typical errors - /// - `invalid-argument`: The `local-address` has the wrong address family. - /// (EAFNOSUPPORT, EFAULT on Windows) - /// - `invalid-argument`: `local-address` is not a unicast address. (EINVAL) - /// - `invalid-argument`: `local-address` is an IPv4-mapped IPv6 address. - /// (EINVAL) - /// - `invalid-state`: The socket is already bound. (EINVAL) - /// - `address-in-use`: No ephemeral ports available. (EADDRINUSE, ENOBUFS - /// on Windows) - /// - `address-in-use`: Address is already in use. (EADDRINUSE) - /// - `address-not-bindable`: `local-address` is not an address that the `network` - /// can bind to. (EADDRNOTAVAIL) - /// - `not-in-progress`: A `bind` operation is not in progress. - /// - `would-block`: Can't finish the operation, it is still in progress. - /// (EWOULDBLOCK, EAGAIN) + /// The returned `headers` resource is immutable: `set`, `append`, and + /// `delete` operations will fail with `header-error.immutable`. /// - /// # Implementors note - /// When binding to a non-zero port, this bind operation shouldn't be affected by - /// the TIME_WAIT - /// state of a recently closed socket on the same local address. In practice this - /// means that the SO_REUSEADDR - /// socket option should be set implicitly on all platforms, except on Windows where - /// this is the default behavior - /// and SO_REUSEADDR performs something different entirely. + /// This headers resource is a child: it must be dropped before the parent + /// `incoming-response` is dropped. + headers: func() -> headers; + + /// Returns the status code from the incoming response. + status: func() -> status-code; + } + + /// Represents an incoming HTTP Request or Response's Body. + /// + /// A body has both its contents - a stream of bytes - and a (possibly + /// empty) set of trailers, indicating that the full contents of the + /// body have been received. This resource represents the contents as + /// an `input-stream` and the delivery of trailers as a `future-trailers`, + /// and ensures that the user of this interface may only be consuming either + /// the body contents or waiting on trailers at any given time. + resource incoming-body { + + /// Returns the contents of the body, as a stream of bytes. /// - /// Unlike in POSIX, in WASI the bind operation is async. This enables - /// interactive WASI hosts to inject permission prompts. Runtimes that - /// don't want to make use of this ability can simply call the native - /// `bind` as part of either `start-bind` or `finish-bind`. + /// Returns success on first call: the stream representing the contents + /// can be retrieved at most once. Subsequent calls will return error. /// - /// # References - /// - - /// - - /// - - /// - - start-bind: func(network: borrow, local-address: ip-socket-address) -> result<_, error-code>; - - /// Connect to a remote endpoint. + /// The returned `input-stream` resource is a child: it must be dropped + /// before the parent `incoming-body` is dropped, or consumed by + /// `incoming-body.finish`. /// - /// On success: - /// - the socket is transitioned into the `connection` state. - /// - a pair of streams is returned that can be used to read & write to the connection + /// This invariant ensures that the implementation can determine whether + /// the user is consuming the contents of the body, waiting on the + /// `future-trailers` to be ready, or neither. This allows for network + /// backpressure is to be applied when the user is consuming the body, + /// and for that backpressure to not inhibit delivery of the trailers if + /// the user does not read the entire body. + %stream: func() -> result; + + /// Takes ownership of `incoming-body`, and returns a `future-trailers`. + /// This function will trap if the `input-stream` child is still alive. + finish: static func(this: incoming-body) -> future-trailers; + } + + /// Represents a future which may eventaully return trailers, or an error. + /// + /// In the case that the incoming HTTP Request or Response did not have any + /// trailers, this future will resolve to the empty set of trailers once the + /// complete Request or Response body has been received. + resource future-trailers { + + /// Returns the contents of the trailers, or an error which occured, + /// once the future is ready. /// - /// After a failed connection attempt, the socket will be in the `closed` - /// state and the only valid action left is to `drop` the socket. A single - /// socket can not be used to connect more than once. + /// The outer `option` represents future readiness. Users can wait on this + /// `option` to become `some` using the `subscribe` method. /// - /// # Typical errors - /// - `invalid-argument`: The `remote-address` has the wrong address family. - /// (EAFNOSUPPORT) - /// - `invalid-argument`: `remote-address` is not a unicast address. (EINVAL, - /// ENETUNREACH on Linux, EAFNOSUPPORT on MacOS) - /// - `invalid-argument`: `remote-address` is an IPv4-mapped IPv6 address. - /// (EINVAL, EADDRNOTAVAIL on Illumos) - /// - `invalid-argument`: The IP address in `remote-address` is set to INADDR_ANY - /// (`0.0.0.0` / `::`). (EADDRNOTAVAIL on Windows) - /// - `invalid-argument`: The port in `remote-address` is set to 0. (EADDRNOTAVAIL - /// on Windows) - /// - `invalid-argument`: The socket is already attached to a different network. - /// The `network` passed to `connect` must be identical to the one passed to `bind`. - /// - `invalid-state`: The socket is already in the `connected` state. - /// (EISCONN) - /// - `invalid-state`: The socket is already in the `listening` state. - /// (EOPNOTSUPP, EINVAL on Windows) - /// - `timeout`: Connection timed out. (ETIMEDOUT) - /// - `connection-refused`: The connection was forcefully rejected. (ECONNREFUSED) - /// - `connection-reset`: The connection was reset. (ECONNRESET) - /// - `connection-aborted`: The connection was aborted. (ECONNABORTED) - /// - `remote-unreachable`: The remote address is not reachable. (EHOSTUNREACH, - /// EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET) - /// - `address-in-use`: Tried to perform an implicit bind, but there were - /// no ephemeral ports available. (EADDRINUSE, EADDRNOTAVAIL on Linux, EAGAIN on BSD) - /// - `not-in-progress`: A connect operation is not in progress. - /// - `would-block`: Can't finish the operation, it is still in progress. - /// (EWOULDBLOCK, EAGAIN) + /// The outer `result` is used to retrieve the trailers or error at most + /// once. It will be success on the first call in which the outer option + /// is `some`, and error on subsequent calls. /// - /// # Implementors note - /// The POSIX equivalent of `start-connect` is the regular `connect` syscall. - /// Because all WASI sockets are non-blocking this is expected to return - /// EINPROGRESS, which should be translated to `ok()` in WASI. + /// The inner `result` represents that either the HTTP Request or Response + /// body, as well as any trailers, were received successfully, or that an + /// error occured receiving them. The optional `trailers` indicates whether + /// or not trailers were present in the body. /// - /// The POSIX equivalent of `finish-connect` is a `poll` for event `POLLOUT` - /// with a timeout of 0 on the socket descriptor. Followed by a check for - /// the `SO_ERROR` socket option, in case the poll signaled readiness. + /// When some `trailers` are returned by this method, the `trailers` + /// resource is immutable, and a child. Use of the `set`, `append`, or + /// `delete` methods will return an error, and the resource must be + /// dropped before the parent `future-trailers` is dropped. + get: func() -> option, error-code>>>; + + /// Returns a pollable which becomes ready when either the trailers have + /// been received, or an error has occured. When this pollable is ready, + /// the `get` method will return `some`. + subscribe: func() -> pollable; + } + + /// Represents an outgoing HTTP Response. + resource outgoing-response { + /// Construct an `outgoing-response`, with a default `status-code` of `200`. + /// If a different `status-code` is needed, it must be set via the + /// `set-status-code` method. /// - /// # References - /// - - /// - - /// - - /// - - start-connect: func(network: borrow, remote-address: ip-socket-address) -> result<_, error-code>; + /// * `headers` is the HTTP Headers for the Response. + constructor(headers: headers); - /// Start listening for new connections. + /// Returns the resource corresponding to the outgoing Body for this Response. /// - /// Transitions the socket into the `listening` state. + /// Returns success on the first call: the `outgoing-body` resource for + /// this `outgoing-response` can be retrieved at most once. Subsequent + /// calls will return error. + body: func() -> result; + + /// Get the headers associated with the Request. /// - /// Unlike POSIX, the socket must already be explicitly bound. + /// The returned `headers` resource is immutable: `set`, `append`, and + /// `delete` operations will fail with `header-error.immutable`. /// - /// # Typical errors - /// - `invalid-state`: The socket is not bound to any local address. (EDESTADDRREQ) - /// - `invalid-state`: The socket is already in the `connected` state. - /// (EISCONN, EINVAL on BSD) - /// - `invalid-state`: The socket is already in the `listening` state. - /// - `address-in-use`: Tried to perform an implicit bind, but there were - /// no ephemeral ports available. (EADDRINUSE) - /// - `not-in-progress`: A listen operation is not in progress. - /// - `would-block`: Can't finish the operation, it is still in progress. - /// (EWOULDBLOCK, EAGAIN) + /// This headers resource is a child: it must be dropped before the parent + /// `outgoing-request` is dropped, or its ownership is transfered to + /// another component by e.g. `outgoing-handler.handle`. + headers: func() -> headers; + + /// Set the HTTP Status Code for the Response. Fails if the status-code + /// given is not a valid http status code. + set-status-code: func(status-code: status-code) -> result; + + /// Get the HTTP Status Code for the Response. + status-code: func() -> status-code; + } + + /// Represents an outgoing HTTP Request or Response's Body. + /// + /// A body has both its contents - a stream of bytes - and a (possibly + /// empty) set of trailers, inducating the full contents of the body + /// have been sent. This resource represents the contents as an + /// `output-stream` child resource, and the completion of the body (with + /// optional trailers) with a static function that consumes the + /// `outgoing-body` resource, and ensures that the user of this interface + /// may not write to the body contents after the body has been finished. + /// + /// If the user code drops this resource, as opposed to calling the static + /// method `finish`, the implementation should treat the body as incomplete, + /// and that an error has occured. The implementation should propogate this + /// error to the HTTP protocol by whatever means it has available, + /// including: corrupting the body on the wire, aborting the associated + /// Request, or sending a late status code for the Response. + resource outgoing-body { + + /// Returns a stream for writing the body contents. /// - /// # Implementors note - /// Unlike in POSIX, in WASI the listen operation is async. This enables - /// interactive WASI hosts to inject permission prompts. Runtimes that - /// don't want to make use of this ability can simply call the native - /// `listen` as part of either `start-listen` or `finish-listen`. + /// The returned `output-stream` is a child resource: it must be dropped + /// before the parent `outgoing-body` resource is dropped (or finished), + /// otherwise the `outgoing-body` drop or `finish` will trap. /// - /// # References - /// - - /// - - /// - - /// - - start-listen: func() -> result<_, error-code>; + /// Returns success on the first call: the `output-stream` resource for + /// this `outgoing-body` may be retrieved at most once. Subsequent calls + /// will return error. + write: func() -> result; - /// Create a `pollable` which can be used to poll for, or block on, - /// completion of any of the asynchronous operations of this socket. + /// Finalize an outgoing body, optionally providing trailers. This must be + /// called to signal that the response is complete. If the `outgoing-body` + /// is dropped without calling `outgoing-body.finalize`, the implementation + /// should treat the body as corrupted. /// - /// When `finish-bind`, `finish-listen`, `finish-connect` or `accept` - /// return `error(would-block)`, this pollable can be used to wait for - /// their success or failure, after which the method can be retried. + /// Fails if the body's `outgoing-request` or `outgoing-response` was + /// constructed with a Content-Length header, and the contents written + /// to the body (via `write`) does not match the value given in the + /// Content-Length. + finish: static func(this: outgoing-body, trailers: option) -> result<_, error-code>; + } + + /// Represents a future which may eventaully return an incoming HTTP + /// Response, or an error. + /// + /// This resource is returned by the `wasi:http/outgoing-handler` interface to + /// provide the HTTP Response corresponding to the sent Request. + resource future-incoming-response { + + /// Returns the incoming HTTP Response, or an error, once one is ready. /// - /// The pollable is not limited to the async operation that happens to be - /// in progress at the time of calling `subscribe` (if any). Theoretically, - /// `subscribe` only has to be called once per socket and can then be - /// (re)used for the remainder of the socket's lifetime. + /// The outer `option` represents future readiness. Users can wait on this + /// `option` to become `some` using the `subscribe` method. /// - /// See - /// for a more information. + /// The outer `result` is used to retrieve the response or error at most + /// once. It will be success on the first call in which the outer option + /// is `some`, and error on subsequent calls. /// - /// Note: this function is here for WASI Preview2 only. - /// It's planned to be removed when `future` is natively supported in Preview3. + /// The inner `result` represents that either the incoming HTTP Response + /// status and headers have recieved successfully, or that an error + /// occured. Errors may also occur while consuming the response body, + /// but those will be reported by the `incoming-body` and its + /// `output-stream` child. + get: func() -> option>>; + + /// Returns a pollable which becomes ready when either the Response has + /// been received, or an error has occured. When this pollable is ready, + /// the `get` method will return `some`. subscribe: func() -> pollable; } + + /// Attempts to extract a http-related `error` from the wasi:io `error` + /// provided. + /// + /// Stream operations which return + /// `wasi:io/stream/stream-error::last-operation-failed` have a payload of + /// type `wasi:io/error/error` with more information about the operation + /// that failed. This payload can be passed through to this function to see + /// if there's http-related information about the error to return. + /// + /// Note that this function is fallible because not all io-errors are + /// http-related errors. + http-error-code: func(err: borrow) -> option; +} + +/// This interface defines a handler of incoming HTTP Requests. It should +/// be exported by components which can respond to HTTP Requests. +interface incoming-handler { + use types.{incoming-request}; + use types.{response-outparam}; + + /// This function is invoked with an incoming HTTP Request, and a resource + /// `response-outparam` which provides the capability to reply with an HTTP + /// Response. The response is sent by calling the `response-outparam.set` + /// method, which allows execution to continue after the response has been + /// sent. This enables both streaming to the response body, and performing other + /// work. + /// + /// The implementor of this function must write a response to the + /// `response-outparam` before returning, or else the caller will respond + /// with an error on its behalf. + handle: func(request: incoming-request, response-out: response-outparam); } -interface tcp-create-socket { - use network.{network}; - use network.{error-code}; - use network.{ip-address-family}; - use tcp.{tcp-socket}; +/// This interface defines a handler of outgoing HTTP Requests. It should be +/// imported by components which wish to make HTTP Requests. +interface outgoing-handler { + use types.{outgoing-request}; + use types.{request-options}; + use types.{future-incoming-response}; + use types.{error-code}; - /// Create a new TCP socket. + /// This function is invoked with an outgoing HTTP Request, and it returns + /// a resource `future-incoming-response` which represents an HTTP Response + /// which may arrive in the future. /// - /// Similar to `socket(AF_INET or AF_INET6, SOCK_STREAM, IPPROTO_TCP)` in POSIX. - /// On IPv6 sockets, IPV6_V6ONLY is enabled by default and can't be configured otherwise. + /// The `options` argument accepts optional parameters for the HTTP + /// protocol's transport layer. /// - /// This function does not require a network capability handle. This is considered - /// to be safe because - /// at time of creation, the socket is not bound to any `network` yet. Up to the moment - /// `bind`/`connect` - /// is called, the socket is effectively an in-memory configuration object, unable - /// to communicate with the outside world. + /// This function may return an error if the `outgoing-request` is invalid + /// or not allowed to be made. Otherwise, protocol errors are reported + /// through the `future-incoming-response`. + handle: func(request: outgoing-request, options: option) -> result; +} + +/// The `wasi:http/imports` world imports all the APIs for HTTP proxies. +/// It is intended to be `include`d in other worlds. +world imports { + import wasi:random/random@0.2.0; + import wasi:io/error@0.2.0; + import wasi:io/poll@0.2.0; + import wasi:io/streams@0.2.0; + import wasi:cli/stdout@0.2.0; + import wasi:cli/stderr@0.2.0; + import wasi:cli/stdin@0.2.0; + import wasi:clocks/monotonic-clock@0.2.0; + import types; + import outgoing-handler; + import wasi:clocks/wall-clock@0.2.0; +} + +/// The `wasi:http/proxy` world captures a widely-implementable intersection of +/// hosts that includes HTTP forward and reverse proxies. Components targeting +/// this world may concurrently stream in and out any number of incoming and +/// outgoing HTTP requests. +world proxy { + import wasi:io/poll@0.2.0; + import wasi:clocks/monotonic-clock@0.2.0; + import wasi:io/error@0.2.0; + import wasi:io/streams@0.2.0; + import types; + import wasi:random/random@0.2.0; + import wasi:cli/stdout@0.2.0; + import wasi:cli/stderr@0.2.0; + import wasi:cli/stdin@0.2.0; + import outgoing-handler; + import wasi:clocks/wall-clock@0.2.0; + export incoming-handler; +} + + +package wasi:io@0.2.0; + +interface error { + /// A resource which represents some error information. /// - /// All sockets are non-blocking. Use the wasi-poll interface to block on asynchronous - /// operations. + /// The only method provided by this resource is `to-debug-string`, + /// which provides some human-readable information about the error. /// - /// # Typical errors - /// - `not-supported`: The specified `address-family` is not supported. (EAFNOSUPPORT) - /// - `new-socket-limit`: The new socket resource could not be created because of - /// a system limit. (EMFILE, ENFILE) + /// In the `wasi:io` package, this resource is returned through the + /// `wasi:io/streams/stream-error` type. /// - /// # References - /// - - /// - - /// - - /// - - create-tcp-socket: func(address-family: ip-address-family) -> result; -} - -interface udp { - use wasi:io/poll@0.2.0.{pollable}; - use network.{network}; - use network.{error-code}; - use network.{ip-socket-address}; - use network.{ip-address-family}; + /// To provide more specific error information, other interfaces may + /// provide functions to further "downcast" this error into more specific + /// error information. For example, `error`s returned in streams derived + /// from filesystem types to be described using the filesystem's own + /// error-code type, using the function + /// `wasi:filesystem/types/filesystem-error-code`, which takes a parameter + /// `borrow` and returns + /// `option`. + /// + /// The set of functions which can "downcast" an `error` into a more + /// concrete type is open. + resource error { - /// A received datagram. - record incoming-datagram { - /// The payload. - /// - /// Theoretical max size: ~64 KiB. In practice, typically less than 1500 bytes. - data: list, - /// The source address. - /// - /// This field is guaranteed to match the remote address the stream was initialized - /// with, if any. + /// Returns a string that is suitable to assist humans in debugging + /// this error. /// - /// Equivalent to the `src_addr` out parameter of `recvfrom`. - remote-address: ip-socket-address, + /// WARNING: The returned string should not be consumed mechanically! + /// It may change across platforms, hosts, or other implementation + /// details. Parsing this string is a major platform-compatibility + /// hazard. + to-debug-string: func() -> string; } +} - /// A datagram to be sent out. - record outgoing-datagram { - /// The payload. - data: list, - /// The destination address. +/// A poll API intended to let users wait for I/O events on multiple handles +/// at once. +interface poll { + /// `pollable` represents a single I/O event which may be ready, or not. + resource pollable { + + /// `block` returns immediately if the pollable is ready, and otherwise + /// blocks until ready. /// - /// The requirements on this field depend on how the stream was initialized: - /// - with a remote address: this field must be None or match the stream's remote - /// address exactly. - /// - without a remote address: this field is required. + /// This function is equivalent to calling `poll.poll` on a list + /// containing only this pollable. + block: func(); + + /// Return the readiness of a pollable. This function never blocks. /// - /// If this value is None, the send operation is equivalent to `send` in POSIX. Otherwise - /// it is equivalent to `sendto`. - remote-address: option, + /// Returns `true` when the pollable is ready, and `false` otherwise. + ready: func() -> bool; } - /// A UDP socket handle. - resource udp-socket { + /// Poll for completion on a set of pollables. + /// + /// This function takes a list of pollables, which identify I/O sources of + /// interest, and waits until one or more of the events is ready for I/O. + /// + /// The result `list` contains one or more indices of handles in the + /// argument list that is ready for I/O. + /// + /// If the list contains more elements than can be indexed with a `u32` + /// value, this function traps. + /// + /// A timeout can be implemented by adding a pollable from the + /// wasi-clocks API to the list. + /// + /// This function does not return a `result`; polling in itself does not + /// do any I/O so it doesn't fail. If any of the I/O sources identified by + /// the pollables has an error, it is indicated by marking the source as + /// being reaedy for I/O. + poll: func(in: list>) -> list; +} - /// Whether this is a IPv4 or IPv6 socket. - /// - /// Equivalent to the SO_DOMAIN socket option. - address-family: func() -> ip-address-family; - finish-bind: func() -> result<_, error-code>; +/// WASI I/O is an I/O abstraction API which is currently focused on providing +/// stream types. +/// +/// In the future, the component model is expected to add built-in stream types; +/// when it does, they are expected to subsume this API. +interface streams { + use error.{error}; + use poll.{pollable}; - /// Get the current bound address. - /// - /// POSIX mentions: - /// > If the socket has not been bound to a local name, the value - /// > stored in the object pointed to by `address` is unspecified. - /// - /// WASI is stricter and requires `local-address` to return `invalid-state` when the - /// socket hasn't been bound yet. - /// - /// # Typical errors - /// - `invalid-state`: The socket is not bound to any local address. + /// An error for input-stream and output-stream operations. + variant stream-error { + /// The last operation (a write or flush) failed before completion. /// - /// # References - /// - - /// - - /// - - /// - - local-address: func() -> result; + /// More information is available in the `error` payload. + last-operation-failed(error), + /// The stream is closed: no more input will be accepted by the + /// stream. A closed output-stream will return this error on all + /// future operations. + closed, + } - /// The kernel buffer space reserved for sends/receives on this socket. - /// - /// If the provided value is 0, an `invalid-argument` error is returned. - /// Any other value will never cause an error, but it might be silently clamped and/or - /// rounded. - /// I.e. after setting a value, reading the same setting back may return a different - /// value. - /// - /// Equivalent to the SO_RCVBUF and SO_SNDBUF socket options. - /// - /// # Typical errors - /// - `invalid-argument`: (set) The provided value was 0. - receive-buffer-size: func() -> result; + /// An input bytestream. + /// + /// `input-stream`s are *non-blocking* to the extent practical on underlying + /// platforms. I/O operations always return promptly; if fewer bytes are + /// promptly available than requested, they return the number of bytes promptly + /// available, which could even be zero. To wait for data to be available, + /// use the `subscribe` function to obtain a `pollable` which can be polled + /// for using `wasi:io/poll`. + resource input-stream { + + /// Read bytes from a stream, after blocking until at least one byte can + /// be read. Except for blocking, behavior is identical to `read`. + blocking-read: func(len: u64) -> result, stream-error>; - /// Get the address the socket is currently streaming to. - /// - /// # Typical errors - /// - `invalid-state`: The socket is not streaming to a specific remote address. (ENOTCONN) - /// - /// # References - /// - - /// - - /// - - /// - - remote-address: func() -> result; - send-buffer-size: func() -> result; - set-receive-buffer-size: func(value: u64) -> result<_, error-code>; - set-send-buffer-size: func(value: u64) -> result<_, error-code>; - set-unicast-hop-limit: func(value: u8) -> result<_, error-code>; + /// Skip bytes from a stream, after blocking until at least one byte + /// can be skipped. Except for blocking behavior, identical to `skip`. + blocking-skip: func(len: u64) -> result; - /// Bind the socket to a specific network on the provided IP address and port. + /// Perform a non-blocking read from the stream. /// - /// If the IP address is zero (`0.0.0.0` in IPv4, `::` in IPv6), it is left to the - /// implementation to decide which - /// network interface(s) to bind to. - /// If the port is zero, the socket will be bound to a random free port. + /// When the source of a `read` is binary data, the bytes from the source + /// are returned verbatim. When the source of a `read` is known to the + /// implementation to be text, bytes containing the UTF-8 encoding of the + /// text are returned. /// - /// # Typical errors - /// - `invalid-argument`: The `local-address` has the wrong address family. - /// (EAFNOSUPPORT, EFAULT on Windows) - /// - `invalid-state`: The socket is already bound. (EINVAL) - /// - `address-in-use`: No ephemeral ports available. (EADDRINUSE, ENOBUFS - /// on Windows) - /// - `address-in-use`: Address is already in use. (EADDRINUSE) - /// - `address-not-bindable`: `local-address` is not an address that the `network` - /// can bind to. (EADDRNOTAVAIL) - /// - `not-in-progress`: A `bind` operation is not in progress. - /// - `would-block`: Can't finish the operation, it is still in progress. - /// (EWOULDBLOCK, EAGAIN) + /// This function returns a list of bytes containing the read data, + /// when successful. The returned list will contain up to `len` bytes; + /// it may return fewer than requested, but not more. The list is + /// empty when no bytes are available for reading at this time. The + /// pollable given by `subscribe` will be ready when more bytes are + /// available. /// - /// # Implementors note - /// Unlike in POSIX, in WASI the bind operation is async. This enables - /// interactive WASI hosts to inject permission prompts. Runtimes that - /// don't want to make use of this ability can simply call the native - /// `bind` as part of either `start-bind` or `finish-bind`. + /// This function fails with a `stream-error` when the operation + /// encounters an error, giving `last-operation-failed`, or when the + /// stream is closed, giving `closed`. /// - /// # References - /// - - /// - - /// - - /// - - start-bind: func(network: borrow, local-address: ip-socket-address) -> result<_, error-code>; + /// When the caller gives a `len` of 0, it represents a request to + /// read 0 bytes. If the stream is still open, this call should + /// succeed and return an empty list, or otherwise fail with `closed`. + /// + /// The `len` parameter is a `u64`, which could represent a list of u8 which + /// is not possible to allocate in wasm32, or not desirable to allocate as + /// as a return value by the callee. The callee may return a list of bytes + /// less than `len` in size while more bytes are available for reading. + read: func(len: u64) -> result, stream-error>; - /// Set up inbound & outbound communication channels, optionally to a specific peer. + /// Skip bytes from a stream. Returns number of bytes skipped. /// - /// This function only changes the local socket configuration and does not generate - /// any network traffic. - /// On success, the `remote-address` of the socket is updated. The `local-address` - /// may be updated as well, - /// based on the best network path to `remote-address`. + /// Behaves identical to `read`, except instead of returning a list + /// of bytes, returns the number of bytes consumed from the stream. + skip: func(len: u64) -> result; + + /// Create a `pollable` which will resolve once either the specified stream + /// has bytes available to read or the other end of the stream has been + /// closed. + /// The created `pollable` is a child resource of the `input-stream`. + /// Implementations may trap if the `input-stream` is dropped before + /// all derived `pollable`s created with this function are dropped. + subscribe: func() -> pollable; + } + + /// An output bytestream. + /// + /// `output-stream`s are *non-blocking* to the extent practical on + /// underlying platforms. Except where specified otherwise, I/O operations also + /// always return promptly, after the number of bytes that can be written + /// promptly, which could even be zero. To wait for the stream to be ready to + /// accept data, the `subscribe` function to obtain a `pollable` which can be + /// polled for using `wasi:io/poll`. + resource output-stream { + + /// Request to flush buffered output, and block until flush completes + /// and stream is ready for writing again. + blocking-flush: func() -> result<_, stream-error>; + + /// Read from one stream and write to another, with blocking. /// - /// When a `remote-address` is provided, the returned streams are limited to communicating - /// with that specific peer: - /// - `send` can only be used to send to this destination. - /// - `receive` will only return datagrams sent from the provided `remote-address`. + /// This is similar to `splice`, except that it blocks until the + /// `output-stream` is ready for writing, and the `input-stream` + /// is ready for reading, before performing the `splice`. + blocking-splice: func(src: borrow, len: u64) -> result; + + /// Perform a write of up to 4096 bytes, and then flush the stream. Block + /// until all of these operations are complete, or an error occurs. /// - /// This method may be called multiple times on the same socket to change its association, - /// but - /// only the most recently returned pair of streams will be operational. Implementations - /// may trap if - /// the streams returned by a previous invocation haven't been dropped yet before - /// calling `stream` again. + /// This is a convenience wrapper around the use of `check-write`, + /// `subscribe`, `write`, and `flush`, and is implemented with the + /// following pseudo-code: /// - /// The POSIX equivalent in pseudo-code is: /// ```text - /// if (was previously connected) { - /// connect(s, AF_UNSPEC) - /// } - /// if (remote_address is Some) { - /// connect(s, remote_address) + /// let pollable = this.subscribe(); + /// while !contents.is_empty() { + /// // Wait for the stream to become writable + /// pollable.block(); + /// let Ok(n) = this.check-write(); // eliding error handling + /// let len = min(n, contents.len()); + /// let (chunk, rest) = contents.split_at(len); + /// this.write(chunk ); // eliding error handling + /// contents = rest; /// } + /// this.flush(); + /// // Wait for completion of `flush` + /// pollable.block(); + /// // Check for any errors that arose during `flush` + /// let _ = this.check-write(); // eliding error handling /// ``` - /// - /// Unlike in POSIX, the socket must already be explicitly bound. - /// - /// # Typical errors - /// - `invalid-argument`: The `remote-address` has the wrong address family. - /// (EAFNOSUPPORT) - /// - `invalid-argument`: The IP address in `remote-address` is set to INADDR_ANY - /// (`0.0.0.0` / `::`). (EDESTADDRREQ, EADDRNOTAVAIL) - /// - `invalid-argument`: The port in `remote-address` is set to 0. (EDESTADDRREQ, - /// EADDRNOTAVAIL) - /// - `invalid-state`: The socket is not bound. - /// - `address-in-use`: Tried to perform an implicit bind, but there were - /// no ephemeral ports available. (EADDRINUSE, EADDRNOTAVAIL on Linux, EAGAIN on BSD) - /// - `remote-unreachable`: The remote address is not reachable. (ECONNRESET, - /// ENETRESET, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET) - /// - `connection-refused`: The connection was refused. (ECONNREFUSED) - /// - /// # References - /// - - /// - - /// - - /// - - %stream: func(remote-address: option) -> result, error-code>; - - /// Create a `pollable` which will resolve once the socket is ready for I/O. - /// - /// Note: this function is here for WASI Preview2 only. - /// It's planned to be removed when `future` is natively supported in Preview3. - subscribe: func() -> pollable; + blocking-write-and-flush: func(contents: list) -> result<_, stream-error>; - /// Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options. + /// Perform a write of up to 4096 zeroes, and then flush the stream. + /// Block until all of these operations are complete, or an error + /// occurs. /// - /// If the provided value is 0, an `invalid-argument` error is returned. + /// This is a convenience wrapper around the use of `check-write`, + /// `subscribe`, `write-zeroes`, and `flush`, and is implemented with + /// the following pseudo-code: /// - /// # Typical errors - /// - `invalid-argument`: (set) The TTL value must be 1 or higher. - unicast-hop-limit: func() -> result; - } - resource incoming-datagram-stream { + /// ```text + /// let pollable = this.subscribe(); + /// while num_zeroes != 0 { + /// // Wait for the stream to become writable + /// pollable.block(); + /// let Ok(n) = this.check-write(); // eliding error handling + /// let len = min(n, num_zeroes); + /// this.write-zeroes(len); // eliding error handling + /// num_zeroes -= len; + /// } + /// this.flush(); + /// // Wait for completion of `flush` + /// pollable.block(); + /// // Check for any errors that arose during `flush` + /// let _ = this.check-write(); // eliding error handling + /// ``` + blocking-write-zeroes-and-flush: func(len: u64) -> result<_, stream-error>; - /// Receive messages on the socket. - /// - /// This function attempts to receive up to `max-results` datagrams on the socket - /// without blocking. - /// The returned list may contain fewer elements than requested, but never more. - /// - /// This function returns successfully with an empty list when either: - /// - `max-results` is 0, or: - /// - `max-results` is greater than 0, but no results are immediately available. - /// This function never returns `error(would-block)`. + /// Check readiness for writing. This function never blocks. /// - /// # Typical errors - /// - `remote-unreachable`: The remote address is not reachable. (ECONNRESET, ENETRESET - /// on Windows, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET) - /// - `connection-refused`: The connection was refused. (ECONNREFUSED) + /// Returns the number of bytes permitted for the next call to `write`, + /// or an error. Calling `write` with more bytes than this function has + /// permitted will trap. /// - /// # References - /// - - /// - - /// - - /// - - /// - - /// - - /// - - /// - - receive: func(max-results: u64) -> result, error-code>; + /// When this function returns 0 bytes, the `subscribe` pollable will + /// become ready when this function will report at least 1 byte, or an + /// error. + check-write: func() -> result; - /// Create a `pollable` which will resolve once the stream is ready to receive again. + /// Request to flush buffered output. This function never blocks. /// - /// Note: this function is here for WASI Preview2 only. - /// It's planned to be removed when `future` is natively supported in Preview3. - subscribe: func() -> pollable; - } - resource outgoing-datagram-stream { + /// This tells the output-stream that the caller intends any buffered + /// output to be flushed. the output which is expected to be flushed + /// is all that has been passed to `write` prior to this call. + /// + /// Upon calling this function, the `output-stream` will not accept any + /// writes (`check-write` will return `ok(0)`) until the flush has + /// completed. The `subscribe` pollable will become ready when the + /// flush has completed and the stream can accept more writes. + flush: func() -> result<_, stream-error>; - /// Check readiness for sending. This function never blocks. + /// Read from one stream and write to another. /// - /// Returns the number of datagrams permitted for the next call to `send`, - /// or an error. Calling `send` with more datagrams than this function has - /// permitted will trap. + /// The behavior of splice is equivelant to: + /// 1. calling `check-write` on the `output-stream` + /// 2. calling `read` on the `input-stream` with the smaller of the + /// `check-write` permitted length and the `len` provided to `splice` + /// 3. calling `write` on the `output-stream` with that read data. /// - /// When this function returns ok(0), the `subscribe` pollable will - /// become ready when this function will report at least ok(1), or an - /// error. + /// Any error reported by the call to `check-write`, `read`, or + /// `write` ends the splice and reports that error. /// - /// Never returns `would-block`. - check-send: func() -> result; + /// This function returns the number of bytes transferred; it may be less + /// than `len`. + splice: func(src: borrow, len: u64) -> result; - /// Send messages on the socket. - /// - /// This function attempts to send all provided `datagrams` on the socket without - /// blocking and - /// returns how many messages were actually sent (or queued for sending). This function - /// never - /// returns `error(would-block)`. If none of the datagrams were able to be sent, `ok(0)` - /// is returned. + /// Create a `pollable` which will resolve once the output-stream + /// is ready for more writing, or an error has occured. When this + /// pollable is ready, `check-write` will return `ok(n)` with n>0, or an + /// error. /// - /// This function semantically behaves the same as iterating the `datagrams` list - /// and sequentially - /// sending each individual datagram until either the end of the list has been reached - /// or the first error occurred. - /// If at least one datagram has been sent successfully, this function never returns - /// an error. + /// If the stream is closed, this pollable is always ready immediately. /// - /// If the input list is empty, the function returns `ok(0)`. + /// The created `pollable` is a child resource of the `output-stream`. + /// Implementations may trap if the `output-stream` is dropped before + /// all derived `pollable`s created with this function are dropped. + subscribe: func() -> pollable; + + /// Perform a write. This function never blocks. /// - /// Each call to `send` must be permitted by a preceding `check-send`. Implementations - /// must trap if - /// either `check-send` was not called or `datagrams` contains more items than `check-send` - /// permitted. + /// When the destination of a `write` is binary data, the bytes from + /// `contents` are written verbatim. When the destination of a `write` is + /// known to the implementation to be text, the bytes of `contents` are + /// transcoded from UTF-8 into the encoding of the destination and then + /// written. /// - /// # Typical errors - /// - `invalid-argument`: The `remote-address` has the wrong address family. - /// (EAFNOSUPPORT) - /// - `invalid-argument`: The IP address in `remote-address` is set to INADDR_ANY - /// (`0.0.0.0` / `::`). (EDESTADDRREQ, EADDRNOTAVAIL) - /// - `invalid-argument`: The port in `remote-address` is set to 0. (EDESTADDRREQ, - /// EADDRNOTAVAIL) - /// - `invalid-argument`: The socket is in "connected" mode and `remote-address` - /// is `some` value that does not match the address passed to `stream`. (EISCONN) - /// - `invalid-argument`: The socket is not "connected" and no value for `remote-address` - /// was provided. (EDESTADDRREQ) - /// - `remote-unreachable`: The remote address is not reachable. (ECONNRESET, - /// ENETRESET on Windows, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET) - /// - `connection-refused`: The connection was refused. (ECONNREFUSED) - /// - `datagram-too-large`: The datagram is too large. (EMSGSIZE) + /// Precondition: check-write gave permit of Ok(n) and contents has a + /// length of less than or equal to n. Otherwise, this function will trap. /// - /// # References - /// - - /// - - /// - - /// - - /// - - /// - - /// - - /// - - send: func(datagrams: list) -> result; + /// returns Err(closed) without writing if the stream has closed since + /// the last call to check-write provided a permit. + write: func(contents: list) -> result<_, stream-error>; - /// Create a `pollable` which will resolve once the stream is ready to send again. + /// Write zeroes to a stream. /// - /// Note: this function is here for WASI Preview2 only. - /// It's planned to be removed when `future` is natively supported in Preview3. - subscribe: func() -> pollable; + /// This should be used precisely like `write` with the exact same + /// preconditions (must use check-write first), but instead of + /// passing a list of bytes, you simply pass the number of zero-bytes + /// that should be written. + write-zeroes: func(len: u64) -> result<_, stream-error>; } } -interface udp-create-socket { - use network.{network}; - use network.{error-code}; - use network.{ip-address-family}; - use udp.{udp-socket}; - - /// Create a new UDP socket. - /// - /// Similar to `socket(AF_INET or AF_INET6, SOCK_DGRAM, IPPROTO_UDP)` in POSIX. - /// On IPv6 sockets, IPV6_V6ONLY is enabled by default and can't be configured otherwise. - /// - /// This function does not require a network capability handle. This is considered - /// to be safe because - /// at time of creation, the socket is not bound to any `network` yet. Up to the moment - /// `bind` is called, - /// the socket is effectively an in-memory configuration object, unable to communicate - /// with the outside world. - /// - /// All sockets are non-blocking. Use the wasi-poll interface to block on asynchronous - /// operations. - /// - /// # Typical errors - /// - `not-supported`: The specified `address-family` is not supported. (EAFNOSUPPORT) - /// - `new-socket-limit`: The new socket resource could not be created because of - /// a system limit. (EMFILE, ENFILE) - /// - /// # References: - /// - - /// - - /// - - /// - - create-udp-socket: func(address-family: ip-address-family) -> result; -} - world imports { - import network; - import instance-network; - import wasi:io/poll@0.2.0; - import udp; - import udp-create-socket; - import wasi:io/error@0.2.0; - import wasi:io/streams@0.2.0; - import wasi:clocks/monotonic-clock@0.2.0; - import tcp; - import tcp-create-socket; - import ip-name-lookup; + import error; + import poll; + import streams; } @@ -1956,862 +1791,1027 @@ interface insecure-seed { /// /// # Expected future evolution /// - /// This will likely be changed to a value import, to prevent it from being - /// called multiple times and potentially used for purposes other than DoS - /// protection. - insecure-seed: func() -> tuple; -} - -/// The insecure interface for insecure pseudo-random numbers. -/// -/// It is intended to be portable at least between Unix-family platforms and -/// Windows. -interface insecure { - /// Return `len` insecure pseudo-random bytes. - /// - /// This function is not cryptographically secure. Do not use it for - /// anything related to security. - /// - /// There are no requirements on the values of the returned bytes, however - /// implementations are encouraged to return evenly distributed values with - /// a long period. - get-insecure-random-bytes: func(len: u64) -> list; - - /// Return an insecure pseudo-random `u64` value. - /// - /// This function returns the same type of pseudo-random data as - /// `get-insecure-random-bytes`, represented as a `u64`. - get-insecure-random-u64: func() -> u64; -} - -/// WASI Random is a random data API. -/// -/// It is intended to be portable at least between Unix-family platforms and -/// Windows. -interface random { - /// Return `len` cryptographically-secure random or pseudo-random bytes. - /// - /// This function must produce data at least as cryptographically secure and - /// fast as an adequately seeded cryptographically-secure pseudo-random - /// number generator (CSPRNG). It must not block, from the perspective of - /// the calling program, under any circumstances, including on the first - /// request and on requests for numbers of bytes. The returned data must - /// always be unpredictable. - /// - /// This function must always return fresh data. Deterministic environments - /// must omit this function, rather than implementing it with deterministic - /// data. - get-random-bytes: func(len: u64) -> list; - - /// Return a cryptographically-secure random or pseudo-random `u64` value. - /// - /// This function returns the same type of data as `get-random-bytes`, - /// represented as a `u64`. - get-random-u64: func() -> u64; -} - -world imports { - import random; - import insecure; - import insecure-seed; -} - - -package wasi:cli@0.2.0; - -interface environment { - /// Get the POSIX-style environment variables. - /// - /// Each environment variable is provided as a pair of string variable names - /// and string value. - /// - /// Morally, these are a value import, but until value imports are available - /// in the component model, this import function should return the same - /// values each time it is called. - get-environment: func() -> list>; - - /// Get the POSIX-style arguments to the program. - get-arguments: func() -> list; - - /// Return a path that programs should use as their initial current working - /// directory, interpreting `.` as shorthand for this. - initial-cwd: func() -> option; -} - -interface exit { - /// Exit the current instance and any linked instances. - exit: func(status: result); -} - -interface run { - /// Run the program. - run: func() -> result; -} - -interface stdin { - use wasi:io/streams@0.2.0.{input-stream}; - get-stdin: func() -> input-stream; -} - -interface stdout { - use wasi:io/streams@0.2.0.{output-stream}; - get-stdout: func() -> output-stream; -} - -interface stderr { - use wasi:io/streams@0.2.0.{output-stream}; - get-stderr: func() -> output-stream; -} - -/// Terminal input. -/// -/// In the future, this may include functions for disabling echoing, -/// disabling input buffering so that keyboard events are sent through -/// immediately, querying supported features, and so on. -interface terminal-input { - /// The input side of a terminal. - resource terminal-input; + /// This will likely be changed to a value import, to prevent it from being + /// called multiple times and potentially used for purposes other than DoS + /// protection. + insecure-seed: func() -> tuple; } -/// Terminal output. +/// The insecure interface for insecure pseudo-random numbers. /// -/// In the future, this may include functions for querying the terminal -/// size, being notified of terminal size changes, querying supported -/// features, and so on. -interface terminal-output { - /// The output side of a terminal. - resource terminal-output; +/// It is intended to be portable at least between Unix-family platforms and +/// Windows. +interface insecure { + /// Return `len` insecure pseudo-random bytes. + /// + /// This function is not cryptographically secure. Do not use it for + /// anything related to security. + /// + /// There are no requirements on the values of the returned bytes, however + /// implementations are encouraged to return evenly distributed values with + /// a long period. + get-insecure-random-bytes: func(len: u64) -> list; + + /// Return an insecure pseudo-random `u64` value. + /// + /// This function returns the same type of pseudo-random data as + /// `get-insecure-random-bytes`, represented as a `u64`. + get-insecure-random-u64: func() -> u64; } -/// An interface providing an optional `terminal-input` for stdin as a -/// link-time authority. -interface terminal-stdin { - use terminal-input.{terminal-input}; +/// WASI Random is a random data API. +/// +/// It is intended to be portable at least between Unix-family platforms and +/// Windows. +interface random { + /// Return `len` cryptographically-secure random or pseudo-random bytes. + /// + /// This function must produce data at least as cryptographically secure and + /// fast as an adequately seeded cryptographically-secure pseudo-random + /// number generator (CSPRNG). It must not block, from the perspective of + /// the calling program, under any circumstances, including on the first + /// request and on requests for numbers of bytes. The returned data must + /// always be unpredictable. + /// + /// This function must always return fresh data. Deterministic environments + /// must omit this function, rather than implementing it with deterministic + /// data. + get-random-bytes: func(len: u64) -> list; - /// If stdin is connected to a terminal, return a `terminal-input` handle - /// allowing further interaction with it. - get-terminal-stdin: func() -> option; + /// Return a cryptographically-secure random or pseudo-random `u64` value. + /// + /// This function returns the same type of data as `get-random-bytes`, + /// represented as a `u64`. + get-random-u64: func() -> u64; } -/// An interface providing an optional `terminal-output` for stdout as a -/// link-time authority. -interface terminal-stdout { - use terminal-output.{terminal-output}; - - /// If stdout is connected to a terminal, return a `terminal-output` handle - /// allowing further interaction with it. - get-terminal-stdout: func() -> option; +world imports { + import random; + import insecure; + import insecure-seed; } -/// An interface providing an optional `terminal-output` for stderr as a -/// link-time authority. -interface terminal-stderr { - use terminal-output.{terminal-output}; - /// If stderr is connected to a terminal, return a `terminal-output` handle - /// allowing further interaction with it. - get-terminal-stderr: func() -> option; -} +package wasi:sockets@0.2.0; -world imports { - import environment; - import exit; - import wasi:io/error@0.2.0; - import wasi:io/poll@0.2.0; - import wasi:io/streams@0.2.0; - import stdin; - import stdout; - import stderr; - import terminal-input; - import terminal-output; - import terminal-stdin; - import terminal-stdout; - import terminal-stderr; - import wasi:clocks/monotonic-clock@0.2.0; - import wasi:clocks/wall-clock@0.2.0; - import wasi:filesystem/types@0.2.0; - import wasi:filesystem/preopens@0.2.0; - import wasi:sockets/network@0.2.0; - import wasi:sockets/instance-network@0.2.0; - import wasi:sockets/udp@0.2.0; - import wasi:sockets/udp-create-socket@0.2.0; - import wasi:sockets/tcp@0.2.0; - import wasi:sockets/tcp-create-socket@0.2.0; - import wasi:sockets/ip-name-lookup@0.2.0; - import wasi:random/random@0.2.0; - import wasi:random/insecure@0.2.0; - import wasi:random/insecure-seed@0.2.0; +interface network { + /// An opaque resource that represents access to (a subset of) the network. + /// This enables context-based security for networking. + /// There is no need for this to map 1:1 to a physical network interface. + resource network; + + /// Error codes. + /// + /// In theory, every API can return any error code. + /// In practice, API's typically only return the errors documented per API + /// combined with a couple of errors that are always possible: + /// - `unknown` + /// - `access-denied` + /// - `not-supported` + /// - `out-of-memory` + /// - `concurrency-conflict` + /// + /// See each individual API for what the POSIX equivalents are. They sometimes differ + /// per API. + enum error-code { + /// Unknown error + unknown, + /// Access denied. + /// + /// POSIX equivalent: EACCES, EPERM + access-denied, + /// The operation is not supported. + /// + /// POSIX equivalent: EOPNOTSUPP + not-supported, + /// One of the arguments is invalid. + /// + /// POSIX equivalent: EINVAL + invalid-argument, + /// Not enough memory to complete the operation. + /// + /// POSIX equivalent: ENOMEM, ENOBUFS, EAI_MEMORY + out-of-memory, + /// The operation timed out before it could finish completely. + timeout, + /// This operation is incompatible with another asynchronous operation that is already + /// in progress. + /// + /// POSIX equivalent: EALREADY + concurrency-conflict, + /// Trying to finish an asynchronous operation that: + /// - has not been started yet, or: + /// - was already finished by a previous `finish-*` call. + /// + /// Note: this is scheduled to be removed when `future`s are natively supported. + not-in-progress, + /// The operation has been aborted because it could not be completed immediately. + /// + /// Note: this is scheduled to be removed when `future`s are natively supported. + would-block, + /// The operation is not valid in the socket's current state. + invalid-state, + /// A new socket resource could not be created because of a system limit. + new-socket-limit, + /// A bind operation failed because the provided address is not an address that the + /// `network` can bind to. + address-not-bindable, + /// A bind operation failed because the provided address is already in use or because + /// there are no ephemeral ports available. + address-in-use, + /// The remote address is not reachable + remote-unreachable, + /// The TCP connection was forcefully rejected + connection-refused, + /// The TCP connection was reset. + connection-reset, + /// A TCP connection was aborted. + connection-aborted, + /// The size of a datagram sent to a UDP socket exceeded the maximum + /// supported size. + datagram-too-large, + /// Name does not exist or has no suitable associated IP addresses. + name-unresolvable, + /// A temporary failure in name resolution occurred. + temporary-resolver-failure, + /// A permanent failure in name resolution occurred. + permanent-resolver-failure + } + enum ip-address-family { + /// Similar to `AF_INET` in POSIX. + ipv4, + /// Similar to `AF_INET6` in POSIX. + ipv6 + } + type ipv4-address = tuple; + type ipv6-address = tuple; + variant ip-address { + ipv4(ipv4-address), + ipv6(ipv6-address), + } + record ipv4-socket-address { + /// sin_port + port: u16, + /// sin_addr + address: ipv4-address, + } + record ipv6-socket-address { + /// sin6_port + port: u16, + /// sin6_flowinfo + flow-info: u32, + /// sin6_addr + address: ipv6-address, + /// sin6_scope_id + scope-id: u32, + } + variant ip-socket-address { + ipv4(ipv4-socket-address), + ipv6(ipv6-socket-address), + } } -world command { - import environment; - import exit; - import wasi:io/error@0.2.0; - import wasi:io/poll@0.2.0; - import wasi:io/streams@0.2.0; - import stdin; - import stdout; - import stderr; - import terminal-input; - import terminal-output; - import terminal-stdin; - import terminal-stdout; - import terminal-stderr; - import wasi:clocks/monotonic-clock@0.2.0; - import wasi:clocks/wall-clock@0.2.0; - import wasi:filesystem/types@0.2.0; - import wasi:filesystem/preopens@0.2.0; - import wasi:sockets/network@0.2.0; - import wasi:sockets/instance-network@0.2.0; - import wasi:sockets/udp@0.2.0; - import wasi:sockets/udp-create-socket@0.2.0; - import wasi:sockets/tcp@0.2.0; - import wasi:sockets/tcp-create-socket@0.2.0; - import wasi:sockets/ip-name-lookup@0.2.0; - import wasi:random/random@0.2.0; - import wasi:random/insecure@0.2.0; - import wasi:random/insecure-seed@0.2.0; - export run; +/// This interface provides a value-export of the default network handle.. +interface instance-network { + use network.{network}; + + /// Get a handle to the default network. + instance-network: func() -> network; } +interface ip-name-lookup { + use wasi:io/poll@0.2.0.{pollable}; + use network.{network}; + use network.{error-code}; + use network.{ip-address}; + resource resolve-address-stream { -package wasi:http@0.2.0; + /// Returns the next address from the resolver. + /// + /// This function should be called multiple times. On each call, it will + /// return the next address in connection order preference. If all + /// addresses have been exhausted, this function returns `none`. + /// + /// This function never returns IPv4-mapped IPv6 addresses. + /// + /// # Typical errors + /// - `name-unresolvable`: Name does not exist or has no suitable associated + /// IP addresses. (EAI_NONAME, EAI_NODATA, EAI_ADDRFAMILY) + /// - `temporary-resolver-failure`: A temporary failure in name resolution occurred. + /// (EAI_AGAIN) + /// - `permanent-resolver-failure`: A permanent failure in name resolution occurred. + /// (EAI_FAIL) + /// - `would-block`: A result is not available yet. (EWOULDBLOCK, EAGAIN) + resolve-next-address: func() -> result, error-code>; -/// This interface defines all of the types and methods for implementing -/// HTTP Requests and Responses, both incoming and outgoing, as well as -/// their headers, trailers, and bodies. -interface types { - use wasi:clocks/monotonic-clock@0.2.0.{duration}; + /// Create a `pollable` which will resolve once the stream is ready for I/O. + /// + /// Note: this function is here for WASI Preview2 only. + /// It's planned to be removed when `future` is natively supported in Preview3. + subscribe: func() -> pollable; + } + + /// Resolve an internet host name to a list of IP addresses. + /// + /// Unicode domain names are automatically converted to ASCII using IDNA encoding. + /// If the input is an IP address string, the address is parsed and returned + /// as-is without making any external requests. + /// + /// See the wasi-socket proposal README.md for a comparison with getaddrinfo. + /// + /// This function never blocks. It either immediately fails or immediately + /// returns successfully with a `resolve-address-stream` that can be used + /// to (asynchronously) fetch the results. + /// + /// # Typical errors + /// - `invalid-argument`: `name` is a syntactically invalid domain name or IP address. + /// + /// # References: + /// - + /// - + /// - + /// - + resolve-addresses: func(network: borrow, name: string) -> result; +} + +interface tcp { use wasi:io/streams@0.2.0.{input-stream}; use wasi:io/streams@0.2.0.{output-stream}; - use wasi:io/error@0.2.0.{error as io-error}; use wasi:io/poll@0.2.0.{pollable}; - - /// This type corresponds to HTTP standard Methods. - variant method { - get, - head, - post, - put, - delete, - connect, - options, - trace, - patch, - other(string), + use wasi:clocks/monotonic-clock@0.2.0.{duration}; + use network.{network}; + use network.{error-code}; + use network.{ip-socket-address}; + use network.{ip-address-family}; + enum shutdown-type { + /// Similar to `SHUT_RD` in POSIX. + receive, + /// Similar to `SHUT_WR` in POSIX. + send, + /// Similar to `SHUT_RDWR` in POSIX. + both } - /// This type corresponds to HTTP standard Related Schemes. - variant scheme { HTTP, HTTPS, other(string) } + /// A TCP socket resource. + /// + /// The socket can be in one of the following states: + /// - `unbound` + /// - `bind-in-progress` + /// - `bound` (See note below) + /// - `listen-in-progress` + /// - `listening` + /// - `connect-in-progress` + /// - `connected` + /// - `closed` + /// See + /// for a more information. + /// + /// Note: Except where explicitly mentioned, whenever this documentation uses + /// the term "bound" without backticks it actually means: in the `bound` state *or + /// higher*. + /// (i.e. `bound`, `listen-in-progress`, `listening`, `connect-in-progress` or `connected`) + /// + /// In addition to the general error codes documented on the + /// `network::error-code` type, TCP socket methods may always return + /// `error(invalid-state)` when in the `closed` state. + resource tcp-socket { - /// Defines the case payload type for `DNS-error` above: - record DNS-error-payload { - rcode: option, - info-code: option, - } + /// Accept a new client socket. + /// + /// The returned socket is bound and in the `connected` state. The following properties + /// are inherited from the listener socket: + /// - `address-family` + /// - `keep-alive-enabled` + /// - `keep-alive-idle-time` + /// - `keep-alive-interval` + /// - `keep-alive-count` + /// - `hop-limit` + /// - `receive-buffer-size` + /// - `send-buffer-size` + /// + /// On success, this function returns the newly accepted client socket along with + /// a pair of streams that can be used to read & write to the connection. + /// + /// # Typical errors + /// - `invalid-state`: Socket is not in the `listening` state. (EINVAL) + /// - `would-block`: No pending connections at the moment. (EWOULDBLOCK, EAGAIN) + /// - `connection-aborted`: An incoming connection was pending, but was terminated + /// by the client before this listener could accept it. (ECONNABORTED) + /// - `new-socket-limit`: The new socket resource could not be created because of + /// a system limit. (EMFILE, ENFILE) + /// + /// # References + /// - + /// - + /// - + /// - + accept: func() -> result, error-code>; - /// Defines the case payload type for `TLS-alert-received` above: - record TLS-alert-received-payload { - alert-id: option, - alert-message: option, - } + /// Whether this is a IPv4 or IPv6 socket. + /// + /// Equivalent to the SO_DOMAIN socket option. + address-family: func() -> ip-address-family; + finish-bind: func() -> result<_, error-code>; + finish-connect: func() -> result, error-code>; + finish-listen: func() -> result<_, error-code>; - /// Defines the case payload type for `HTTP-response-{header,trailer}-size` above: - record field-size-payload { - field-name: option, - field-size: option, - } + /// Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options. + /// + /// If the provided value is 0, an `invalid-argument` error is returned. + /// + /// # Typical errors + /// - `invalid-argument`: (set) The TTL value must be 1 or higher. + hop-limit: func() -> result; - /// These cases are inspired by the IANA HTTP Proxy Error Types: - /// https://www.iana.org/assignments/http-proxy-status/http-proxy-status.xhtml#table-http-proxy-error-types - variant error-code { - DNS-timeout, - DNS-error(DNS-error-payload), - destination-not-found, - destination-unavailable, - destination-IP-prohibited, - destination-IP-unroutable, - connection-refused, - connection-terminated, - connection-timeout, - connection-read-timeout, - connection-write-timeout, - connection-limit-reached, - TLS-protocol-error, - TLS-certificate-error, - TLS-alert-received(TLS-alert-received-payload), - HTTP-request-denied, - HTTP-request-length-required, - HTTP-request-body-size(option), - HTTP-request-method-invalid, - HTTP-request-URI-invalid, - HTTP-request-URI-too-long, - HTTP-request-header-section-size(option), - HTTP-request-header-size(option), - HTTP-request-trailer-section-size(option), - HTTP-request-trailer-size(field-size-payload), - HTTP-response-incomplete, - HTTP-response-header-section-size(option), - HTTP-response-header-size(field-size-payload), - HTTP-response-body-size(option), - HTTP-response-trailer-section-size(option), - HTTP-response-trailer-size(field-size-payload), - HTTP-response-transfer-coding(option), - HTTP-response-content-coding(option), - HTTP-response-timeout, - HTTP-upgrade-failed, - HTTP-protocol-error, - loop-detected, - configuration-error, - /// This is a catch-all error for anything that doesn't fit cleanly into a - /// more specific case. It also includes an optional string for an - /// unstructured description of the error. Users should not depend on the - /// string for diagnosing errors, as it's not required to be consistent - /// between implementations. - internal-error(option), - } + /// Whether the socket is in the `listening` state. + /// + /// Equivalent to the SO_ACCEPTCONN socket option. + is-listening: func() -> bool; - /// This type enumerates the different kinds of errors that may occur when - /// setting or appending to a `fields` resource. - variant header-error { - /// This error indicates that a `field-key` or `field-value` was - /// syntactically invalid when used with an operation that sets headers in a - /// `fields`. - invalid-syntax, - /// This error indicates that a forbidden `field-key` was used when trying - /// to set a header in a `fields`. - forbidden, - /// This error indicates that the operation on the `fields` was not - /// permitted because the fields are immutable. - immutable, - } + /// The maximum amount of keepalive packets TCP should send before aborting the connection. + /// + /// If the provided value is 0, an `invalid-argument` error is returned. + /// Any other value will never cause an error, but it might be silently clamped and/or + /// rounded. + /// I.e. after setting a value, reading the same setting back may return a different + /// value. + /// + /// Equivalent to the TCP_KEEPCNT socket option. + /// + /// # Typical errors + /// - `invalid-argument`: (set) The provided value was 0. + keep-alive-count: func() -> result; - /// Field keys are always strings. - type field-key = string; + /// Enables or disables keepalive. + /// + /// The keepalive behavior can be adjusted using: + /// - `keep-alive-idle-time` + /// - `keep-alive-interval` + /// - `keep-alive-count` + /// These properties can be configured while `keep-alive-enabled` is false, but only + /// come into effect when `keep-alive-enabled` is true. + /// + /// Equivalent to the SO_KEEPALIVE socket option. + keep-alive-enabled: func() -> result; - /// Field values should always be ASCII strings. However, in - /// reality, HTTP implementations often have to interpret malformed values, - /// so they are provided as a list of bytes. - type field-value = list; + /// Amount of time the connection has to be idle before TCP starts sending keepalive + /// packets. + /// + /// If the provided value is 0, an `invalid-argument` error is returned. + /// Any other value will never cause an error, but it might be silently clamped and/or + /// rounded. + /// I.e. after setting a value, reading the same setting back may return a different + /// value. + /// + /// Equivalent to the TCP_KEEPIDLE socket option. (TCP_KEEPALIVE on MacOS) + /// + /// # Typical errors + /// - `invalid-argument`: (set) The provided value was 0. + keep-alive-idle-time: func() -> result; - /// This following block defines the `fields` resource which corresponds to - /// HTTP standard Fields. Fields are a common representation used for both - /// Headers and Trailers. - /// - /// A `fields` may be mutable or immutable. A `fields` created using the - /// constructor, `from-list`, or `clone` will be mutable, but a `fields` - /// resource given by other means (including, but not limited to, - /// `incoming-request.headers`, `outgoing-request.headers`) might be be - /// immutable. In an immutable fields, the `set`, `append`, and `delete` - /// operations will fail with `header-error.immutable`. - resource fields { - /// Construct an empty HTTP Fields. + /// The time between keepalive packets. /// - /// The resulting `fields` is mutable. - constructor(); + /// If the provided value is 0, an `invalid-argument` error is returned. + /// Any other value will never cause an error, but it might be silently clamped and/or + /// rounded. + /// I.e. after setting a value, reading the same setting back may return a different + /// value. + /// + /// Equivalent to the TCP_KEEPINTVL socket option. + /// + /// # Typical errors + /// - `invalid-argument`: (set) The provided value was 0. + keep-alive-interval: func() -> result; - /// Append a value for a key. Does not change or delete any existing - /// values for that key. + /// Get the bound local address. /// - /// Fails with `header-error.immutable` if the `fields` are immutable. + /// POSIX mentions: + /// > If the socket has not been bound to a local name, the value + /// > stored in the object pointed to by `address` is unspecified. /// - /// Fails with `header-error.invalid-syntax` if the `field-key` or - /// `field-value` are syntactically invalid. - append: func(name: field-key, value: field-value) -> result<_, header-error>; + /// WASI is stricter and requires `local-address` to return `invalid-state` when the + /// socket hasn't been bound yet. + /// + /// # Typical errors + /// - `invalid-state`: The socket is not bound to any local address. + /// + /// # References + /// - + /// - + /// - + /// - + local-address: func() -> result; - /// Make a deep copy of the Fields. Equivelant in behavior to calling the - /// `fields` constructor on the return value of `entries`. The resulting - /// `fields` is mutable. - clone: func() -> fields; + /// The kernel buffer space reserved for sends/receives on this socket. + /// + /// If the provided value is 0, an `invalid-argument` error is returned. + /// Any other value will never cause an error, but it might be silently clamped and/or + /// rounded. + /// I.e. after setting a value, reading the same setting back may return a different + /// value. + /// + /// Equivalent to the SO_RCVBUF and SO_SNDBUF socket options. + /// + /// # Typical errors + /// - `invalid-argument`: (set) The provided value was 0. + receive-buffer-size: func() -> result; - /// Delete all values for a key. Does nothing if no values for the key - /// exist. + /// Get the remote address. /// - /// Fails with `header-error.immutable` if the `fields` are immutable. + /// # Typical errors + /// - `invalid-state`: The socket is not connected to a remote address. (ENOTCONN) /// - /// Fails with `header-error.invalid-syntax` if the `field-key` is - /// syntactically invalid. - delete: func(name: field-key) -> result<_, header-error>; + /// # References + /// - + /// - + /// - + /// - + remote-address: func() -> result; + send-buffer-size: func() -> result; + set-hop-limit: func(value: u8) -> result<_, error-code>; + set-keep-alive-count: func(value: u32) -> result<_, error-code>; + set-keep-alive-enabled: func(value: bool) -> result<_, error-code>; + set-keep-alive-idle-time: func(value: duration) -> result<_, error-code>; + set-keep-alive-interval: func(value: duration) -> result<_, error-code>; - /// Retrieve the full set of keys and values in the Fields. Like the - /// constructor, the list represents each key-value pair. + /// Hints the desired listen queue size. Implementations are free to ignore this. /// - /// The outer list represents each key-value pair in the Fields. Keys - /// which have multiple values are represented by multiple entries in this - /// list with the same key. - entries: func() -> list>; + /// If the provided value is 0, an `invalid-argument` error is returned. + /// Any other value will never cause an error, but it might be silently clamped and/or + /// rounded. + /// + /// # Typical errors + /// - `not-supported`: (set) The platform does not support changing the backlog + /// size after the initial listen. + /// - `invalid-argument`: (set) The provided value was 0. + /// - `invalid-state`: (set) The socket is in the `connect-in-progress` or + /// `connected` state. + set-listen-backlog-size: func(value: u64) -> result<_, error-code>; + set-receive-buffer-size: func(value: u64) -> result<_, error-code>; + set-send-buffer-size: func(value: u64) -> result<_, error-code>; - /// Get all of the values corresponding to a key. If the key is not present - /// in this `fields` or is syntactically invalid, an empty list is returned. - /// However, if the key is present but empty, this is represented by a list - /// with one or more empty field-values present. - get: func(name: field-key) -> list; + /// Initiate a graceful shutdown. + /// + /// - `receive`: The socket is not expecting to receive any data from + /// the peer. The `input-stream` associated with this socket will be + /// closed. Any data still in the receive queue at time of calling + /// this method will be discarded. + /// - `send`: The socket has no more data to send to the peer. The `output-stream` + /// associated with this socket will be closed and a FIN packet will be sent. + /// - `both`: Same effect as `receive` & `send` combined. + /// + /// This function is idempotent. Shutting a down a direction more than once + /// has no effect and returns `ok`. + /// + /// The shutdown function does not close (drop) the socket. + /// + /// # Typical errors + /// - `invalid-state`: The socket is not in the `connected` state. (ENOTCONN) + /// + /// # References + /// - + /// - + /// - + /// - + shutdown: func(shutdown-type: shutdown-type) -> result<_, error-code>; - /// Returns `true` when the key is present in this `fields`. If the key is - /// syntactically invalid, `false` is returned. - has: func(name: field-key) -> bool; + /// Bind the socket to a specific network on the provided IP address and port. + /// + /// If the IP address is zero (`0.0.0.0` in IPv4, `::` in IPv6), it is left to the + /// implementation to decide which + /// network interface(s) to bind to. + /// If the TCP/UDP port is zero, the socket will be bound to a random free port. + /// + /// Bind can be attempted multiple times on the same socket, even with + /// different arguments on each iteration. But never concurrently and + /// only as long as the previous bind failed. Once a bind succeeds, the + /// binding can't be changed anymore. + /// + /// # Typical errors + /// - `invalid-argument`: The `local-address` has the wrong address family. + /// (EAFNOSUPPORT, EFAULT on Windows) + /// - `invalid-argument`: `local-address` is not a unicast address. (EINVAL) + /// - `invalid-argument`: `local-address` is an IPv4-mapped IPv6 address. + /// (EINVAL) + /// - `invalid-state`: The socket is already bound. (EINVAL) + /// - `address-in-use`: No ephemeral ports available. (EADDRINUSE, ENOBUFS + /// on Windows) + /// - `address-in-use`: Address is already in use. (EADDRINUSE) + /// - `address-not-bindable`: `local-address` is not an address that the `network` + /// can bind to. (EADDRNOTAVAIL) + /// - `not-in-progress`: A `bind` operation is not in progress. + /// - `would-block`: Can't finish the operation, it is still in progress. + /// (EWOULDBLOCK, EAGAIN) + /// + /// # Implementors note + /// When binding to a non-zero port, this bind operation shouldn't be affected by + /// the TIME_WAIT + /// state of a recently closed socket on the same local address. In practice this + /// means that the SO_REUSEADDR + /// socket option should be set implicitly on all platforms, except on Windows where + /// this is the default behavior + /// and SO_REUSEADDR performs something different entirely. + /// + /// Unlike in POSIX, in WASI the bind operation is async. This enables + /// interactive WASI hosts to inject permission prompts. Runtimes that + /// don't want to make use of this ability can simply call the native + /// `bind` as part of either `start-bind` or `finish-bind`. + /// + /// # References + /// - + /// - + /// - + /// - + start-bind: func(network: borrow, local-address: ip-socket-address) -> result<_, error-code>; - /// Set all of the values for a key. Clears any existing values for that - /// key, if they have been set. + /// Connect to a remote endpoint. + /// + /// On success: + /// - the socket is transitioned into the `connection` state. + /// - a pair of streams is returned that can be used to read & write to the connection + /// + /// After a failed connection attempt, the socket will be in the `closed` + /// state and the only valid action left is to `drop` the socket. A single + /// socket can not be used to connect more than once. + /// + /// # Typical errors + /// - `invalid-argument`: The `remote-address` has the wrong address family. + /// (EAFNOSUPPORT) + /// - `invalid-argument`: `remote-address` is not a unicast address. (EINVAL, + /// ENETUNREACH on Linux, EAFNOSUPPORT on MacOS) + /// - `invalid-argument`: `remote-address` is an IPv4-mapped IPv6 address. + /// (EINVAL, EADDRNOTAVAIL on Illumos) + /// - `invalid-argument`: The IP address in `remote-address` is set to INADDR_ANY + /// (`0.0.0.0` / `::`). (EADDRNOTAVAIL on Windows) + /// - `invalid-argument`: The port in `remote-address` is set to 0. (EADDRNOTAVAIL + /// on Windows) + /// - `invalid-argument`: The socket is already attached to a different network. + /// The `network` passed to `connect` must be identical to the one passed to `bind`. + /// - `invalid-state`: The socket is already in the `connected` state. + /// (EISCONN) + /// - `invalid-state`: The socket is already in the `listening` state. + /// (EOPNOTSUPP, EINVAL on Windows) + /// - `timeout`: Connection timed out. (ETIMEDOUT) + /// - `connection-refused`: The connection was forcefully rejected. (ECONNREFUSED) + /// - `connection-reset`: The connection was reset. (ECONNRESET) + /// - `connection-aborted`: The connection was aborted. (ECONNABORTED) + /// - `remote-unreachable`: The remote address is not reachable. (EHOSTUNREACH, + /// EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET) + /// - `address-in-use`: Tried to perform an implicit bind, but there were + /// no ephemeral ports available. (EADDRINUSE, EADDRNOTAVAIL on Linux, EAGAIN on BSD) + /// - `not-in-progress`: A connect operation is not in progress. + /// - `would-block`: Can't finish the operation, it is still in progress. + /// (EWOULDBLOCK, EAGAIN) + /// + /// # Implementors note + /// The POSIX equivalent of `start-connect` is the regular `connect` syscall. + /// Because all WASI sockets are non-blocking this is expected to return + /// EINPROGRESS, which should be translated to `ok()` in WASI. /// - /// Fails with `header-error.immutable` if the `fields` are immutable. + /// The POSIX equivalent of `finish-connect` is a `poll` for event `POLLOUT` + /// with a timeout of 0 on the socket descriptor. Followed by a check for + /// the `SO_ERROR` socket option, in case the poll signaled readiness. /// - /// Fails with `header-error.invalid-syntax` if the `field-key` or any of - /// the `field-value`s are syntactically invalid. - set: func(name: field-key, value: list) -> result<_, header-error>; + /// # References + /// - + /// - + /// - + /// - + start-connect: func(network: borrow, remote-address: ip-socket-address) -> result<_, error-code>; - /// Construct an HTTP Fields. - /// - /// The resulting `fields` is mutable. + /// Start listening for new connections. /// - /// The list represents each key-value pair in the Fields. Keys - /// which have multiple values are represented by multiple entries in this - /// list with the same key. + /// Transitions the socket into the `listening` state. /// - /// The tuple is a pair of the field key, represented as a string, and - /// Value, represented as a list of bytes. + /// Unlike POSIX, the socket must already be explicitly bound. /// - /// An error result will be returned if any `field-key` or `field-value` is - /// syntactically invalid, or if a field is forbidden. - from-list: static func(entries: list>) -> result; - } - - /// Headers is an alias for Fields. - type headers = fields; - - /// Trailers is an alias for Fields. - type trailers = fields; - - /// Represents an incoming HTTP Request. - resource incoming-request { - - /// Returns the authority from the request, if it was present. - authority: func() -> option; - - /// Gives the `incoming-body` associated with this request. Will only - /// return success at most once, and subsequent calls will return error. - consume: func() -> result; - - /// Get the `headers` associated with the request. + /// # Typical errors + /// - `invalid-state`: The socket is not bound to any local address. (EDESTADDRREQ) + /// - `invalid-state`: The socket is already in the `connected` state. + /// (EISCONN, EINVAL on BSD) + /// - `invalid-state`: The socket is already in the `listening` state. + /// - `address-in-use`: Tried to perform an implicit bind, but there were + /// no ephemeral ports available. (EADDRINUSE) + /// - `not-in-progress`: A listen operation is not in progress. + /// - `would-block`: Can't finish the operation, it is still in progress. + /// (EWOULDBLOCK, EAGAIN) /// - /// The returned `headers` resource is immutable: `set`, `append`, and - /// `delete` operations will fail with `header-error.immutable`. + /// # Implementors note + /// Unlike in POSIX, in WASI the listen operation is async. This enables + /// interactive WASI hosts to inject permission prompts. Runtimes that + /// don't want to make use of this ability can simply call the native + /// `listen` as part of either `start-listen` or `finish-listen`. /// - /// The `headers` returned are a child resource: it must be dropped before - /// the parent `incoming-request` is dropped. Dropping this - /// `incoming-request` before all children are dropped will trap. - headers: func() -> headers; - - /// Returns the method of the incoming request. - method: func() -> method; - - /// Returns the path with query parameters from the request, as a string. - path-with-query: func() -> option; - - /// Returns the protocol scheme from the request. - scheme: func() -> option; - } + /// # References + /// - + /// - + /// - + /// - + start-listen: func() -> result<_, error-code>; - /// Represents an outgoing HTTP Request. - resource outgoing-request { - /// Construct a new `outgoing-request` with a default `method` of `GET`, and - /// `none` values for `path-with-query`, `scheme`, and `authority`. - /// - /// * `headers` is the HTTP Headers for the Request. + /// Create a `pollable` which can be used to poll for, or block on, + /// completion of any of the asynchronous operations of this socket. /// - /// It is possible to construct, or manipulate with the accessor functions - /// below, an `outgoing-request` with an invalid combination of `scheme` - /// and `authority`, or `headers` which are not permitted to be sent. - /// It is the obligation of the `outgoing-handler.handle` implementation - /// to reject invalid constructions of `outgoing-request`. - constructor(headers: headers); - - /// Get the HTTP Authority for the Request. A value of `none` may be used - /// with Related Schemes which do not require an Authority. The HTTP and - /// HTTPS schemes always require an authority. - authority: func() -> option; - - /// Returns the resource corresponding to the outgoing Body for this - /// Request. + /// When `finish-bind`, `finish-listen`, `finish-connect` or `accept` + /// return `error(would-block)`, this pollable can be used to wait for + /// their success or failure, after which the method can be retried. /// - /// Returns success on the first call: the `outgoing-body` resource for - /// this `outgoing-request` can be retrieved at most once. Subsequent - /// calls will return error. - body: func() -> result; - - /// Get the headers associated with the Request. + /// The pollable is not limited to the async operation that happens to be + /// in progress at the time of calling `subscribe` (if any). Theoretically, + /// `subscribe` only has to be called once per socket and can then be + /// (re)used for the remainder of the socket's lifetime. /// - /// The returned `headers` resource is immutable: `set`, `append`, and - /// `delete` operations will fail with `header-error.immutable`. + /// See + /// for a more information. /// - /// This headers resource is a child: it must be dropped before the parent - /// `outgoing-request` is dropped, or its ownership is transfered to - /// another component by e.g. `outgoing-handler.handle`. - headers: func() -> headers; - - /// Get the Method for the Request. - method: func() -> method; - - /// Get the combination of the HTTP Path and Query for the Request. - /// When `none`, this represents an empty Path and empty Query. - path-with-query: func() -> option; - - /// Get the HTTP Related Scheme for the Request. When `none`, the - /// implementation may choose an appropriate default scheme. - scheme: func() -> option; - - /// Set the HTTP Authority for the Request. A value of `none` may be used - /// with Related Schemes which do not require an Authority. The HTTP and - /// HTTPS schemes always require an authority. Fails if the string given is - /// not a syntactically valid uri authority. - set-authority: func(authority: option) -> result; - - /// Set the Method for the Request. Fails if the string present in a - /// `method.other` argument is not a syntactically valid method. - set-method: func(method: method) -> result; - - /// Set the combination of the HTTP Path and Query for the Request. - /// When `none`, this represents an empty Path and empty Query. Fails is the - /// string given is not a syntactically valid path and query uri component. - set-path-with-query: func(path-with-query: option) -> result; - - /// Set the HTTP Related Scheme for the Request. When `none`, the - /// implementation may choose an appropriate default scheme. Fails if the - /// string given is not a syntactically valid uri scheme. - set-scheme: func(scheme: option) -> result; + /// Note: this function is here for WASI Preview2 only. + /// It's planned to be removed when `future` is natively supported in Preview3. + subscribe: func() -> pollable; } +} - /// Parameters for making an HTTP Request. Each of these parameters is - /// currently an optional timeout applicable to the transport layer of the - /// HTTP protocol. - /// - /// These timeouts are separate from any the user may use to bound a - /// blocking call to `wasi:io/poll.poll`. - resource request-options { - /// Construct a default `request-options` value. - constructor(); - - /// The timeout for receiving subsequent chunks of bytes in the Response - /// body stream. - between-bytes-timeout: func() -> option; - - /// The timeout for the initial connect to the HTTP Server. - connect-timeout: func() -> option; - - /// The timeout for receiving the first byte of the Response body. - first-byte-timeout: func() -> option; +interface tcp-create-socket { + use network.{network}; + use network.{error-code}; + use network.{ip-address-family}; + use tcp.{tcp-socket}; - /// Set the timeout for receiving subsequent chunks of bytes in the Response - /// body stream. An error return value indicates that this timeout is not - /// supported. - set-between-bytes-timeout: func(duration: option) -> result; + /// Create a new TCP socket. + /// + /// Similar to `socket(AF_INET or AF_INET6, SOCK_STREAM, IPPROTO_TCP)` in POSIX. + /// On IPv6 sockets, IPV6_V6ONLY is enabled by default and can't be configured otherwise. + /// + /// This function does not require a network capability handle. This is considered + /// to be safe because + /// at time of creation, the socket is not bound to any `network` yet. Up to the moment + /// `bind`/`connect` + /// is called, the socket is effectively an in-memory configuration object, unable + /// to communicate with the outside world. + /// + /// All sockets are non-blocking. Use the wasi-poll interface to block on asynchronous + /// operations. + /// + /// # Typical errors + /// - `not-supported`: The specified `address-family` is not supported. (EAFNOSUPPORT) + /// - `new-socket-limit`: The new socket resource could not be created because of + /// a system limit. (EMFILE, ENFILE) + /// + /// # References + /// - + /// - + /// - + /// - + create-tcp-socket: func(address-family: ip-address-family) -> result; +} - /// Set the timeout for the initial connect to the HTTP Server. An error - /// return value indicates that this timeout is not supported. - set-connect-timeout: func(duration: option) -> result; +interface udp { + use wasi:io/poll@0.2.0.{pollable}; + use network.{network}; + use network.{error-code}; + use network.{ip-socket-address}; + use network.{ip-address-family}; - /// Set the timeout for receiving the first byte of the Response body. An - /// error return value indicates that this timeout is not supported. - set-first-byte-timeout: func(duration: option) -> result; + /// A received datagram. + record incoming-datagram { + /// The payload. + /// + /// Theoretical max size: ~64 KiB. In practice, typically less than 1500 bytes. + data: list, + /// The source address. + /// + /// This field is guaranteed to match the remote address the stream was initialized + /// with, if any. + /// + /// Equivalent to the `src_addr` out parameter of `recvfrom`. + remote-address: ip-socket-address, } - /// Represents the ability to send an HTTP Response. - /// - /// This resource is used by the `wasi:http/incoming-handler` interface to - /// allow a Response to be sent corresponding to the Request provided as the - /// other argument to `incoming-handler.handle`. - resource response-outparam { - - /// Set the value of the `response-outparam` to either send a response, - /// or indicate an error. + /// A datagram to be sent out. + record outgoing-datagram { + /// The payload. + data: list, + /// The destination address. /// - /// This method consumes the `response-outparam` to ensure that it is - /// called at most once. If it is never called, the implementation - /// will respond with an error. + /// The requirements on this field depend on how the stream was initialized: + /// - with a remote address: this field must be None or match the stream's remote + /// address exactly. + /// - without a remote address: this field is required. /// - /// The user may provide an `error` to `response` to allow the - /// implementation determine how to respond with an HTTP error response. - set: static func(param: response-outparam, response: result); + /// If this value is None, the send operation is equivalent to `send` in POSIX. Otherwise + /// it is equivalent to `sendto`. + remote-address: option, } - /// This type corresponds to the HTTP standard Status Code. - type status-code = u16; - - /// Represents an incoming HTTP Response. - resource incoming-response { + /// A UDP socket handle. + resource udp-socket { - /// Returns the incoming body. May be called at most once. Returns error - /// if called additional times. - consume: func() -> result; + /// Whether this is a IPv4 or IPv6 socket. + /// + /// Equivalent to the SO_DOMAIN socket option. + address-family: func() -> ip-address-family; + finish-bind: func() -> result<_, error-code>; - /// Returns the headers from the incoming response. + /// Get the current bound address. /// - /// The returned `headers` resource is immutable: `set`, `append`, and - /// `delete` operations will fail with `header-error.immutable`. + /// POSIX mentions: + /// > If the socket has not been bound to a local name, the value + /// > stored in the object pointed to by `address` is unspecified. /// - /// This headers resource is a child: it must be dropped before the parent - /// `incoming-response` is dropped. - headers: func() -> headers; - - /// Returns the status code from the incoming response. - status: func() -> status-code; - } - - /// Represents an incoming HTTP Request or Response's Body. - /// - /// A body has both its contents - a stream of bytes - and a (possibly - /// empty) set of trailers, indicating that the full contents of the - /// body have been received. This resource represents the contents as - /// an `input-stream` and the delivery of trailers as a `future-trailers`, - /// and ensures that the user of this interface may only be consuming either - /// the body contents or waiting on trailers at any given time. - resource incoming-body { + /// WASI is stricter and requires `local-address` to return `invalid-state` when the + /// socket hasn't been bound yet. + /// + /// # Typical errors + /// - `invalid-state`: The socket is not bound to any local address. + /// + /// # References + /// - + /// - + /// - + /// - + local-address: func() -> result; - /// Returns the contents of the body, as a stream of bytes. + /// The kernel buffer space reserved for sends/receives on this socket. /// - /// Returns success on first call: the stream representing the contents - /// can be retrieved at most once. Subsequent calls will return error. + /// If the provided value is 0, an `invalid-argument` error is returned. + /// Any other value will never cause an error, but it might be silently clamped and/or + /// rounded. + /// I.e. after setting a value, reading the same setting back may return a different + /// value. /// - /// The returned `input-stream` resource is a child: it must be dropped - /// before the parent `incoming-body` is dropped, or consumed by - /// `incoming-body.finish`. + /// Equivalent to the SO_RCVBUF and SO_SNDBUF socket options. /// - /// This invariant ensures that the implementation can determine whether - /// the user is consuming the contents of the body, waiting on the - /// `future-trailers` to be ready, or neither. This allows for network - /// backpressure is to be applied when the user is consuming the body, - /// and for that backpressure to not inhibit delivery of the trailers if - /// the user does not read the entire body. - %stream: func() -> result; + /// # Typical errors + /// - `invalid-argument`: (set) The provided value was 0. + receive-buffer-size: func() -> result; - /// Takes ownership of `incoming-body`, and returns a `future-trailers`. - /// This function will trap if the `input-stream` child is still alive. - finish: static func(this: incoming-body) -> future-trailers; - } + /// Get the address the socket is currently streaming to. + /// + /// # Typical errors + /// - `invalid-state`: The socket is not streaming to a specific remote address. (ENOTCONN) + /// + /// # References + /// - + /// - + /// - + /// - + remote-address: func() -> result; + send-buffer-size: func() -> result; + set-receive-buffer-size: func(value: u64) -> result<_, error-code>; + set-send-buffer-size: func(value: u64) -> result<_, error-code>; + set-unicast-hop-limit: func(value: u8) -> result<_, error-code>; - /// Represents a future which may eventaully return trailers, or an error. - /// - /// In the case that the incoming HTTP Request or Response did not have any - /// trailers, this future will resolve to the empty set of trailers once the - /// complete Request or Response body has been received. - resource future-trailers { + /// Bind the socket to a specific network on the provided IP address and port. + /// + /// If the IP address is zero (`0.0.0.0` in IPv4, `::` in IPv6), it is left to the + /// implementation to decide which + /// network interface(s) to bind to. + /// If the port is zero, the socket will be bound to a random free port. + /// + /// # Typical errors + /// - `invalid-argument`: The `local-address` has the wrong address family. + /// (EAFNOSUPPORT, EFAULT on Windows) + /// - `invalid-state`: The socket is already bound. (EINVAL) + /// - `address-in-use`: No ephemeral ports available. (EADDRINUSE, ENOBUFS + /// on Windows) + /// - `address-in-use`: Address is already in use. (EADDRINUSE) + /// - `address-not-bindable`: `local-address` is not an address that the `network` + /// can bind to. (EADDRNOTAVAIL) + /// - `not-in-progress`: A `bind` operation is not in progress. + /// - `would-block`: Can't finish the operation, it is still in progress. + /// (EWOULDBLOCK, EAGAIN) + /// + /// # Implementors note + /// Unlike in POSIX, in WASI the bind operation is async. This enables + /// interactive WASI hosts to inject permission prompts. Runtimes that + /// don't want to make use of this ability can simply call the native + /// `bind` as part of either `start-bind` or `finish-bind`. + /// + /// # References + /// - + /// - + /// - + /// - + start-bind: func(network: borrow, local-address: ip-socket-address) -> result<_, error-code>; - /// Returns the contents of the trailers, or an error which occured, - /// once the future is ready. + /// Set up inbound & outbound communication channels, optionally to a specific peer. /// - /// The outer `option` represents future readiness. Users can wait on this - /// `option` to become `some` using the `subscribe` method. + /// This function only changes the local socket configuration and does not generate + /// any network traffic. + /// On success, the `remote-address` of the socket is updated. The `local-address` + /// may be updated as well, + /// based on the best network path to `remote-address`. /// - /// The outer `result` is used to retrieve the trailers or error at most - /// once. It will be success on the first call in which the outer option - /// is `some`, and error on subsequent calls. + /// When a `remote-address` is provided, the returned streams are limited to communicating + /// with that specific peer: + /// - `send` can only be used to send to this destination. + /// - `receive` will only return datagrams sent from the provided `remote-address`. /// - /// The inner `result` represents that either the HTTP Request or Response - /// body, as well as any trailers, were received successfully, or that an - /// error occured receiving them. The optional `trailers` indicates whether - /// or not trailers were present in the body. + /// This method may be called multiple times on the same socket to change its association, + /// but + /// only the most recently returned pair of streams will be operational. Implementations + /// may trap if + /// the streams returned by a previous invocation haven't been dropped yet before + /// calling `stream` again. /// - /// When some `trailers` are returned by this method, the `trailers` - /// resource is immutable, and a child. Use of the `set`, `append`, or - /// `delete` methods will return an error, and the resource must be - /// dropped before the parent `future-trailers` is dropped. - get: func() -> option, error-code>>>; + /// The POSIX equivalent in pseudo-code is: + /// ```text + /// if (was previously connected) { + /// connect(s, AF_UNSPEC) + /// } + /// if (remote_address is Some) { + /// connect(s, remote_address) + /// } + /// ``` + /// + /// Unlike in POSIX, the socket must already be explicitly bound. + /// + /// # Typical errors + /// - `invalid-argument`: The `remote-address` has the wrong address family. + /// (EAFNOSUPPORT) + /// - `invalid-argument`: The IP address in `remote-address` is set to INADDR_ANY + /// (`0.0.0.0` / `::`). (EDESTADDRREQ, EADDRNOTAVAIL) + /// - `invalid-argument`: The port in `remote-address` is set to 0. (EDESTADDRREQ, + /// EADDRNOTAVAIL) + /// - `invalid-state`: The socket is not bound. + /// - `address-in-use`: Tried to perform an implicit bind, but there were + /// no ephemeral ports available. (EADDRINUSE, EADDRNOTAVAIL on Linux, EAGAIN on BSD) + /// - `remote-unreachable`: The remote address is not reachable. (ECONNRESET, + /// ENETRESET, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET) + /// - `connection-refused`: The connection was refused. (ECONNREFUSED) + /// + /// # References + /// - + /// - + /// - + /// - + %stream: func(remote-address: option) -> result, error-code>; - /// Returns a pollable which becomes ready when either the trailers have - /// been received, or an error has occured. When this pollable is ready, - /// the `get` method will return `some`. + /// Create a `pollable` which will resolve once the socket is ready for I/O. + /// + /// Note: this function is here for WASI Preview2 only. + /// It's planned to be removed when `future` is natively supported in Preview3. subscribe: func() -> pollable; + + /// Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options. + /// + /// If the provided value is 0, an `invalid-argument` error is returned. + /// + /// # Typical errors + /// - `invalid-argument`: (set) The TTL value must be 1 or higher. + unicast-hop-limit: func() -> result; } + resource incoming-datagram-stream { - /// Represents an outgoing HTTP Response. - resource outgoing-response { - /// Construct an `outgoing-response`, with a default `status-code` of `200`. - /// If a different `status-code` is needed, it must be set via the - /// `set-status-code` method. + /// Receive messages on the socket. /// - /// * `headers` is the HTTP Headers for the Response. - constructor(headers: headers); + /// This function attempts to receive up to `max-results` datagrams on the socket + /// without blocking. + /// The returned list may contain fewer elements than requested, but never more. + /// + /// This function returns successfully with an empty list when either: + /// - `max-results` is 0, or: + /// - `max-results` is greater than 0, but no results are immediately available. + /// This function never returns `error(would-block)`. + /// + /// # Typical errors + /// - `remote-unreachable`: The remote address is not reachable. (ECONNRESET, ENETRESET + /// on Windows, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET) + /// - `connection-refused`: The connection was refused. (ECONNREFUSED) + /// + /// # References + /// - + /// - + /// - + /// - + /// - + /// - + /// - + /// - + receive: func(max-results: u64) -> result, error-code>; - /// Returns the resource corresponding to the outgoing Body for this Response. + /// Create a `pollable` which will resolve once the stream is ready to receive again. /// - /// Returns success on the first call: the `outgoing-body` resource for - /// this `outgoing-response` can be retrieved at most once. Subsequent - /// calls will return error. - body: func() -> result; + /// Note: this function is here for WASI Preview2 only. + /// It's planned to be removed when `future` is natively supported in Preview3. + subscribe: func() -> pollable; + } + resource outgoing-datagram-stream { - /// Get the headers associated with the Request. + /// Check readiness for sending. This function never blocks. /// - /// The returned `headers` resource is immutable: `set`, `append`, and - /// `delete` operations will fail with `header-error.immutable`. + /// Returns the number of datagrams permitted for the next call to `send`, + /// or an error. Calling `send` with more datagrams than this function has + /// permitted will trap. /// - /// This headers resource is a child: it must be dropped before the parent - /// `outgoing-request` is dropped, or its ownership is transfered to - /// another component by e.g. `outgoing-handler.handle`. - headers: func() -> headers; - - /// Set the HTTP Status Code for the Response. Fails if the status-code - /// given is not a valid http status code. - set-status-code: func(status-code: status-code) -> result; - - /// Get the HTTP Status Code for the Response. - status-code: func() -> status-code; - } - - /// Represents an outgoing HTTP Request or Response's Body. - /// - /// A body has both its contents - a stream of bytes - and a (possibly - /// empty) set of trailers, inducating the full contents of the body - /// have been sent. This resource represents the contents as an - /// `output-stream` child resource, and the completion of the body (with - /// optional trailers) with a static function that consumes the - /// `outgoing-body` resource, and ensures that the user of this interface - /// may not write to the body contents after the body has been finished. - /// - /// If the user code drops this resource, as opposed to calling the static - /// method `finish`, the implementation should treat the body as incomplete, - /// and that an error has occured. The implementation should propogate this - /// error to the HTTP protocol by whatever means it has available, - /// including: corrupting the body on the wire, aborting the associated - /// Request, or sending a late status code for the Response. - resource outgoing-body { + /// When this function returns ok(0), the `subscribe` pollable will + /// become ready when this function will report at least ok(1), or an + /// error. + /// + /// Never returns `would-block`. + check-send: func() -> result; - /// Returns a stream for writing the body contents. + /// Send messages on the socket. /// - /// The returned `output-stream` is a child resource: it must be dropped - /// before the parent `outgoing-body` resource is dropped (or finished), - /// otherwise the `outgoing-body` drop or `finish` will trap. + /// This function attempts to send all provided `datagrams` on the socket without + /// blocking and + /// returns how many messages were actually sent (or queued for sending). This function + /// never + /// returns `error(would-block)`. If none of the datagrams were able to be sent, `ok(0)` + /// is returned. /// - /// Returns success on the first call: the `output-stream` resource for - /// this `outgoing-body` may be retrieved at most once. Subsequent calls - /// will return error. - write: func() -> result; - - /// Finalize an outgoing body, optionally providing trailers. This must be - /// called to signal that the response is complete. If the `outgoing-body` - /// is dropped without calling `outgoing-body.finalize`, the implementation - /// should treat the body as corrupted. + /// This function semantically behaves the same as iterating the `datagrams` list + /// and sequentially + /// sending each individual datagram until either the end of the list has been reached + /// or the first error occurred. + /// If at least one datagram has been sent successfully, this function never returns + /// an error. /// - /// Fails if the body's `outgoing-request` or `outgoing-response` was - /// constructed with a Content-Length header, and the contents written - /// to the body (via `write`) does not match the value given in the - /// Content-Length. - finish: static func(this: outgoing-body, trailers: option) -> result<_, error-code>; - } - - /// Represents a future which may eventaully return an incoming HTTP - /// Response, or an error. - /// - /// This resource is returned by the `wasi:http/outgoing-handler` interface to - /// provide the HTTP Response corresponding to the sent Request. - resource future-incoming-response { - - /// Returns the incoming HTTP Response, or an error, once one is ready. + /// If the input list is empty, the function returns `ok(0)`. /// - /// The outer `option` represents future readiness. Users can wait on this - /// `option` to become `some` using the `subscribe` method. + /// Each call to `send` must be permitted by a preceding `check-send`. Implementations + /// must trap if + /// either `check-send` was not called or `datagrams` contains more items than `check-send` + /// permitted. /// - /// The outer `result` is used to retrieve the response or error at most - /// once. It will be success on the first call in which the outer option - /// is `some`, and error on subsequent calls. + /// # Typical errors + /// - `invalid-argument`: The `remote-address` has the wrong address family. + /// (EAFNOSUPPORT) + /// - `invalid-argument`: The IP address in `remote-address` is set to INADDR_ANY + /// (`0.0.0.0` / `::`). (EDESTADDRREQ, EADDRNOTAVAIL) + /// - `invalid-argument`: The port in `remote-address` is set to 0. (EDESTADDRREQ, + /// EADDRNOTAVAIL) + /// - `invalid-argument`: The socket is in "connected" mode and `remote-address` + /// is `some` value that does not match the address passed to `stream`. (EISCONN) + /// - `invalid-argument`: The socket is not "connected" and no value for `remote-address` + /// was provided. (EDESTADDRREQ) + /// - `remote-unreachable`: The remote address is not reachable. (ECONNRESET, + /// ENETRESET on Windows, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET) + /// - `connection-refused`: The connection was refused. (ECONNREFUSED) + /// - `datagram-too-large`: The datagram is too large. (EMSGSIZE) /// - /// The inner `result` represents that either the incoming HTTP Response - /// status and headers have recieved successfully, or that an error - /// occured. Errors may also occur while consuming the response body, - /// but those will be reported by the `incoming-body` and its - /// `output-stream` child. - get: func() -> option>>; + /// # References + /// - + /// - + /// - + /// - + /// - + /// - + /// - + /// - + send: func(datagrams: list) -> result; - /// Returns a pollable which becomes ready when either the Response has - /// been received, or an error has occured. When this pollable is ready, - /// the `get` method will return `some`. + /// Create a `pollable` which will resolve once the stream is ready to send again. + /// + /// Note: this function is here for WASI Preview2 only. + /// It's planned to be removed when `future` is natively supported in Preview3. subscribe: func() -> pollable; } - - /// Attempts to extract a http-related `error` from the wasi:io `error` - /// provided. - /// - /// Stream operations which return - /// `wasi:io/stream/stream-error::last-operation-failed` have a payload of - /// type `wasi:io/error/error` with more information about the operation - /// that failed. This payload can be passed through to this function to see - /// if there's http-related information about the error to return. - /// - /// Note that this function is fallible because not all io-errors are - /// http-related errors. - http-error-code: func(err: borrow) -> option; } -/// This interface defines a handler of incoming HTTP Requests. It should -/// be exported by components which can respond to HTTP Requests. -interface incoming-handler { - use types.{incoming-request}; - use types.{response-outparam}; +interface udp-create-socket { + use network.{network}; + use network.{error-code}; + use network.{ip-address-family}; + use udp.{udp-socket}; - /// This function is invoked with an incoming HTTP Request, and a resource - /// `response-outparam` which provides the capability to reply with an HTTP - /// Response. The response is sent by calling the `response-outparam.set` - /// method, which allows execution to continue after the response has been - /// sent. This enables both streaming to the response body, and performing other - /// work. + /// Create a new UDP socket. /// - /// The implementor of this function must write a response to the - /// `response-outparam` before returning, or else the caller will respond - /// with an error on its behalf. - handle: func(request: incoming-request, response-out: response-outparam); -} - -/// This interface defines a handler of outgoing HTTP Requests. It should be -/// imported by components which wish to make HTTP Requests. -interface outgoing-handler { - use types.{outgoing-request}; - use types.{request-options}; - use types.{future-incoming-response}; - use types.{error-code}; - - /// This function is invoked with an outgoing HTTP Request, and it returns - /// a resource `future-incoming-response` which represents an HTTP Response - /// which may arrive in the future. + /// Similar to `socket(AF_INET or AF_INET6, SOCK_DGRAM, IPPROTO_UDP)` in POSIX. + /// On IPv6 sockets, IPV6_V6ONLY is enabled by default and can't be configured otherwise. /// - /// The `options` argument accepts optional parameters for the HTTP - /// protocol's transport layer. + /// This function does not require a network capability handle. This is considered + /// to be safe because + /// at time of creation, the socket is not bound to any `network` yet. Up to the moment + /// `bind` is called, + /// the socket is effectively an in-memory configuration object, unable to communicate + /// with the outside world. /// - /// This function may return an error if the `outgoing-request` is invalid - /// or not allowed to be made. Otherwise, protocol errors are reported - /// through the `future-incoming-response`. - handle: func(request: outgoing-request, options: option) -> result; + /// All sockets are non-blocking. Use the wasi-poll interface to block on asynchronous + /// operations. + /// + /// # Typical errors + /// - `not-supported`: The specified `address-family` is not supported. (EAFNOSUPPORT) + /// - `new-socket-limit`: The new socket resource could not be created because of + /// a system limit. (EMFILE, ENFILE) + /// + /// # References: + /// - + /// - + /// - + /// - + create-udp-socket: func(address-family: ip-address-family) -> result; } -/// The `wasi:http/imports` world imports all the APIs for HTTP proxies. -/// It is intended to be `include`d in other worlds. world imports { - import wasi:random/random@0.2.0; - import wasi:io/error@0.2.0; - import wasi:io/poll@0.2.0; - import wasi:io/streams@0.2.0; - import wasi:cli/stdout@0.2.0; - import wasi:cli/stderr@0.2.0; - import wasi:cli/stdin@0.2.0; - import wasi:clocks/monotonic-clock@0.2.0; - import types; - import outgoing-handler; - import wasi:clocks/wall-clock@0.2.0; -} - -/// The `wasi:http/proxy` world captures a widely-implementable intersection of -/// hosts that includes HTTP forward and reverse proxies. Components targeting -/// this world may concurrently stream in and out any number of incoming and -/// outgoing HTTP requests. -world proxy { + import network; + import instance-network; import wasi:io/poll@0.2.0; - import wasi:clocks/monotonic-clock@0.2.0; + import udp; + import udp-create-socket; import wasi:io/error@0.2.0; import wasi:io/streams@0.2.0; - import types; - import wasi:random/random@0.2.0; - import wasi:cli/stdout@0.2.0; - import wasi:cli/stderr@0.2.0; - import wasi:cli/stdin@0.2.0; - import outgoing-handler; - import wasi:clocks/wall-clock@0.2.0; - export incoming-handler; + import wasi:clocks/monotonic-clock@0.2.0; + import tcp; + import tcp-create-socket; + import ip-name-lookup; } diff --git a/testdata/wit-parser/cross-package-resource.wit.json.golden.wit b/testdata/wit-parser/cross-package-resource.wit.json.golden.wit index 824cd311..1a6a1fe0 100644 --- a/testdata/wit-parser/cross-package-resource.wit.json.golden.wit +++ b/testdata/wit-parser/cross-package-resource.wit.json.golden.wit @@ -1,13 +1,13 @@ -package some:dep; +package foo:bar; interface foo { - resource r; + use some:dep/foo.{r}; + type t = own; } -package foo:bar; +package some:dep; interface foo { - use some:dep/foo.{r}; - type t = own; + resource r; } diff --git a/testdata/wit-parser/foreign-deps-union.wit.json.golden.wit b/testdata/wit-parser/foreign-deps-union.wit.json.golden.wit index 2de213f9..91339096 100644 --- a/testdata/wit-parser/foreign-deps-union.wit.json.golden.wit +++ b/testdata/wit-parser/foreign-deps-union.wit.json.golden.wit @@ -20,37 +20,6 @@ interface the-default { } -package foo:some-pkg; - -interface the-default { - type from-default = string; -} - -interface some-interface { - type another-type = u32; -} - -interface another-interface { - type yet-another-type = u8; -} - - -package foo:wasi; - -interface clocks { - type timestamp = u64; -} - -interface filesystem { - record stat { ino: u64 } -} - -world wasi { - import filesystem; - import clocks; -} - - package foo:root; interface foo { @@ -96,3 +65,34 @@ world unionw-world { export foo:corp/saas; export foo; } + + +package foo:some-pkg; + +interface the-default { + type from-default = string; +} + +interface some-interface { + type another-type = u32; +} + +interface another-interface { + type yet-another-type = u8; +} + + +package foo:wasi; + +interface clocks { + type timestamp = u64; +} + +interface filesystem { + record stat { ino: u64 } +} + +world wasi { + import filesystem; + import clocks; +} diff --git a/testdata/wit-parser/foreign-deps.wit.json.golden.wit b/testdata/wit-parser/foreign-deps.wit.json.golden.wit index 6cd5f1d9..024979fe 100644 --- a/testdata/wit-parser/foreign-deps.wit.json.golden.wit +++ b/testdata/wit-parser/foreign-deps.wit.json.golden.wit @@ -20,32 +20,6 @@ interface the-default { } -package foo:some-pkg; - -interface the-default { - type from-default = string; -} - -interface some-interface { - type another-type = u32; -} - -interface another-interface { - type yet-another-type = u8; -} - - -package foo:wasi; - -interface clocks { - type timestamp = u64; -} - -interface filesystem { - record stat { ino: u64 } -} - - package foo:root; interface foo { @@ -84,3 +58,29 @@ world bars-world { import foo:some-pkg/the-default; import foo:another-pkg/other-interface; } + + +package foo:some-pkg; + +interface the-default { + type from-default = string; +} + +interface some-interface { + type another-type = u32; +} + +interface another-interface { + type yet-another-type = u8; +} + + +package foo:wasi; + +interface clocks { + type timestamp = u64; +} + +interface filesystem { + record stat { ino: u64 } +} diff --git a/testdata/wit-parser/kinds-of-deps.wit.json.golden.wit b/testdata/wit-parser/kinds-of-deps.wit.json.golden.wit index 14dfa433..8b8c25e2 100644 --- a/testdata/wit-parser/kinds-of-deps.wit.json.golden.wit +++ b/testdata/wit-parser/kinds-of-deps.wit.json.golden.wit @@ -1,11 +1,11 @@ -package d:d; - -interface d {} - - -package e:e; +package a:a; -interface e {} +world a { + import b:b/b; + import c:c/c; + import d:d/d; + import e:e/e; +} package b:b; @@ -18,11 +18,11 @@ package c:c; interface c {} -package a:a; +package d:d; -world a { - import b:b/b; - import c:c/c; - import d:d/d; - import e:e/e; -} +interface d {} + + +package e:e; + +interface e {} diff --git a/testdata/wit-parser/multi-package-shared-deps.wit.json.golden.wit b/testdata/wit-parser/multi-package-shared-deps.wit.json.golden.wit index fb7e4266..bcb6992c 100644 --- a/testdata/wit-parser/multi-package-shared-deps.wit.json.golden.wit +++ b/testdata/wit-parser/multi-package-shared-deps.wit.json.golden.wit @@ -1,19 +1,19 @@ -package foo:dep1; +package foo:bar; -interface types {} +world w-bar { + import foo:dep1/types; + import foo:dep2/types; +} -package foo:dep2; +package foo:dep1; interface types {} -package foo:bar; +package foo:dep2; -world w-bar { - import foo:dep1/types; - import foo:dep2/types; -} +interface types {} package foo:qux; diff --git a/testdata/wit-parser/multi-package-transitive-deps.wit.json.golden.wit b/testdata/wit-parser/multi-package-transitive-deps.wit.json.golden.wit index ab5f5e97..b182f0e4 100644 --- a/testdata/wit-parser/multi-package-transitive-deps.wit.json.golden.wit +++ b/testdata/wit-parser/multi-package-transitive-deps.wit.json.golden.wit @@ -1,7 +1,8 @@ -package foo:dep2; +package foo:bar; -interface types { - resource a; +world w-bar { + import foo:dep2/types; + import foo:dep1/types; } @@ -13,11 +14,10 @@ interface types { } -package foo:bar; +package foo:dep2; -world w-bar { - import foo:dep2/types; - import foo:dep1/types; +interface types { + resource a; } diff --git a/testdata/wit-parser/name-both-resource-and-type.wit.json.golden.wit b/testdata/wit-parser/name-both-resource-and-type.wit.json.golden.wit index 81fa2611..8fbee6ec 100644 --- a/testdata/wit-parser/name-both-resource-and-type.wit.json.golden.wit +++ b/testdata/wit-parser/name-both-resource-and-type.wit.json.golden.wit @@ -1,10 +1,3 @@ -package some:dep; - -interface foo { - resource a; -} - - package foo:bar; interface foo { @@ -13,3 +6,10 @@ interface foo { type t2 = borrow; type t3 = borrow; } + + +package some:dep; + +interface foo { + resource a; +} diff --git a/testdata/wit-parser/packages-explicit-internal-references.wit.json.golden.wit b/testdata/wit-parser/packages-explicit-internal-references.wit.json.golden.wit index af2aaba7..0fcc5b89 100644 --- a/testdata/wit-parser/packages-explicit-internal-references.wit.json.golden.wit +++ b/testdata/wit-parser/packages-explicit-internal-references.wit.json.golden.wit @@ -1,10 +1,3 @@ -package foo:name; - -interface i1 { - type a = u32; -} - - package bar:name; world w1 { @@ -14,3 +7,10 @@ world w1 { fn: func(a: a); } } + + +package foo:name; + +interface i1 { + type a = u32; +} From 97c3f00676256fec4e06c211ace1b94dd4056033 Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Sat, 8 Jun 2024 18:28:00 -0400 Subject: [PATCH 12/15] wit: unbreak wasm tests Remove exec.LookPath --- wit/testdata_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wit/testdata_test.go b/wit/testdata_test.go index 327faf58..5b0fdba5 100644 --- a/wit/testdata_test.go +++ b/wit/testdata_test.go @@ -63,7 +63,9 @@ func TestGoldenWITFiles(t *testing.T) { } var canWasmTools = sync.OnceValue[bool](func() bool { - _, err := exec.LookPath("wasm-tools") + // This is explicitly NOT using exec.LookPath so itfails to run on WebAssembly. + // This disables tests that require wasm-tools. + err := exec.Command("wasm-tools", "--version").Run() return err == nil }) From 5447a32186d9ebaccbf97a83f552344636640048 Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Sun, 9 Jun 2024 19:00:38 -0400 Subject: [PATCH 13/15] wit, Makefile: wasm-tools updates - Revised JSON serialization format for Stability - Use --all-features when calling wasm-tools --- Makefile | 2 +- wit/codec.go | 46 +++++++++++++++++--------------------------- wit/load.go | 2 +- wit/testdata_test.go | 2 +- 4 files changed, 21 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index cf09fc88..c7d542dc 100644 --- a/Makefile +++ b/Makefile @@ -5,4 +5,4 @@ json: $(wit_files) .PHONY: $(wit_files) $(wit_files): - wasm-tools component wit -j --features active $@ > $@.json + wasm-tools component wit -j --all-features $@ > $@.json diff --git a/wit/codec.go b/wit/codec.go index 1b99b156..f3f26fe8 100644 --- a/wit/codec.go +++ b/wit/codec.go @@ -41,7 +41,7 @@ func (res *Resolve) ResolveCodec(v any) codec.Codec { case *Handle: return &handleCodec{v} case *Stability: - return &stabilityCodec{v: v} + return &stabilityCodec{v} case *Type: return &typeCodec{v, res} case *TypeDefKind: @@ -472,9 +472,7 @@ func (c *handleCodec) DecodeField(dec codec.Decoder, name string) error { } type stabilityCodec struct { - v *Stability - since semver.Version - feature string + v *Stability } func (c *stabilityCodec) DecodeField(dec codec.Decoder, name string) error { @@ -488,34 +486,26 @@ func (c *stabilityCodec) DecodeField(dec codec.Decoder, name string) error { v := &Unstable{} err = dec.Decode(v) *c.v = v + } + return err +} - // Additional fields for serde tag="type" representation. - // TODO: remove this if the JSON format changes. - case "type": - var typ string - err = dec.Decode(&typ) - switch typ { - case "stable": - *c.v = &Stable{Since: c.since, Feature: c.feature} - case "unstable": - *c.v = &Unstable{Feature: c.feature} - } +func (s *Stable) DecodeField(dec codec.Decoder, name string) error { + switch name { case "since": - err = dec.Decode(&c.since) - switch v := (*c.v).(type) { - case *Stable: - v.Since = c.since - } + return dec.Decode(&s.Since) case "feature": - err = dec.Decode(&c.feature) - switch v := (*c.v).(type) { - case *Stable: - v.Feature = c.feature - case *Unstable: - v.Feature = c.feature - } + return dec.Decode(&s.Feature) } - return err + return nil +} + +func (u *Unstable) DecodeField(dec codec.Decoder, name string) error { + switch name { + case "feature": + return dec.Decode(&u.Feature) + } + return nil } type semverCodec struct { diff --git a/wit/load.go b/wit/load.go index 5a58586c..d4d8983c 100644 --- a/wit/load.go +++ b/wit/load.go @@ -39,7 +39,7 @@ func LoadWIT(path string) (*Resolve, error) { var stdout bytes.Buffer var stderr bytes.Buffer - cmd := exec.Command(wasmTools, "component", "wit", "-j", "--features", "active") + cmd := exec.Command(wasmTools, "component", "wit", "-j", "--all-features") cmd.Stdout = &stdout cmd.Stderr = &stderr if path == "" || path == "-" { diff --git a/wit/testdata_test.go b/wit/testdata_test.go index 5b0fdba5..a8900926 100644 --- a/wit/testdata_test.go +++ b/wit/testdata_test.go @@ -85,7 +85,7 @@ func TestGoldenWITRoundTrip(t *testing.T) { } t.Run(path, func(t *testing.T) { // Run the generated WIT through wasm-tools to generate JSON. - cmd := exec.Command("wasm-tools", "component", "wit", "-j", "--features", "active") + cmd := exec.Command("wasm-tools", "component", "wit", "-j", "--all-features") cmd.Stdin = strings.NewReader(data) stdout := &bytes.Buffer{} stderr := &bytes.Buffer{} From 261db49e2848b22d4f16bd3af801585538e21009 Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Sun, 9 Jun 2024 19:00:52 -0400 Subject: [PATCH 14/15] testdata: wasm-tools updates - Revised JSON serialization format for Stability - Use --all-features when calling wasm-tools --- testdata/wit-parser/feature-gates.wit.json | 456 +++++++++++++++--- .../feature-gates.wit.json.golden.wit | 47 ++ .../wit-parser/since-and-unstable.wit.json | 218 +++++---- .../since-and-unstable.wit.json.golden.wit | 3 + 4 files changed, 563 insertions(+), 161 deletions(-) diff --git a/testdata/wit-parser/feature-gates.wit.json b/testdata/wit-parser/feature-gates.wit.json index e211f045..aa2330e8 100644 --- a/testdata/wit-parser/feature-gates.wit.json +++ b/testdata/wit-parser/feature-gates.wit.json @@ -1,55 +1,113 @@ { "worlds": [ + { + "name": "gated-world", + "imports": { + "interface-7": { + "interface": { + "id": 7, + "stability": { + "unstable": { + "feature": "inactive" + } + } + } + } + }, + "exports": { + "interface-7": { + "interface": { + "id": 7, + "stability": { + "unstable": { + "feature": "inactive" + } + } + } + } + }, + "package": 0, + "stability": { + "unstable": { + "feature": "inactive" + } + } + }, { "name": "ungated-world", "imports": { - "interface-4": { + "interface-8": { "interface": { - "id": 4, + "id": 8, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "active" + } } } } }, "exports": { - "interface-4": { + "interface-8": { "interface": { - "id": 4, + "id": 8, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "active" + } } } } }, "package": 0, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "active" + } } }, { "name": "mixed-world", "imports": { - "interface-4": { + "interface-7": { "interface": { - "id": 4, + "id": 7, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "inactive" + } + } + } + }, + "interface-8": { + "interface": { + "id": 8, + "stability": { + "unstable": { + "feature": "active" + } } } } }, "exports": { - "interface-4": { + "interface-7": { "interface": { - "id": 4, + "id": 7, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "inactive" + } + } + } + }, + "interface-8": { + "interface": { + "id": 8, + "stability": { + "unstable": { + "feature": "active" + } } } } @@ -58,61 +116,128 @@ } ], "interfaces": [ + { + "name": "gated", + "types": {}, + "functions": {}, + "stability": { + "unstable": { + "feature": "not-active" + } + }, + "package": 0 + }, { "name": "ungated", "types": {}, "functions": { + "gated": { + "name": "gated", + "kind": "freestanding", + "params": [], + "results": [], + "stability": { + "unstable": { + "feature": "not-active" + } + } + }, "ungated": { "name": "ungated", "kind": "freestanding", "params": [], "results": [], "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "active" + } } } }, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "active" + } }, "package": 0 }, { "name": "ungated2", "types": { - "ungated": 0, - "ungated2": 1 + "gated": 0, + "gated2": 1, + "gated-with-anonymous-type": 3, + "ungated": 4, + "ungated2": 5 + }, + "functions": {}, + "stability": { + "unstable": { + "feature": "active" + } + }, + "package": 0 + }, + { + "name": "gated-use-target", + "types": { + "t": 6 + }, + "functions": {}, + "stability": { + "unstable": { + "feature": "inactive" + } + }, + "package": 0 + }, + { + "name": "gated-use", + "types": { + "t": 7 }, "functions": {}, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "inactive" + } }, "package": 0 }, { "name": "ungated-use-target", "types": { - "t": 2 + "t": 8 }, "functions": {}, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "active" + } }, "package": 0 }, { "name": "ungated-use", "types": { - "t": 3 + "t": 9 + }, + "functions": {}, + "stability": { + "unstable": { + "feature": "active" + } }, + "package": 0 + }, + { + "name": "gated-for-world", + "types": {}, "functions": {}, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "inactive" + } }, "package": 0 }, @@ -121,60 +246,113 @@ "types": {}, "functions": {}, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "active" + } }, "package": 0 }, { "name": "with-resources", "types": { - "ungated": 4 + "gated": 10, + "ungated": 11 }, "functions": { + "[constructor]gated": { + "name": "[constructor]gated", + "kind": { + "constructor": 10 + }, + "params": [], + "results": [ + { + "type": 14 + } + ], + "stability": { + "unstable": { + "feature": "inactive" + } + } + }, + "[static]gated.x": { + "name": "[static]gated.x", + "kind": { + "static": 10 + }, + "params": [], + "results": [], + "stability": { + "unstable": { + "feature": "inactive" + } + } + }, + "[method]gated.y": { + "name": "[method]gated.y", + "kind": { + "method": 10 + }, + "params": [ + { + "name": "self", + "type": 12 + } + ], + "results": [], + "stability": { + "unstable": { + "feature": "inactive" + } + } + }, "[constructor]ungated": { "name": "[constructor]ungated", "kind": { - "constructor": 4 + "constructor": 11 }, "params": [], "results": [ { - "type": 6 + "type": 15 } ], "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "active" + } } }, "[static]ungated.x": { "name": "[static]ungated.x", "kind": { - "static": 4 + "static": 11 }, "params": [], "results": [], "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "active" + } } }, "[method]ungated.y": { "name": "[method]ungated.y", "kind": { - "method": 4 + "method": 11 }, "params": [ { "name": "self", - "type": 5 + "type": 13 } ], "results": [], "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "active" + } } } }, @@ -183,33 +361,61 @@ ], "types": [ { - "name": "ungated", + "name": "gated", "kind": { "type": "u32" }, "owner": { - "interface": 1 + "interface": 2 }, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "not-active" + } } }, { - "name": "ungated2", + "name": "gated2", "kind": { "type": 0 }, "owner": { - "interface": 1 + "interface": 2 + }, + "stability": { + "unstable": { + "feature": "not-active" + } + } + }, + { + "name": null, + "kind": { + "option": 0 }, + "owner": null, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "not-active" + } } }, { - "name": "t", + "name": "gated-with-anonymous-type", + "kind": { + "option": 2 + }, + "owner": { + "interface": 2 + }, + "stability": { + "unstable": { + "feature": "not-active" + } + } + }, + { + "name": "ungated", "kind": { "type": "u32" }, @@ -217,52 +423,147 @@ "interface": 2 }, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "active" + } + } + }, + { + "name": "ungated2", + "kind": { + "type": 4 + }, + "owner": { + "interface": 2 + }, + "stability": { + "unstable": { + "feature": "active" + } } }, { "name": "t", "kind": { - "type": 2 + "type": "u32" }, "owner": { "interface": 3 }, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "inactive" + } + } + }, + { + "name": "t", + "kind": { + "type": 6 + }, + "owner": { + "interface": 4 + }, + "stability": { + "unstable": { + "feature": "inactive" + } + } + }, + { + "name": "t", + "kind": { + "type": "u32" + }, + "owner": { + "interface": 5 + }, + "stability": { + "unstable": { + "feature": "active" + } + } + }, + { + "name": "t", + "kind": { + "type": 8 + }, + "owner": { + "interface": 6 + }, + "stability": { + "unstable": { + "feature": "active" + } + } + }, + { + "name": "gated", + "kind": "resource", + "owner": { + "interface": 9 + }, + "stability": { + "unstable": { + "feature": "inactive" + } } }, { "name": "ungated", "kind": "resource", "owner": { - "interface": 5 + "interface": 9 }, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "active" + } } }, { "name": null, "kind": { "handle": { - "borrow": 4 + "borrow": 10 } }, "owner": null, "stability": { - "type": "unstable", - "feature": "active" + "unstable": { + "feature": "inactive" + } } }, { "name": null, "kind": { "handle": { - "own": 4 + "borrow": 11 + } + }, + "owner": null, + "stability": { + "unstable": { + "feature": "active" + } + } + }, + { + "name": null, + "kind": { + "handle": { + "own": 10 + } + }, + "owner": null + }, + { + "name": null, + "kind": { + "handle": { + "own": 11 } }, "owner": null @@ -272,16 +573,21 @@ { "name": "a:b", "interfaces": { - "ungated": 0, - "ungated2": 1, - "ungated-use-target": 2, - "ungated-use": 3, - "ungated-for-world": 4, - "with-resources": 5 + "gated": 0, + "ungated": 1, + "ungated2": 2, + "gated-use-target": 3, + "gated-use": 4, + "ungated-use-target": 5, + "ungated-use": 6, + "gated-for-world": 7, + "ungated-for-world": 8, + "with-resources": 9 }, "worlds": { - "ungated-world": 0, - "mixed-world": 1 + "gated-world": 0, + "ungated-world": 1, + "mixed-world": 2 } } ] diff --git a/testdata/wit-parser/feature-gates.wit.json.golden.wit b/testdata/wit-parser/feature-gates.wit.json.golden.wit index 24100060..abbcb5cb 100644 --- a/testdata/wit-parser/feature-gates.wit.json.golden.wit +++ b/testdata/wit-parser/feature-gates.wit.json.golden.wit @@ -1,19 +1,42 @@ package a:b; +@unstable(feature = not-active) +interface gated {} + @unstable(feature = active) interface ungated { + @unstable(feature = not-active) + gated: func(); @unstable(feature = active) ungated: func(); } @unstable(feature = active) interface ungated2 { + @unstable(feature = not-active) + type gated = u32; + @unstable(feature = not-active) + type gated2 = gated; + @unstable(feature = not-active) + type gated-with-anonymous-type = option>; @unstable(feature = active) type ungated = u32; @unstable(feature = active) type ungated2 = ungated; } +@unstable(feature = inactive) +interface gated-use-target { + @unstable(feature = inactive) + type t = u32; +} + +@unstable(feature = inactive) +interface gated-use { + @unstable(feature = inactive) + use gated-use-target.{t}; +} + @unstable(feature = active) interface ungated-use-target { @unstable(feature = active) @@ -26,10 +49,22 @@ interface ungated-use { use ungated-use-target.{t}; } +@unstable(feature = inactive) +interface gated-for-world {} + @unstable(feature = active) interface ungated-for-world {} interface with-resources { + @unstable(feature = inactive) + resource gated { + @unstable(feature = inactive) + constructor(); + @unstable(feature = inactive) + y: func(); + @unstable(feature = inactive) + x: static func(); + } @unstable(feature = active) resource ungated { @unstable(feature = active) @@ -41,6 +76,14 @@ interface with-resources { } } +@unstable(feature = inactive) +world gated-world { + @unstable(feature = inactive) + import gated-for-world; + @unstable(feature = inactive) + export gated-for-world; +} + @unstable(feature = active) world ungated-world { @unstable(feature = active) @@ -50,8 +93,12 @@ world ungated-world { } world mixed-world { + @unstable(feature = inactive) + import gated-for-world; @unstable(feature = active) import ungated-for-world; + @unstable(feature = inactive) + export gated-for-world; @unstable(feature = active) export ungated-for-world; } diff --git a/testdata/wit-parser/since-and-unstable.wit.json b/testdata/wit-parser/since-and-unstable.wit.json index c5efc938..1f13de7b 100644 --- a/testdata/wit-parser/since-and-unstable.wit.json +++ b/testdata/wit-parser/since-and-unstable.wit.json @@ -6,8 +6,9 @@ "exports": {}, "package": 0, "stability": { - "type": "stable", - "since": "1.0.1" + "stable": { + "since": "1.0.1" + } } }, { @@ -16,8 +17,9 @@ "exports": {}, "package": 0, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { @@ -25,19 +27,21 @@ "imports": { "y": { "interface": { - "id": 5, + "id": 6, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } } }, - "interface-4": { + "interface-5": { "interface": { - "id": 4, + "id": 5, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } } }, @@ -69,8 +73,9 @@ "params": [], "results": [], "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } } }, @@ -87,8 +92,9 @@ } ], "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } } } @@ -101,26 +107,29 @@ "params": [], "results": [], "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } } }, "y": { "interface": { - "id": 6, + "id": 7, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } } }, - "interface-4": { + "interface-5": { "interface": { - "id": 4, + "id": 5, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } } } @@ -134,8 +143,9 @@ "types": {}, "functions": {}, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } }, "package": 0 }, @@ -144,9 +154,10 @@ "types": {}, "functions": {}, "stability": { - "type": "stable", - "since": "1.0.0", - "feature": "foo" + "stable": { + "since": "1.0.0", + "feature": "foo" + } }, "package": 0 }, @@ -155,9 +166,21 @@ "types": {}, "functions": {}, "stability": { - "type": "stable", - "since": "1.0.0", - "feature": "foo-bar" + "stable": { + "since": "1.0.0", + "feature": "foo-bar" + } + }, + "package": 0 + }, + { + "name": "foo4", + "types": {}, + "functions": {}, + "stability": { + "unstable": { + "feature": "foo2" + } }, "package": 0 }, @@ -180,8 +203,9 @@ "params": [], "results": [], "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, "[constructor]r3": { @@ -196,8 +220,9 @@ } ], "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, "[static]r3.x1": { @@ -208,8 +233,9 @@ "params": [], "results": [], "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, "[method]r3.x2": { @@ -225,8 +251,9 @@ ], "results": [], "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } } }, @@ -243,8 +270,9 @@ "types": {}, "functions": {}, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } }, "package": 0 }, @@ -253,8 +281,9 @@ "types": {}, "functions": {}, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } }, "package": 0 } @@ -264,22 +293,24 @@ "name": "r1", "kind": "resource", "owner": { - "interface": 3 + "interface": 4 }, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { "name": "r2", "kind": "resource", "owner": { - "interface": 3 + "interface": 4 }, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { @@ -288,11 +319,12 @@ "type": "u32" }, "owner": { - "interface": 3 + "interface": 4 }, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { @@ -308,11 +340,12 @@ } }, "owner": { - "interface": 3 + "interface": 4 }, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { @@ -327,11 +360,12 @@ } }, "owner": { - "interface": 3 + "interface": 4 }, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { @@ -346,11 +380,12 @@ } }, "owner": { - "interface": 3 + "interface": 4 }, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { @@ -366,22 +401,24 @@ } }, "owner": { - "interface": 3 + "interface": 4 }, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { "name": "r3", "kind": "resource", "owner": { - "interface": 3 + "interface": 4 }, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { @@ -393,8 +430,9 @@ }, "owner": null, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { @@ -413,8 +451,9 @@ "world": 2 }, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { @@ -432,8 +471,9 @@ "world": 2 }, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { @@ -452,8 +492,9 @@ "world": 2 }, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { @@ -471,8 +512,9 @@ "world": 2 }, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { @@ -484,8 +526,9 @@ "world": 2 }, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { @@ -495,8 +538,9 @@ "world": 2 }, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { @@ -506,8 +550,9 @@ "world": 2 }, "stability": { - "type": "stable", - "since": "1.0.0" + "stable": { + "since": "1.0.0" + } } }, { @@ -536,8 +581,9 @@ "foo1": 0, "foo2": 1, "foo3": 2, - "in-an-interface": 3, - "z": 4 + "foo4": 3, + "in-an-interface": 4, + "z": 5 }, "worlds": { "w1": 0, diff --git a/testdata/wit-parser/since-and-unstable.wit.json.golden.wit b/testdata/wit-parser/since-and-unstable.wit.json.golden.wit index a4423a7a..c4abf4b5 100644 --- a/testdata/wit-parser/since-and-unstable.wit.json.golden.wit +++ b/testdata/wit-parser/since-and-unstable.wit.json.golden.wit @@ -9,6 +9,9 @@ interface foo2 {} @since(version = 1.0.0, feature = foo-bar) interface foo3 {} +@unstable(feature = foo2) +interface foo4 {} + interface in-an-interface { @since(version = 1.0.0) resource r1; From 006f17dea053e5b36d290866666608b8d5e09c0c Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Mon, 10 Jun 2024 17:58:41 -0700 Subject: [PATCH 15/15] .github/workflows: use wasm-tools v1.210.0 --- .github/workflows/test.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 7074f80f..0b6684f9 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -43,7 +43,7 @@ jobs: - name: Set up wasm-tools uses: bytecodealliance/actions/wasm-tools/setup@v1.1.0 with: - version: v1.209.1 + version: v1.210.0 - name: Set up Go uses: actions/setup-go@v5.0.1 @@ -82,7 +82,7 @@ jobs: - name: Set up wasm-tools uses: bytecodealliance/actions/wasm-tools/setup@v1.1.0 with: - version: v1.209.1 + version: v1.210.0 - name: Set up Go uses: actions/setup-go@v5.0.1 @@ -119,7 +119,7 @@ jobs: - name: Set up wasm-tools uses: bytecodealliance/actions/wasm-tools/setup@v1.1.0 with: - version: v1.209.1 + version: v1.210.0 - name: Set up Wasmtime uses: bytecodealliance/actions/wasmtime/setup@v1.1.0