Debian Codegraph @

crates/wasm-wave/README.md

raw
1 # WAVE: Web Assembly Value Encoding
2
3 WAVE is a human-oriented text encoding of WebAssembly Component Model values. It is designed to be consistent with the
4 [WIT IDL format](https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md).
5
6 |Type|Example Values
7 |---|---
8 |Bools|`true`, `false`
9 |Integers|`123`, `-9`
10 |Floats|`3.14`, `6.022e+23`, `nan`, `-inf`
11 |Chars|`'x'`, `'โ˜ƒ๏ธŽ'`, `'\''`, `'\u{0}'`
12 |Strings|`"abc\t123"`
13 |Tuples|`("abc", 123)`
14 |Lists|`[1, 2, 3]`
15 |Records|`{field-a: 1, field-b: "two"}`
16 |Variants|`days(30)`, `forever`
17 |Enums|`south`, `west`
18 |Options|`"flat some"`, `some("explicit some")`, `none`
19 |Results|`"flat ok"`, `ok("explicit ok")`, `err("oops")`
20 |Flags|`{read, write}`, `{}`
21
22 ## Usage
23
24 ```rust
25 use wasmtime::component::{Type, Val};
26
27 let val: Val = wasm_wave::from_str(&Type::String, "\"๐Ÿ‘‹ Hello, world! ๐Ÿ‘‹\"").unwrap();
28 println!("{}", wasm_wave::to_string(&val).unwrap());
29 ```
30
31 โ†’ `"๐Ÿ‘‹ Hello, world! ๐Ÿ‘‹"`
32
33 ## Encoding
34
35 Values are encoded as Unicode text. UTF-8 should be used wherever an interoperable binary encoding is required.
36
37 ### Whitespace
38
39 Whitespace is _insignificant between tokens_ and _significant within tokens_: keywords, labels, chars, and strings.
40
41 ### Comments
42
43 Comments start with `//` and run to the end of the line.
44
45 ### Keywords
46
47 Several tokens are reserved WAVE keywords: `true`, `false`, `inf`, `nan`, `some`, `none`, `ok`, `err`. Variant or enum cases that match one of these keywords must be prefixed with `%`.
48
49 ### Labels
50
51 Kebab-case labels are used for record fields, variant cases, enum cases, and flags. Labels use ASCII alphanumeric characters and hyphens, following the [Wit identifier syntax](https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md#identifiers):
52
53 - Labels consist of one or more hypen-separated words.
54 - `one`, `two-words`
55 - Words consist of one ASCII letter followed by any number of ASCII alphanumeric characters.
56 - `q`, `abc123`
57 - Each word can contain lowercase or uppercase characters but not both; each word in a label can use a different (single) case.
58 - `HTTP3`, `method-GET`
59 - Any label may be prefixed with `%`; this is not part of the label itself but allows for representing labels that would otherwise be parsed as keywords.
60 - `%err`, `%non-keyword`
61
62 ### Bools
63
64 Bools are encoded as one of the keywords `false` or `true`.
65
66 ### Integers
67
68 Integers are encoded as base-10 numbers.
69
70 > TBD: hex/bin repr? e.g. `0xab`, `0b1011`
71
72 ### Floats
73
74 Floats are encoded as JSON numbers or one of the keywords `nan`, (not a number) `inf` (infinity), or `-inf` (negative infinity).
75
76 ### Chars
77
78 Chars are encoded as `'<char>'`, where `<char>` is one of:
79
80 - a single [Unicode Scalar Value](https://unicode.org/glossary/#unicode_scalar_value)
81 - one of these escapes:
82 - `\'` โ†’ `'`
83 - `\"` โ†’ `"`
84 - `\\` โ†’ `\`
85 - `\t` โ†’ U+9 (HT)
86 - `\n` โ†’ U+A (LF)
87 - `\r` โ†’ U+D (CR)
88 - `\u{ยทยทยท}` โ†’ U+ยทยทยท (where `ยทยทยท` is a hexadecimal Unicode Scalar Value)
89
90 Escaping newline (`\n`), `\`, and `'` is mandatory for chars.
91
92 ### Strings
93
94 Strings are encoded as a double-quote-delimited sequence of `<char>`s (as for [Chars](#chars)).
95
96 Escaping newline (`\n`), `\`, and `"` is mandatory for strings.
97
98 ### Multiline Strings
99
100 A multiline string begins with `"""` followed immediately by a line break (`\n` or `\r\n`) and ends with a line break, zero or more spaces, and `"""`. The number of spaces immediately preceding the ending `"""` determines the indent level of the entire multiline string. Every other line break in the string must be followed by at least this many spaces which are then omitted ("dedented") from the decoded string.
101
102 Each line break in the encoded string except for the first and last is decoded as a newline character (`\n`).
103
104 Escaping `\` is mandatory for multiline strings. Escaping carriage return (`\r`) is mandatory immediately before a literal newline character (`\n`) if it is to be retained. Escaping `"` is mandatory where necessary to break up any sequence of `"""` within a string, even if the first `"` is escaped (i.e. `\"""` is prohibited).
105
106 ```python
107 """
108 A single line
109 """
110 ```
111 โ†’ `"A single line"`
112
113 ```python
114 """
115 Indentation determined
116 by ending delimiter
117 """
118 ```
119 โ†’
120 ```clike
121 " Indentation determined\n by ending delimiter"
122 ```
123
124 ```python
125 """
126 Must escape carriage return at end of line: \r
127 Must break up double quote triplets: ""\""
128 """
129 ```
130 โ†’
131 ```clike
132 "Must escape carriage return at end of line: \r\nMust break up double quote triplets: \"\"\"\""
133 ```
134
135 ### Tuples
136
137 Tuples are encoded as a parenthesized sequence of comma-separated values. Trailing commas are permitted.
138
139 `tuple<u8, string>` โ†’ `(123, "abc")`
140
141 ### Lists
142
143 Lists are encoded as a square-braketed sequence of comma-separated values. Trailing commas are permitted.
144
145 `list<char>` โ†’ `[]`, `['a', 'b', 'c']`
146
147 ### Records
148
149 Records are encoded as curly-braced set of comma-separated record entries. Trailing commas are permitted. Each record entry consists of a field label, a colon, and a value. Fields may be present in any order. Record entries with the `option`-typed value `none` may be omitted; if all of a record's fields are omitted in this way the "empty" record must be encoded as `{:}` (to disambiguate from an empty `flags` value).
150
151 ```clike
152 record example {
153 must-have: u8,
154 optional: option<u8>,
155 }
156 ```
157
158 โ†’ `{must-have: 123}` = `{must-have: 123, optional: none,}`
159
160 ```clike
161 record all-optional {
162 optional: option<u8>,
163 }
164 ```
165
166 โ†’ `{:}` = `{optional: none}`
167
168 > Note: Field labels _may_ be prefixed with `%` but this is never required.
169
170 ### Variants
171
172 Variants are encoded as a case label. If the case has a payload, the label is followed by the parenthesized case payload value.
173
174 If a variant case matches a WAVE keyword it must be prefixed with `%`.
175
176 ```clike
177 variant response {
178 empty,
179 body(list<u8>),
180 err(string),
181 }
182 ```
183
184 โ†’ `empty`, `body([79, 75])`, `%err("oops")`
185
186 ### Enums
187
188 Enums are encoded as a case label.
189
190 If an enum case matches a WAVE keyword it must be prefixed with `%`.
191
192 `enum status { ok, not-found }` โ†’ `%ok`, `not-found`
193
194 ### Options
195
196 Options may be encoded in their variant form (e.g. `some(...)` or `none`). A `some` value may also be encoded as the "flat" payload value itself, but only if the payload is not an option or result type.
197
198 - `option<u8>` โ†’ `123` = `some(123)`
199
200 ### Results
201
202 Results may be encoded in their variant form (e.g. `ok(...)`, `err("oops")`). An `ok` value may also be encoded as the "flat" payload value itself, but only if it has a payload which is not an option or result type.
203
204 - `result<u8>` โ†’ `123` = `ok(123)`
205 - `result<_, string>` โ†’ `ok`, `err("oops")`
206 - `result` โ†’ `ok`, `err`
207
208 ### Flags
209
210 Flags are encoded as a curly-braced set of comma-separated flag labels in any order. Trailing commas are permitted.
211
212 `flags perms { read, write, exec }` โ†’ `{write, read,}`
213
214 > Note: Flags _may_ be prefixed with `%` but this is never required.
215
216 > TBD: Allow record form? `{read: true, write: true, exec: false}`
217
218 ### Resources
219
220 > TBD (`<named-type>(<idx>)`?)
221
222 ## Appendix: Function calls
223
224 Some applications may benefit from a standard way to encode function calls and/or results, described here.
225
226 Function calls can be encoded as some application-dependent function identifier (such as a kebab-case label) followed by parenthesized function arguments.
227
228 If function results need to be encoded along with the call, they can be separated from the call by `->`.
229
230 ```clike
231 my-func("param")
232
233 with-result() -> ok("result")
234 ```
235
236 ### Function arguments
237
238 Arguments are encoded as a sequence of comma-separated values.
239
240 Any number of `option` `none` values at the end of the sequence may be omitted.
241
242 ```clike
243 // f: func(a: option<u8>, b: option<u8>, c: option<u8>)
244 // all equivalent:
245 f(some(1))
246 f(some(1), none)
247 f(some(1), none, none)
248 ```
249
250 > TBD: Named-parameter encoding? e.g.
251 > `my-func(named-param: 1)`
252 > Could allow omitting "middle" `none` params.
253
254 ### Function results
255
256 Results are encoded in one of several ways depending on the number of result values:
257
258 - Any number of result values may be encoded as a parenthesized sequence of comma-separated result entries. Each result entry consists of a label (for named results) or zero-based index number, a colon, and the result value. Result entry ordering must match the function definition.
259 - Zero result values are encoded as `()` or omitted entirely.
260 - A single result value may be encoded as the "flat" result value itself.
261
262 ```clike
263 -> ()
264
265 -> some("single result")
266 // or
267 -> (0: some("single result"))
268
269 -> (result-a: "abc", result-b: 123)
270 ```
271
272 ---
273
274 :ocean: