Skip to content

Commit 65dd40b

Browse files
committed
Clarify validation rules of canonical options
* Refactor the spec a bit to have a dedicated section for general-purpose validation of canonical options with additional rules per-use-site. * Each option is present at most once in a list of options. * Validation is required if an option is present, for example `memory` must always be a subtype of `(memory 1)` even if the lifting/lowering doesn't require a `memory`. * At most one string encoding can be specified. * The `callback` option is specified what type the function must have. * The `callback` option requires the `async` option. * The `async` option is explicitly disallowed on `error-context.*` builtins. * The `error-context.*` builtins require `memory` and `realloc` as appropriate. * The requirement of `memory` and `realloc` on `{stream,future}.{read,write}` is documented (although "required by" is currently a bit vague, that's left to a future refactoring).
1 parent 6f1fb38 commit 65dd40b

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

design/mvp/CanonicalABI.md

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ being specified here.
3333
* [Canonical definitions](#canonical-definitions)
3434
* [`canon lift`](#canon-lift)
3535
* [`canon lower`](#canon-lower)
36+
* [`canon $opts`](#canon-opts)
3637
* [`canon resource.new`](#canon-resourcenew)
3738
* [`canon resource.drop`](#canon-resourcedrop)
3839
* [`canon resource.rep`](#canon-resourcerep)
@@ -2727,12 +2728,14 @@ For a canonical definition:
27272728
```wat
27282729
(canon lift $callee:<funcidx> $opts:<canonopt>* (func $f (type $ft)))
27292730
```
2730-
validation specifies:
2731+
2732+
In addition to [general validation of `$opts`](#canon-opts) the additional
2733+
validation is performed:
2734+
27312735
* `$callee` must have type `flatten_functype($opts, $ft, 'lift')`
27322736
* `$f` is given type `$ft`
2733-
* a `memory` is present if required by lifting and is a subtype of `(memory 1)`
2734-
* a `realloc` is present if required by lifting and has type `(func (param i32 i32 i32 i32) (result i32))`
2735-
* if `async` is set, a `post-return` function may not be set
2737+
* a `memory` is present if required by lifting
2738+
* a `realloc` is present if required by lifting
27362739
* if a `post-return` is present, it has type `(func (param flatten_functype({}, $ft, 'lift').results))`
27372740

27382741
When instantiating component instance `$inst`:
@@ -2890,11 +2893,13 @@ For a canonical definition:
28902893
```wat
28912894
(canon lower $callee:<funcidx> $opts:<canonopt>* (core func $f))
28922895
```
2893-
where `$callee` has type `$ft`, validation specifies:
2896+
2897+
In addition to [general validation of `$opts`](#canon-opts) the additional
2898+
validation is performed where `$callee` has type `$ft`:
2899+
28942900
* `$f` is given type `flatten_functype($opts, $ft, 'lower')`
2895-
* a `memory` is present if required by lifting and is a subtype of `(memory 1)`
2896-
* a `realloc` is present if required by lifting and has type `(func (param i32 i32 i32 i32) (result i32))`
2897-
* there is no `post-return` in `$opts`
2901+
* a `memory` is present if required by lowering
2902+
* a `realloc` is present if required by lowering
28982903
* if `contains_async_value($ft)`, then `$opts.async` must be set
28992904

29002905
When instantiating component instance `$inst`:
@@ -3034,6 +3039,26 @@ elimination of string operations on the labels of records and variants) as well
30343039
as post-MVP [adapter functions].
30353040

30363041

3042+
### `canon $opts`
3043+
3044+
Canonical options, specified here as `$opts` in a number of locations
3045+
throughout this document, can be specified at most once per `$opts`. For example
3046+
specifying `string-encoding=utf8` twice is an error. Each individual option, if
3047+
present, is validated as such:
3048+
3049+
* `string-encoding=utf8` - cannot be combined with `utf16` or `latin1+utf16`
3050+
* `string-encoding=utf16` - cannot be combined with `utf8` or `latin1+utf16`
3051+
* `string-encoding=latin1+utf16` - cannot be combined with `utf8` or `utf16`
3052+
* `memory` - this is a subtype of `(memory 1)`
3053+
* `realloc` - the function has type `(func (param i32 i32 i32 i32) (result i32))`
3054+
* `post-return` - only allowed on [`canon lift`](#canon-lift), which has rules
3055+
for validation
3056+
* 🔀 `async` - cannot be present with `post-return`
3057+
* 🔀 `callback` - the function has type `(func (param i32 i32 i32 i32) (result
3058+
i32))` and cannot be present without `async` and is only allowed with [`canon
3059+
lift`](#canon-lift)
3060+
3061+
30373062
### `canon resource.new`
30383063

30393064
For a canonical definition:
@@ -3205,7 +3230,10 @@ For a canonical definition:
32053230
```wat
32063231
(canon task.return (result $t)? $opts (core func $f))
32073232
```
3208-
validation specifies:
3233+
3234+
In addition to [general validation of `$opts`](#canon-opts) validation
3235+
specifies:
3236+
32093237
* `$f` is given type `flatten_functype($opts, (func (param $t)?), 'lower')`
32103238
* `$opts` may only contain `memory`, `string-encoding` and `realloc`
32113239

@@ -3461,8 +3489,12 @@ For canonical definitions:
34613489
(canon stream.read $t $opts (core func $f))
34623490
(canon stream.write $t $opts (core func $f))
34633491
```
3464-
validation specifies:
3492+
In addition to [general validation of `$opts`](#canon-opts) validation
3493+
specifies:
34653494
* `$f` is given type `(func (param i32 i32 i32) (result i32))`
3495+
* `memory` is required for `stream.write` if required by lowering
3496+
* `memory` is required for `stream.read` if required by lifting
3497+
* `realloc` is required for `stream.read` if required by lifting
34663498

34673499
For canonical definitions:
34683500
```wat
@@ -3471,6 +3503,9 @@ For canonical definitions:
34713503
```
34723504
validation specifies:
34733505
* `$f` is given type `(func (param i32 i32) (result i32))`
3506+
* `memory` is required for `future.write` if required by lowering
3507+
* `memory` is required for `future.read` if required by lifting
3508+
* `realloc` is required for `future.read` if required by lifting
34743509

34753510
The implementation of these four built-ins all funnel down to a single
34763511
parameterized `copy` function:
@@ -3704,6 +3739,8 @@ For a canonical definition:
37043739
```
37053740
validation specifies:
37063741
* `$f` is given type `(func (param i32 i32) (result i32))`
3742+
* `async` is not present
3743+
* `memory` must be present
37073744

37083745
Calling `$f` calls the following function which uses the `$opts` immediate to
37093746
(non-deterministically) lift the debug message, create a new `ErrorContext`
@@ -3743,6 +3780,9 @@ For a canonical definition:
37433780
```
37443781
validation specifies:
37453782
* `$f` is given type `(func (param i32 i32))`
3783+
* `async` is not present
3784+
* `memory` must be present
3785+
* `realloc` must be present
37463786

37473787
Calling `$f` calls the following function which uses the `$opts` immediate to
37483788
lowers the `ErrorContext`'s debug message. While *producing* an `error-context`

0 commit comments

Comments
 (0)