Skip to content

Commit 2bfcea2

Browse files
committed
Add 'optional' imports/exports (WIP)
1 parent 9ff90fd commit 2bfcea2

7 files changed

Lines changed: 736 additions & 174 deletions

File tree

‎design/mvp/Binary.md‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ core:inlineexport ::= n:<core:name> si:<core:sortidx> => (e
7777
instance ::= ie:<instanceexpr> => (instance ie)
7878
instanceexpr ::= 0x00 c:<componentidx> arg*:vec(<instantiatearg>) => (instantiate c arg*)
7979
| 0x01 e*:vec(<inlineexport>) => e*
80+
| 0x02 i:<typeidx> e*:vec(<inlineexport>) => (type i) e*
8081
instantiatearg ::= n:<name> si:<sortidx> => (with n si)
8182
name ::= n:<core:name> => n
8283
sortidx ::= sort:<sort> idx:<u32> => (sort idx)
@@ -102,13 +103,22 @@ Notes:
102103
sort, but would be extended to accept other sorts as core wasm is extended.
103104
* Validation of `instantiate` requires each `<externname>` in `c` to match a
104105
`name` in a `with` argument (using plain string equality) and for the types to
105-
match.
106+
match unless the `<externname>`'s type is `optional`, in which case no `with`
107+
argument need be given.
106108
* When validating `instantiate`, after each individual type-import is supplied
107109
via `with`, the actual type supplied is immediately substituted for all uses
108110
of the import, so that subsequent imports and all exports are now specialized
109111
to the actual type.
110112
* The indices in `sortidx` are validated according to their `sort`'s index
111113
spaces, which are built incrementally as each definition is validated.
114+
* Validation specifies that, if a `typeidx` is given to the `instanceexpr`, the
115+
`typeidx` must be an `instance` type, the given `inlineexport*` set must
116+
match that type and the resulting `instanceidx`'s type is `typeidx`, with the
117+
additional allowance that `optional` definitions can be supplied for
118+
non-`optional` `export`s of an `optional` instance (as described in the
119+
[optionality](Text.md#-optionality) section).
120+
* Otherwise, if a `typeidx` is not given to the `instanceexpr`, the resulting
121+
`instanceidx`'s type is inferred from the `componentidx` or `inlineexport*`.
112122

113123

114124
## Alias Definitions
@@ -221,13 +231,21 @@ labellit ::= len:<u32> l:<label> => "l" (if len = |l|
221231
valtype ::= i:<typeidx> => i
222232
| pvt:<primvaltype> => pvt
223233
resourcetype ::= 0x3f t:<core:valtype> f?:<core:funcidx>? => (resource (rep t) (dtor f)?)
224-
functype ::= 0x40 ps:<paramlist> rs:<resultlist> => (func ps rs)
225-
| 0x43 ps:<paramlist> rs:<resultlist> => (func async ps rs)
226-
paramlist ::= lt*:vec(<labelvaltype>) => (param lt)*
227-
resultlist ::= 0x00 t:<valtype> => (result t)
234+
functype ::= 0x40 pr:<paramsresult> => (func pr)
235+
| 0x43 pr:<paramsresult> => (func async pr) 🔀
236+
| 0x44 i*:<funcimms> pr:<paramsresult> => (func i* pr) ❓
237+
paramsresult ::= p*:vec(<labelvaltype>) r:<result> => (param p)* r
238+
result ::= 0x00 t:<valtype> => (result t)
228239
| 0x01 0x00 =>
240+
funcimms ::= 0x00 =>
241+
| 0x01 => optional ❓
242+
| 0x02 => async 🔀
243+
| 0x03 => optional async ❓🔀
229244
componenttype ::= 0x41 cd*:vec(<componentdecl>) => (component cd*)
230245
instancetype ::= 0x42 id*:vec(<instancedecl>) => (instance id*)
246+
| 0x45 o?:<opt?> id*:vec(<instancedecl>) => (instance o? id*) ❓
247+
opt? ::= 0x00 =>
248+
| 0x01 => optional ❓
231249
componentdecl ::= 0x03 id:<importdecl> => id
232250
| id:<instancedecl> => id
233251
instancedecl ::= 0x00 t:<core:type> => t
@@ -244,8 +262,9 @@ externtype ::= 0x00 0x11 i:<core:typeidx> => (core module (type
244262
| 0x05 i:<typeidx> => (instance (type i))
245263
typebound ::= 0x00 i:<typeidx> => (eq i)
246264
| 0x01 => (sub resource)
265+
| 0x02 o?:<opt?> => (sub o? resource) ❓
247266
valuebound ::= 0x00 i:<valueidx> => (eq i) 🪙
248-
| 0x01 t:<valtype> => t 🪙
267+
| 0x01 o?:<opt?> t:<valtype> => o? t 🪙❓
249268
```
250269
Notes:
251270
* The type opcodes follow the same negative-SLEB128 scheme as Core WebAssembly,
@@ -307,9 +326,10 @@ canon ::= 0x00 0x00 f:<core:funcidx> opts:<opts> ft:<typeidx> => (canon lift
307326
| 0x02 rt:<typeidx> => (canon resource.new rt (core func))
308327
| 0x03 rt:<typeidx> => (canon resource.drop rt (core func))
309328
| 0x04 rt:<typeidx> => (canon resource.rep rt (core func))
329+
| 0x07 si:<sortidx> => (canon optional.present si (core global)) ❓
310330
| 0x24 => (canon backpressure.inc (core func)) 🔀
311331
| 0x25 => (canon backpressure.dec (core func)) 🔀
312-
| 0x09 rs:<resultlist> opts:<opts> => (canon task.return rs opts (core func)) 🔀
332+
| 0x09 r:<result> opts:<opts> => (canon task.return r opts (core func)) 🔀
313333
| 0x05 => (canon task.cancel (core func)) 🔀
314334
| 0x0a t:<core:valtype> i:<u32> => (canon context.get t i (core func)) 🔀
315335
| 0x0b t:<core:valtype> i:<u32> => (canon context.set t i (core func)) 🔀
@@ -435,7 +455,8 @@ Notes:
435455
* Validation requires that, for `plainname`s annotated with `[constructor]`,
436456
`[method]`, or `[static]`, the first `label` matches the `plainname` of a
437457
preceding `resource` import or export, respectively, in the same scope
438-
(component, component type or instance type).
458+
(component, component type or instance type) and that, if the `resource`
459+
type is `optional`, so is the `func` type.
439460
* 🏷️ Validation requires that `implements`-annotated imports or exports are
440461
`instance`-typed and have a `plainname` name.
441462
* 🏷️/🔗 Validation requires that a `vec(<attribute>)` contains each kind of
@@ -571,6 +592,7 @@ named once.
571592
* The opcodes (for types, canon built-ins, etc) should be re-sorted
572593
* The two `list` type codes should be merged into one with an optional immediate
573594
and similarly for `func`.
595+
* The redundant opcodes introduced for `optional` should be merged.
574596
* The redundant `0x00` and `0x01` opcodes of `nameattributes` will be merged.
575597
* The vestigial `0x00` in `thread.*` and `waitable-set.*` built-ins may be
576598
removed.

0 commit comments

Comments
 (0)