Skip to content

Commit

Permalink
wit, testdata: improve own<t> and borrow<t> WIT generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ydnar committed Sep 25, 2023
1 parent 2d8027e commit 4fe67a4
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 44 deletions.
2 changes: 1 addition & 1 deletion testdata/cross-package-resource.wit.json.golden.wit
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ package foo:bar

interface foo {
use some:dep/foo.{r}
/* TODO(t) */
type t = own<r>
}
4 changes: 2 additions & 2 deletions testdata/name-both-resource-and-type.wit.json.golden.wit
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ package foo:bar
interface foo {
use some:dep/foo.{a}
type t1 = a
/* TODO(t2) */
/* TODO(t3) */
type t2 = borrow<a>
type t3 = borrow<t1>
}
4 changes: 2 additions & 2 deletions testdata/resources-empty.wit.json.golden.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package foo:resources-empty

interface resources-empty {
resource r1 {} // TODO: constructor, methods, and static functions
t1: func(a: /* TODO() */)
t2: func(a: /* TODO() */)
t1: func(a: own<r1>)
t2: func(a: borrow<r1>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package foo:resources1

interface resources1 {
resource r1 {} // TODO: constructor, methods, and static functions
[method]r1.f1: func(self: /* TODO() */) -> a: s32, handle: /* TODO() */
t1: func(a: /* TODO() */)
[method]r1.f1: func(self: borrow<r1>) -> a: s32, handle: borrow<r1>
t1: func(a: borrow<r1>)
}
4 changes: 2 additions & 2 deletions testdata/resources-multiple-returns-own.wit.json.golden.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package foo:resources1

interface resources1 {
resource r1 {} // TODO: constructor, methods, and static functions
[method]r1.f1: func(self: /* TODO() */) -> a: s32, handle: /* TODO() */
t1: func(a: /* TODO() */)
[method]r1.f1: func(self: borrow<r1>) -> a: s32, handle: own<r1>
t1: func(a: own<r1>)
}
24 changes: 12 additions & 12 deletions testdata/resources-multiple.wit.json.golden.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package foo:resources-multiple

interface resources-multiple {
resource r1 {} // TODO: constructor, methods, and static functions
[method]r1.f1: func(self: /* TODO() */)
[method]r1.f10: func(self: /* TODO() */) -> u: u32
[method]r1.f11: func(self: /* TODO() */)
[method]r1.f2: func(self: /* TODO() */, a: u32)
[method]r1.f3: func(self: /* TODO() */, a: u32)
[method]r1.f4: func(self: /* TODO() */) -> u32
[method]r1.f6: func(self: /* TODO() */) -> tuple<u32, u32>
[method]r1.f7: func(self: /* TODO() */, a: float32, b: float32) -> tuple<u32, u32>
[method]r1.f8: func(self: /* TODO() */, a: /* TODO() */) -> /* TODO() */
[method]r1.f9: func(self: /* TODO() */) -> u: u32, f: float32
t1: func(a: /* TODO() */)
t2: func(a: /* TODO() */)
[method]r1.f1: func(self: borrow<r1>)
[method]r1.f10: func(self: borrow<r1>) -> u: u32
[method]r1.f11: func(self: borrow<r1>)
[method]r1.f2: func(self: borrow<r1>, a: u32)
[method]r1.f3: func(self: borrow<r1>, a: u32)
[method]r1.f4: func(self: borrow<r1>) -> u32
[method]r1.f6: func(self: borrow<r1>) -> tuple<u32, u32>
[method]r1.f7: func(self: borrow<r1>, a: float32, b: float32) -> tuple<u32, u32>
[method]r1.f8: func(self: borrow<r1>, a: /* TODO() */) -> /* TODO() */
[method]r1.f9: func(self: borrow<r1>) -> u: u32, f: float32
t1: func(a: borrow<r1>)
t2: func(a: own<r1>)
}
4 changes: 2 additions & 2 deletions testdata/resources-return-borrow.wit.json.golden.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package foo:resources1

interface resources1 {
resource r1 {} // TODO: constructor, methods, and static functions
[method]r1.f1: func(self: /* TODO() */) -> /* TODO() */
t1: func(a: /* TODO() */)
[method]r1.f1: func(self: borrow<r1>) -> borrow<r1>
t1: func(a: borrow<r1>)
}
4 changes: 2 additions & 2 deletions testdata/resources-return-own.wit.json.golden.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package foo:resources1

interface resources1 {
resource r1 {} // TODO: constructor, methods, and static functions
[method]r1.f1: func(self: /* TODO() */) -> /* TODO() */
t1: func(a: /* TODO() */)
[method]r1.f1: func(self: borrow<r1>) -> own<r1>
t1: func(a: own<r1>)
}
18 changes: 9 additions & 9 deletions testdata/resources.wit.json.golden.wit
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ interface foo {
resource c {} // TODO: constructor, methods, and static functions
resource d {} // TODO: constructor, methods, and static functions
resource e {} // TODO: constructor, methods, and static functions
[constructor]b: func() -> /* TODO() */
[constructor]c: func(x: u32) -> /* TODO() */
[constructor]d: func(x: u32) -> /* TODO() */
[constructor]e: func(other: /* TODO() */, other2: /* TODO() */) -> /* TODO() */
[method]d.a: func(self: /* TODO() */)
[method]e.method: func(self: /* TODO() */, thing: /* TODO() */, thing2: /* TODO() */)
[constructor]b: func() -> own<b>
[constructor]c: func(x: u32) -> own<c>
[constructor]d: func(x: u32) -> own<d>
[constructor]e: func(other: own<e>, other2: borrow<e>) -> own<e>
[method]d.a: func(self: borrow<d>)
[method]e.method: func(self: borrow<e>, thing: own<e>, thing2: borrow<e>)
[static]d.b: func()
}

interface i {
resource a {} // TODO: constructor, methods, and static functions
type t1 = a
/* TODO(t2) */
/* TODO(t3) */
type t2 = borrow<a>
type t3 = borrow<t1>
}

world w {
import [constructor]c: func() -> /* TODO() */
import [constructor]c: func() -> own<c>
resource a {} // TODO: constructor, methods, and static functions
resource b {} // TODO: constructor, methods, and static functions
resource c {} // TODO: constructor, methods, and static functions
Expand Down
8 changes: 4 additions & 4 deletions testdata/resources1.wit.json.golden.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package foo:resources1

interface resources1 {
resource r1 {} // TODO: constructor, methods, and static functions
[method]r1.f1: func(self: /* TODO() */)
t1: func(a: /* TODO() */)
t2: func(a: /* TODO() */)
t3: func(a: /* TODO() */)
[method]r1.f1: func(self: borrow<r1>)
t1: func(a: borrow<r1>)
t2: func(a: own<r1>)
t3: func(a: own<r1>)
}
12 changes: 6 additions & 6 deletions testdata/world-top-level-resources.wit.json.golden.wit
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package foo:foo
interface handler {
use foo:foo/types.{request}
use foo:foo/types.{response}
handle: func(some: /* TODO() */) -> /* TODO() */
handle-owned: func(some: /* TODO() */) -> /* TODO() */
handle: func(some: borrow<request>) -> borrow<response>
handle-owned: func(some: own<request>) -> own<response>
}

interface types {
resource request {} // TODO: constructor, methods, and static functions
resource response {} // TODO: constructor, methods, and static functions
[method]request.bar: func(self: /* TODO() */, arg: list<u32>)
[method]request.foo: func(self: /* TODO() */)
[method]response.bar: func(self: /* TODO() */, arg: list<u32>)
[method]response.foo: func(self: /* TODO() */)
[method]request.bar: func(self: borrow<request>, arg: list<u32>)
[method]request.foo: func(self: borrow<request>)
[method]response.bar: func(self: borrow<response>, arg: list<u32>)
[method]response.foo: func(self: borrow<response>)
}

world proxy {
Expand Down
26 changes: 26 additions & 0 deletions wit/wit.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,32 @@ func (r *Resource) WIT(ctx Node, name string) string {
return b.String()
}

func (h *OwnedHandle) WIT(ctx Node, name string) string {
var b strings.Builder
if name != "" {
b.WriteString("type ")
b.WriteString(name)
b.WriteString(" = ")
}
b.WriteString("own<")
b.WriteString(h.Type.WIT(h, ""))
b.WriteRune('>')
return b.String()
}

func (h *BorrowedHandle) WIT(ctx Node, name string) string {
var b strings.Builder
if name != "" {
b.WriteString("type ")
b.WriteString(name)
b.WriteString(" = ")
}
b.WriteString("borrow<")
b.WriteString(h.Type.WIT(h, ""))
b.WriteRune('>')
return b.String()
}

func (t *Tuple) WIT(ctx Node, _ string) string {
var b strings.Builder
b.WriteString("tuple<")
Expand Down

0 comments on commit 4fe67a4

Please sign in to comment.