From 2b7b33df26cc1d9bbe3de22dbf1848b916b1e2b5 Mon Sep 17 00:00:00 2001 From: Luke Wagner Date: Mon, 6 Jul 2026 12:46:20 -0500 Subject: [PATCH] Add 'optional' imports/exports --- design/mvp/Binary.md | 43 +- design/mvp/CanonicalABI.md | 191 ++++++--- design/mvp/JS-Reference.md | 102 +++-- design/mvp/Text.md | 382 +++++++++++++++-- design/mvp/WIT.md | 208 +++++++-- design/mvp/canonical-abi/definitions.py | 93 ++-- design/mvp/canonical-abi/run_tests.py | 114 +++-- test/binary/binary.wast | 24 +- test/nyi.txt | 4 + test/optional/async.wast | 65 +++ test/optional/binary.wast | 56 +++ test/optional/runtime.wast | 225 ++++++++++ test/optional/validation.wast | 546 ++++++++++++++++++++++++ 13 files changed, 1815 insertions(+), 238 deletions(-) create mode 100644 test/optional/async.wast create mode 100644 test/optional/binary.wast create mode 100644 test/optional/runtime.wast create mode 100644 test/optional/validation.wast diff --git a/design/mvp/Binary.md b/design/mvp/Binary.md index c4663bc8..607476c2 100644 --- a/design/mvp/Binary.md +++ b/design/mvp/Binary.md @@ -77,6 +77,7 @@ core:inlineexport ::= n: si: => (e instance ::= ie: => (instance ie) instanceexpr ::= 0x00 c: arg*:vec() => (instantiate c arg*) | 0x01 e*:vec() => e* + | 0x02 i: e*:vec() => (type i) e* instantiatearg ::= n: si: => (with n si) name ::= n: => n sortidx ::= sort: idx: => (sort idx) @@ -102,13 +103,23 @@ Notes: sort, but would be extended to accept other sorts as core wasm is extended. * Validation of `instantiate` requires each `` in `c` to match a `name` in a `with` argument (using plain string equality) and for the types to - match. + match unless the ``'s type is `optional`, in which case no `with` + argument need be given. * When validating `instantiate`, after each individual type-import is supplied via `with`, the actual type supplied is immediately substituted for all uses of the import, so that subsequent imports and all exports are now specialized to the actual type. * The indices in `sortidx` are validated according to their `sort`'s index spaces, which are built incrementally as each definition is validated. +* Validation of an `instanceexpr` with an ascribed `typeidx` requires that the + `typeidx` refer to an `instance` type and the given `inlineexport*` match the + `export`s of that `instance` type with the additional allowances that: + `optional` definitions can be supplied for non-`optional` `export`s of an + `optional` instance; and the `optional` `export`s in an `instance` type can be + omitted (see [optionality]). The resulting `instanceidx`'s type is then + exactly the ascribed type, with superfluous `inlineexport`s ignored. +* If a `typeidx` is not given to the `instanceexpr`, the resulting + `instanceidx`'s type is inferred from the `componentidx` or `inlineexport*`. ## Alias Definitions @@ -221,13 +232,21 @@ labellit ::= len: l: