Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 47 additions & 42 deletions design/mvp/Binary.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ defvaltype ::= pvt:<primvaltype> => pvt
| 0x66 t?:<valtype>? => (stream t?) 🔀
| 0x65 t?:<valtype>? => (future t?) 🔀
| 0x63 k:<valtype> v:<valtype> => (map k v) (if k is in <keytype>) 🗺️
| 0x62 lt*:vec(<labelvaltype>) => (union (case lt)+) (if |lt*| > 0) 💍
labelvaltype ::= l:<labellit> t:<valtype> => l t
case ::= l:<labellit> t?:<valtype>? 0x00 => (case l t?)
labellit ::= len:<u32> l:<label> => "l" (if len = |l|)
Expand Down Expand Up @@ -284,15 +285,18 @@ Notes:
* `<label>` is defined as part of the
[text format](Text.md#import-and-export-definitions).
* All parameter labels, result labels, record field labels, variant case
labels, flag labels, enum case labels, component import names, component
export names, instance import names and instance export names must be
[strongly-unique] in their containing scope.
labels, union case labels, flag labels, enum case labels, component import
names, component export names, instance import names and instance export
names must be [strongly-unique] in their containing scope.
* Validation of `externtype` requires the various `typeidx` type constructors
to match the preceding `sort`.
* (The `0x00` immediate of `case` may be reinterpreted in the future as the
`none` case of an optional immediate.)
* 🔧 for fixed-sized lists the length of the list must be larger than 0 to pass
validation.
* 💍 Validation of `union` requires the case payloads to be pairwise
[distinguishable](Text.md#-union-types), and forbids any case from having a
`union`, `option`, `result`, `stream`, `future`, or `error-context` type.
* Validation requires that, for every `defvaltype` `t`, the worst-case in-memory
element size is less than 2<sup>28</sup>, as defined by `elem_size` in the
[Canonical ABI](CanonicalABI.md#element-size).
Expand Down Expand Up @@ -433,8 +437,8 @@ Notes:
`[method]`, `[static]`, 📡 `[get]`, or 📡 `[set]` only occur on `func` imports
or exports.
* Validation requires that, for `plainname`s annotated with `[constructor]`,
`[method]`, or `[static]`, the first `label` matches the `plainname` of a
preceding `resource` import or export, respectively, in the same scope
`[method]`, or `[static]`, the resource name `label` matches the `plainname`
of a preceding `resource` import or export, respectively, in the same scope
(component, component type or instance type).
* 🏷️ Validation requires that `implements`-annotated imports or exports are
`instance`-typed and have a `plainname` name.
Expand Down Expand Up @@ -481,43 +485,44 @@ Notes:
(See [Value Definitions](Text.md#-value-definitions) in the explainer.)

```ebnf
value ::= t:<valtype> len:<core:u32> v:<val(t)> => (value t v) (where len = ||v||)
val(bool) ::= 0x00 => false
| 0x01 => true
val(u8) ::= v:<core:byte> => v
val(s8) ::= v:<core:byte> => v' (where v' = v if v < 128 else (v - 256))
val(s16) ::= v:<core:s16> => v
val(u16) ::= v:<core:u16> => v
val(s32) ::= v:<core:s32> => v
val(u32) ::= v:<core:u32> => v
val(s64) ::= v:<core:s64> => v
val(u64) ::= v:<core:u64> => v
val(f32) ::= v:<core:f32> => v (if !isnan(v))
| 0x00 0x00 0xC0 0x7F => nan
val(f64) ::= v:<core:f64> => v (if !isnan(v))
| 0x00 0x00 0x00 0x00 0x00 0x00 0xF8 0x7F => nan
val(char) ::= b*:<core:byte>* => c (where b* = core:utf8(c))
val(string) ::= v:<core:name> => v
val(i:<typeidx>) ::= v:<val(type-index-space[i])> => v
val((record (field l t)+)) ::= v+:<val(t)>+ => (record v+)
val((variant (case l t?)+) ::= i:<core:u32> v?:<val(t[i])>? => (variant l[i] v?)
val((list t)) ::= v:vec(<val(t)>) => (list v)
val((list t len)) ::= (v:<val(t)>)^len => (list v^len) 🔧
val((tuple t+)) ::= v+:<val(t)>+ => (tuple v+)
val((flags l+)) ::= (v:<core:byte>)^N => (flags (l[i] for i in 0..|l+|-1 if v[floor(i / 8)] & 2^(i mod 8) > 0)) (where N = ceil(|l+| / 8))
val((enum l+)) ::= i:<core:u32> => (enum l[i])
val((option t)) ::= 0x00 => none
| 0x01 v:<val(t)> => (some v)
val((result)) ::= 0x00 => ok
| 0x01 => error
val((result t)) ::= 0x00 v:<val(t)> => (ok v)
| 0x01 => error
val((result (error u))) ::= 0x00 => ok
| 0x01 v:<val(u)> => (error v)
val((result t (error u))) ::= 0x00 v:<val(t)> => (ok v)
| 0x01 v:<val(u)> => (error v)
val((map k v)) ::= e*:vec(<entryval(k, v)>) => (map e*) 🗺️
entryval(k, v) ::= key:<val(k)> val:<val(v)> => (entry key val) 🗺️
value ::= t:<valtype> len:<core:u32> v:<val(t)> => (value t v) (where len = ||v||)
val(bool) ::= 0x00 => false
| 0x01 => true
val(u8) ::= v:<core:byte> => v
val(s8) ::= v:<core:byte> => v' (where v' = v if v < 128 else (v - 256))
val(s16) ::= v:<core:s16> => v
val(u16) ::= v:<core:u16> => v
val(s32) ::= v:<core:s32> => v
val(u32) ::= v:<core:u32> => v
val(s64) ::= v:<core:s64> => v
val(u64) ::= v:<core:u64> => v
val(f32) ::= v:<core:f32> => v (if !isnan(v))
| 0x00 0x00 0xC0 0x7F => nan
val(f64) ::= v:<core:f64> => v (if !isnan(v))
| 0x00 0x00 0x00 0x00 0x00 0x00 0xF8 0x7F => nan
val(char) ::= b*:<core:byte>* => c (where b* = core:utf8(c))
val(string) ::= v:<core:name> => v
val(i:<typeidx>) ::= v:<val(type-index-space[i])> => v
val((record (field l t)+)) ::= v+:<val(t)>+ => (record v+)
val((variant (case l t?)+)) ::= i:<core:u32> v?:<val(t[i])>? => (variant l[i] v?)
val((union (case l t)+)) ::= i:<core:u32> v:<val(t[i])> => (union l[i] v) 💍
val((list t)) ::= v:vec(<val(t)>) => (list v)
val((list t len)) ::= (v:<val(t)>)^len => (list v^len) 🔧
val((tuple t+)) ::= v+:<val(t)>+ => (tuple v+)
val((flags l+)) ::= (v:<core:byte>)^N => (flags (l[i] for i in 0..|l+|-1 if v[floor(i / 8)] & 2^(i mod 8) > 0)) (where N = ceil(|l+| / 8))
val((enum l+)) ::= i:<core:u32> => (enum l[i])
val((option t)) ::= 0x00 => none
| 0x01 v:<val(t)> => (some v)
val((result)) ::= 0x00 => ok
| 0x01 => error
val((result t)) ::= 0x00 v:<val(t)> => (ok v)
| 0x01 => error
val((result (error u))) ::= 0x00 => ok
| 0x01 v:<val(u)> => (error v)
val((result t (error u))) ::= 0x00 v:<val(t)> => (ok v)
| 0x01 v:<val(u)> => (error v)
val((map k v)) ::= e*:vec(<entryval(k, v)>) => (map e*) 🗺️
entryval(k, v) ::= key:<val(k)> val:<val(v)> => (entry key val) 🗺️
```

Notes:
Expand Down
1 change: 1 addition & 0 deletions design/mvp/CanonicalABI.md
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,7 @@ def despecialize(t):
case OptionType(t) : return VariantType([ CaseType("none", None), CaseType("some", t) ])
case ResultType(ok, err) : return VariantType([ CaseType("ok", ok), CaseType("error", err) ])
case MapType(k, v) : return ListType(despecialize(TupleType([k, v])))
case UnionType(cases) : return VariantType(cases)
case _ : return t
```
The specialized value types `string` and `flags` are missing from this list
Expand Down
59 changes: 51 additions & 8 deletions design/mvp/JS-Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ Dispatch on `componentValType`:
- `flags "L"+` → `ToJSValueFlags`(|componentValue|, the labels).
- `enum "L"+` → String, the label verbatim.
- `option<T>` not nested within another `option` → **undefined** for `none`, else `ToJSValue`(the payload, T).
- `variant`, and nested `option`, and `result` outside return position → `ToJSValueVariant`(|componentValue|, the cases). In return position a `result` is unwrapped instead, into a return value or a thrown `ComponentError` (see [Read the imports](#read-the-imports-object) and [Create the exports object](#create-the-exports-object)).
- `variant`, and nested `option`, and `result` outside return position → `ToJSValueVariant`(|componentValue|). In return position a `result` is unwrapped instead, into a return value or a thrown `ComponentError` (see [Read the imports](#read-the-imports-object) and [Create the exports object](#create-the-exports-object)).
- `union` → Given the |payload| of |componentValue|, `ToJSValue`(|payload|, type of |payload|).
- `map<K, V>` → `ToJSValueMap`(|componentValue|, K, V).
- `own<R>` / `borrow<R>` → See [Resource types](#resource-types).
- `future<T>` → a Promise (TODO).
Expand Down Expand Up @@ -293,11 +294,12 @@ The `scalar typed array class` is given by:
1. Perform `CreateDataPropertyOrThrow`(|object|, `CamelCase`(L), |value|'s `L` bit as a Boolean).
1. Return |object|.

`ToJSValueVariant(value, cases)`:
`ToJSValueVariant(value)`:
1. Let |case| be the current case of the variant |value|.
1. Let |object| be `OrdinaryObjectCreate`(**null**).
1. Perform `CreateDataPropertyOrThrow`(|object|, "kind", `CaseLabelOf`(|value|, |cases|)).
1. If that case has a payload of type T:
1. Perform `CreateDataPropertyOrThrow`(|object|, "value", `ToJSValue`(`PayloadOf`(|value|), T)).
1. Perform `CreateDataPropertyOrThrow`(|object|, "kind", the label of |case|).
1. If |case| has a payload of type |T|:
1. Perform `CreateDataPropertyOrThrow`(|object|, "value", `ToJSValue`(the payload of |value|, |T|)).
1. Return |object|.

This operation is used for variant, and also cases in the above table where we can't specialize the behavior of `option` and `result`.
Expand Down Expand Up @@ -337,6 +339,7 @@ Dispatch on `targetComponentType`:
- `enum` → ? `ToString`(|jsValue|) must be one of the labels, else throw a `TypeError`.
- `option<T>` not nested within another `option` → **null** and **undefined** both give `none`; anything else gives `some(ToComponentValue(jsValue, T))`.
- `variant`, and nested `option`, and `result` outside return position → ? `ToComponentValueVariant`(|jsValue|, the cases). In return position a `result` is unwrapped instead: a JS return value becomes `result.ok`, and a thrown exception becomes `result.error` (see [Read the imports](#read-the-imports-object) and [Create the exports object](#create-the-exports-object)).
- `union` → ? `ToComponentValueUnion`(|jsValue|, the cases)
- `map<K, V>` → ? `ToComponentValueMap`(|jsValue|, K, V).
- `own<R>` / `borrow<R>` → See [Resource types](#resource-types).
- `future<T>` → TODO.
Expand Down Expand Up @@ -453,6 +456,47 @@ An absent property is therefore **false**, matching a `boolean` dictionary membe

This operation is used for variant, and also cases in the above table where we can't specialize the behavior of `option` and `result`.

`DistinguishableKind(jsValue, resourceTypes)`:
1. For each |R| in |resourceTypes|:
1. Let |abstractTypeKey| be the *abstract type key* of |R|.
1. If |R|'s abstract type is one of the component's type imports:
1. Let |hostType| be the current call's [[HostResourceTypes]][|abstractTypeKey|].
1. Let |matches| be ? `brand check` given |jsValue| and |hostType|.[[ConstructorObject]].
1. If |matches|:
1. Return **resource |R|**.
1. Otherwise:
1. Let |class| be the current call's [[GuestResourceClasses]][|abstractTypeKey|].
1. If |jsValue| has a [[ResourceClass]] internal slot and |jsValue|.[[ResourceClass]] is |class|:
1. Return **resource |R|**.
1. If |jsValue| is a String, return **string**.
1. If |jsValue| is a Boolean, return **numeric**.
1. If |jsValue| is a Number, return **numeric**.
1. If |jsValue| is a BigInt, return **numeric**.
1. If |jsValue| is not an Object, throw a `TypeError`.
1. If `IsArray`(|jsValue|), return **list**.
1. If |jsValue| has a [[TypedArrayName]] internal slot, return **list**.
1. Return **object**.

This operation is used for `ToComponentValueUnion`.

`ToComponentValueUnion(jsValue, cases)`:
1. Let |resourceTypes| be all the resource types in the [distinguishable kinds](Text.md#-union-types) of the members of |cases|, in order.
1. Let |kind| be ? `DistinguishableKind`(|jsValue|, |resourceTypes|).
1. For each |case| of |cases|:
1. Let |T| be the payload type of |case|.
1. Let |caseKinds| be the [distinguishable kinds](Text.md#-union-types) of |T|.
1. If |kind| is in |caseKinds|:
1. If |kind| is **resource |R|** where |R|'s abstract type is one of the component's type imports:
1. Let |abstractTypeKey| be the *abstract type key* of |kind|.
1. Let |hostType| be the current call's [[HostResourceTypes]][|abstractTypeKey|].
1. Let |payload| be a host resource value whose [[Type]] is |hostType| and whose [[JSValue]] is |jsValue|.
1. Otherwise:
1. Let |payload| be ? `ToComponentValue`(|jsValue|, |T|).
1. Return a union value of |case| whose payload is |payload|.
1. Throw a `TypeError`.

This operation dynamically selects a union case based on the distinguishable kind of the provided value, before using `ToComponentValue` for the final result. It ensures that `instanceof` is only called once per (non-WebIDL) host resource type in `cases`.

`ToComponentValueMap(jsValue, K, V)`:
1. If |jsValue| is not an Object:
1. Throw a `TypeError`.
Expand Down Expand Up @@ -1064,8 +1108,8 @@ To `create the exports object` given an [instance record](#instance-records) |in
1. Let |value| be ? `create the exports object` given |instance| and the exports of the exported instance.
1. **component**:
1. Let |value| be a new `Component` whose [[Component]] is |export|.Component.
1. Perform `CreateDataPropertyOrThrow`(|exportsObject|, |key|, |value|).
1. Perform `SetIntegrityLevel`(|exportsObject|, "frozen").
1. Perform ? `CreateDataPropertyOrThrow`(|exportsObject|, |key|, |value|).
1. Perform ? `SetIntegrityLevel`(|exportsObject|, "frozen").
1. Return |exportsObject|.

TODO: We need a JSName for members that hit the `reserved class members` list. It needs to concatenate the resource name with the method name somehow.
Expand Down Expand Up @@ -1181,7 +1225,6 @@ A component's exports become the bindings of its module namespace object. There

## Follow ups

1. How to dynamically pass a union value? Statically passing a single case of the union works, but not dynamic choice.
1. How to import multiple overloads of a function? Can we just use `external-id`?
1. How to support class inheritance and casting?
1. Do we support a reference equality protocol? `ToJSValue` creates a fresh resource object per lift, so two `borrow`s of one component-defined resource are two JS objects that do not compare equal. Reps are opaque and reusable after a drop, so an identity map would need careful invalidation.
Expand Down
Loading
Loading