Skip to content

Commit f76b64a

Browse files
authored
Merge pull request #96 from WebAssembly/add-webidl-variant-note
Add JS API notes about variants
2 parents 7ea933a + 05bc3d6 commit f76b64a

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

design/mvp/Explainer.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ At a high level, the additional coercions would be:
968968
| `float32`, `float64` | as a Number, mapping the canonical NaN to [JS NaN] | `ToNumber` mapping [JS NaN] to the canonical NaN |
969969
| `char` | same as [`USVString`] | same as [`USVString`], throw if the USV length is not 1 |
970970
| `record` | TBD: maybe a [JS Record]? | same as [`dictionary`] |
971-
| `variant` | TBD | TBD |
971+
| `variant` | see below | see below |
972972
| `list` | create a typed array copy for number types; otherwise produce a JS array (like [`sequence`]) | same as [`sequence`] |
973973
| `string` | same as [`USVString`] | same as [`USVString`] |
974974
| `tuple` | TBD: maybe a [JS Tuple]? | TBD |
@@ -986,6 +986,15 @@ Notes:
986986
the return value is specified by `ToJSValue` above. Otherwise, the function
987987
result is wrapped into a JS object whose field names are taken from the result
988988
names and whose field values are specified by `ToJSValue` above.
989+
* In lieu of an existing standard JS representation for `variant`, the JS API
990+
would need to define its own custom binding built from objects. As a sketch,
991+
the JS values accepted by `(variant (case "a" u32) (case "b" string))` could
992+
include `{ tag: 'a', value: 42 }` and `{ tag: 'b', value: "hi" }`.
993+
* For `union` and `option`, when Web IDL doesn't support particular type
994+
combinations (e.g., `(option (option u32))`), the JS API would fall back to
995+
the JS API of the unspecialized `variant` (e.g.,
996+
`(variant (case "some" (option u32)) (case "none"))`, despecializing only
997+
the problematic outer `option`).
989998
* The forthcoming addition of [resource and handle types] would additionally
990999
allow coercion to and from the remaining Symbol and Object JavaScript value
9911000
types.

0 commit comments

Comments
 (0)