Skip to content

Commit 08c60a4

Browse files
authored
Merge pull request #206 from WebAssembly/add-resources-to-wit
Add Wit.md section for resource items
2 parents 1bd1266 + 10346b5 commit 08c60a4

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

design/mvp/WIT.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,69 @@ union-cases ::= ty
10021002
| ty ',' union-cases?
10031003
```
10041004

1005+
### Item: `resource`
1006+
1007+
A `resource` statement defines a new abstract type for a *resource*, which is
1008+
an entity with a lifetime that can only be passed around indirectly via [handle
1009+
values](#handles). Resource types are used in interfaces to describe things
1010+
that can't or shouldn't be copied by value.
1011+
1012+
For example, the following Wit defines a resource type and a function that
1013+
takes and returns a handle to a `blob`:
1014+
```wit
1015+
resource blob
1016+
transform: func(blob) -> blob
1017+
```
1018+
1019+
As syntactic sugar, resource statements can also declare any number of
1020+
*methods*, which are functions that implicitly take a `self` parameter that is
1021+
a handle. A resource statement can also contain any number of *static
1022+
functions*, which do not have an implicit `self` parameter but are meant to be
1023+
lexically nested in the scope of the resource type. Lastly, a resource
1024+
statement can contain at most one *constructor* function, which is syntactic
1025+
sugar for a function returning a handle of the containing resource type.
1026+
1027+
For example, the following resource definition:
1028+
```wit
1029+
resource blob {
1030+
constructor(init: list<u8>)
1031+
write: func(bytes: list<u8>)
1032+
read: func(n: u32) -> list<u8>
1033+
merge: static func(lhs: borrow<blob>, rhs: borrow<blob>) -> blob
1034+
}
1035+
```
1036+
desugars into:
1037+
```wit
1038+
resource blob
1039+
%[constructor]blob: func(self: borrow<blob>, bytes: list<u8>) -> blob
1040+
%[method]blob.write: func(self: borrow<blob>, bytes: list<u8>)
1041+
%[method]blob.read: func(self: borrow<blob>, n: u32) -> list<u8>
1042+
%[static]blob.merge: func(lhs: borrow<blob>, rhs: borrow<blob>) -> blob
1043+
```
1044+
These `%`-prefixed [`name`s](Explainer.md) embed the resource type name so that
1045+
bindings generators can generate idiomatic syntax for the target language or
1046+
(for languages like C) fall back to an appropriately-prefixed free function
1047+
name.
1048+
1049+
When a resource type name is used directly (e.g. when `blob` is used as the
1050+
return value of the constructor above), it stands for an "owning" handle
1051+
that will call the resource's destructor when dropped. When a resource
1052+
type name is wrapped with `borrow<...>`, it stands for a "borrowed" handle
1053+
that will *not* call the destructor when dropped. As shown above, methods
1054+
always desugar to a borrowed self parameter whereas constructors always
1055+
desugar to an owned return value.
1056+
1057+
Specifically, the syntax for a `resource` definition is:
1058+
```ebnf
1059+
resource-item ::= 'resource' id resource-methods?
1060+
resource-methods ::= '{' resource-method* '}'
1061+
resource-method ::= func-item
1062+
| id ':' 'static' func-type
1063+
| 'constructor' param-list
1064+
```
1065+
1066+
The syntax for handle types is presented [below](#handles).
1067+
10051068
## Types
10061069

10071070
As mentioned previously the intention of `wit` is to allow defining types

0 commit comments

Comments
 (0)