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
4 changes: 2 additions & 2 deletions design/mvp/Binary.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,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
15 changes: 8 additions & 7 deletions design/mvp/JS-Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ 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)).
- `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 +293,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 @@ -1064,8 +1065,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
219 changes: 121 additions & 98 deletions design/mvp/Text.md
Original file line number Diff line number Diff line change
Expand Up @@ -2727,8 +2727,10 @@ plainname ::= <label>
| '[get]' <label> 📡
| '[set]' <label> 📡
| '[constructor]' <label>
| '[method]' (📡 '[get]' | '[set]')? <label> '.' <label>
| '[static]' (📡 '[get]' | '[set]')? <label> '.' <label>
| '[method]' <label> '.' <label>
| '[method]' ('[get]' | '[set]') <label> '.' <label> 📡
| '[static]' <label> '.' <label>
| '[static]' ('[get]' | '[set]') <label> '.' <label> 📡
labellit ::= '"' <label> '"'
label ::= <first-fragment> ( '-' <fragment> )*
first-fragment ::= <first-word>
Expand Down Expand Up @@ -2762,45 +2764,28 @@ The names of component imports and exports provide two options:
(🪺 possibly nested) package that is published in a (🪺 possibly nested)
namespace.

The `plainname` production captures several language-neutral syntactic hints
that allow bindings generators to produce more idiomatic bindings in their
target language. At the top level, a `plainname` allows functions to be
annotated as being a constructor, method, 📡 property getter, 📡 property setter,
or static function of a preceding resource, as well as a 📡 property getter or
📡 property setter not attached to a resource.

When a function is annotated with `constructor`, `method`, or `static`, the
first `label` is the name of the resource and the second `label` is the logical
field name of the function. This additional nesting information allows bindings
generators to insert the function into the nested scope of a class, abstract
data type, object, namespace, package, module or whatever resources get bound
to. For example, a function named `[method]C.foo` could be bound in C++ to a
member function `foo` in a class `C`. The [JS API](JS-Explainer.md#exporting-a-resource) describes
how the native JavaScript bindings look.

To restrict the set of cases that bindings generators need to consider, these
annotations trigger additional type-validation rules (listed in detail in
[Binary.md](Binary.md#import-and-export-definitions)):
* A `[constructor]` import or export named `R` must be a function whose result
type is `(own $R)` or `(result (own $R) (error <valtype>)?)`, where `$R` is
the index of the resource type named `R`.
* A `[method]` import or export named `R.foo` must be a function whose first
parameter is `(param "self" (borrow $R))`, where `$R` is the index of the
resource type named `R`.
* A `[static]` import or export named `R.foo` must be a function, and `R` must
be the name of a resource type.
* 📡 A `[get]` import or export must have no parameters (besides the required
`self` parameter from `[method]`), and must return a value.
* 📡 A `[set]` import or export must be preceded by a matching `[get]` import
or export, must have exactly one parameter (besides the required `self`
parameter from `[method]`) whose type matches the getter's return type, and
must return either nothing or `(result (error <valtype>)?)`.
As an example, the following component uses all 4 cases of imports and exports:
```wat
(component
(import "custom-hook" (func (param string) (result string)))
(import "wasi:http/handler" (instance
(export "request" (type $request (sub resource)))
(export "response" (type $response (sub resource)))
(export "handle" (func (param (own $request)) (result (own $response))))
))
... impl
(export "wasi:http/handler" (instance $http_handler_impl))
(export "get-JSON" (func $get_json_impl))
)
```
Here, `custom-hook` and `get-JSON` are plain names for functions whose semantic
contract is particular to this component and not defined elsewhere. In
contrast, `wasi:http/handler` is the name of a separately-defined interface,
allowing the component to request the ability to make outgoing HTTP requests
(through imports) and receive incoming HTTP requests (through exports) in a way
that can be mechanically interpreted by hosts and tooling.

The `valid semver` production (along with its `version core` and `pre-release`
sub-productions) is as defined by the [Semantic Versioning 2.0] spec and is
meant to be interpreted according to that specification. The use of
`valid semver` in `interfaceversion` is temporary for backward compatibility;
see [Canonical interface name](#-canonical-interface-name) below (🔗).
#### Kebab Case

The `label` production used inside `plainname` as well as the labels of
`record` and `variant` types must be [kebab case]. The reason for
Expand All @@ -2823,56 +2808,97 @@ Technically, based on the definitions given, the `fragment` `2` (and `3`) in
`a1-2-3` is ambiguously either a `word` or an `acronym`, but the distinction
only relates to casing and numbers aren't cased.

As an example, the following component uses all 4 cases of imports and exports:
```wat
(component
(import "custom-hook" (func (param string) (result string)))
(import "wasi:http/handler" (instance
(export "request" (type $request (sub resource)))
(export "response" (type $response (sub resource)))
(export "handle" (func (param (own $request)) (result (own $response))))
))
... impl
(export "wasi:http/handler" (instance $http_handler_impl))
(export "get-JSON" (func $get_json_impl))
)
```
Here, `custom-hook` and `get-JSON` are plain names for functions whose semantic
contract is particular to this component and not defined elsewhere. In
contrast, `wasi:http/handler` is the name of a separately-defined interface,
allowing the component to request the ability to make outgoing HTTP requests
(through imports) and receive incoming HTTP requests (through exports) in a way
that can be mechanically interpreted by hosts and tooling.
#### Name Attributes

The `plainname` production captures several language-neutral syntactic hints
that allow bindings generators to produce more idiomatic bindings in their
target language. At the top level, a `plainname` adds extra attributes that
allow functions to be marked as constructors, methods, or static functions of
a preceding resource, as well as 📡 property getters or 📡 property setters.

Depending on their attributes, `plainname`s can have up to two `label`s. The
two possible labels are called the *resource name* and the *function name*.

The `[constructor]`, `[method]`, and `[static]` attributes allow functions to
be associated with resource types, enabling bindings generators to insert the
function into the nested scope of a class, abstract data type, object,
namespace, package, module, or whatever other construct is used to represent
resource types. For example, a function named `[method]C.foo` could be bound in
C++ to a member function `foo` in a class `C`, and `[constructor]C` could be
bound to a constructor of `C`. `[constructor]` requires a resource name but
no function name. `[method]` and `[static]` require both a resource name
and function name.

📡 The `[get]` and `[set]` attributes allow functions to be bound as property
accessors ("getters and setters") in languages that support them. `[get]` and
`[set]` are compatible with `[method]` and `[static]`, but not `[constructor]`.
For example, `[method][get]C.foo` could be bound in JS to a getter called `foo`
in a class `C`. "Standalone" getters and setters are also supported, as some
languages allow getters and setters that are not associated with a resource or
class.

These options impose additional validation rules on the function type (e.g.
`[method]` requires a `self` param), as well as on the other function names in
the same scope (e.g. `[set]foo` requires `[get]foo`). All of these rules are
described in detail in [Binary.md](Binary.md#import-and-export-definitions).

To sum things up, here are some valid and invalid names:

```
;; Valid
foo
foo-bar
[constructor]blob
[method]blob.read
[static]blob.merge
[get]global-context ;; 📡
[method][get]blob.position ;; 📡
[static][get]blob.max-size ;; 📡
[static][set]blob.max-size ;; 📡

;; Invalid
[constructor][method]blob ;; [constructor] and [method] are incompatible
[method]blob ;; missing function name
[constructor]blob.with-bytes ;; [constructor] cannot take a function name
[get][method]blob.position ;; 📡 attributes in wrong order
```

#### Semver

The `valid semver` production (along with its `version core` and `pre-release`
sub-productions) is as defined by the [Semantic Versioning 2.0] spec and is
meant to be interpreted according to that specification. The use of
`valid semver` in `interfaceversion` is temporary for backward compatibility;
see [Canonical interface name](#-canonical-interface-name) below (🔗).

#### Name Uniqueness

The goal of the `label` and `externname` productions defined and used above
is to allow automated bindings generators to map these names into something more
idiomatic to the language. For example, the name `[method]my-resource.my-method`
might get mapped to a method named `myMethod` nested inside a class
`MyResource`. To unburden bindings generators from having to consider
pathological cases where two unique-in-the-component names get mapped to the
same source-language identifier, Component Model validation imposes a stronger
form of uniqueness than simple string equality on all the names that appear
within the same scope.

To determine whether two names (defined as sequences of [Unicode Scalar
Values]) are **strongly-unique**:

1. Canonicalize each name:
1. Remove all hyphens and lowercase all the `acronym`s (uppercase letters) in
the name.
2. If the name is `[...]*l.l` for any annotations `[...]*` and some `label`
`l`, replace the name with `l` (e.g. `[method]foo.foo` becomes `foo`).
3. Strip all `[...]` annotations from the name besides `[constructor]`
and 📡 `[set]`.
2. The names are strongly-unique if the resulting canonicalized strings are
unequal.

Thus, the following set of names are strongly-unique and can thus all be
imports (or exports) of the same component (or component type or instance
type):
`MyResource`. However, there are many cases where two unique component names
may map to the same source-language identifier. To encourage broad
source-language compatibility and make bindings generators easier to write,
Component Model validation imposes a stronger form of uniqueness than simple
string equality on all names that appear within the same scope.

First, we define what it means to **canonicalize** a name:

1. Remove all hyphens and lowercase the entire name.
2. If the resource name and function name are equal, replace the entire name
with the resource name (e.g. `[method]foo.foo` becomes `foo`).
3. Strip all `[...]` attributes from the name besides `[constructor]` and
`[set]`.

For example, `[method]FOO.bar-baz` is canonicalized to `foo.barbaz`, and
`[constructor]R` is canonicalized to `[constructor]r`.

Then, we say that two names are **strongly-unique** if their canonical forms
are unequal.

Thus, the following set of names are strongly-unique and can all be imports (or
exports) of the same component (or component type or instance type):

* `foo`, `foo-bar`, `[constructor]foo`, `[method]foo.bar`, `[static]foo.baz`,
`foo:bar/baz`, 📡 `[get]prop`, 📡 `[set]prop`, 📡 `[method][get]foo.prop`,
Expand All @@ -2897,30 +2923,27 @@ but attempting to add *any* of the following names would be a validation error:
`[method][set]foo.prop`)

Note that additional validation rules involving types apply to names with
annotations. For example, the validation rules for `[constructor]foo` require
attributes. For example, the validation rules for `[constructor]foo` require
`foo` to be a resource type. See [Binary.md](Binary.md#import-and-export-definitions)
for details.

Note that these rules prevent having a method or static function (📡 or getter
or setter) with the same name as its resource type, leaving room for a
constructor. This is to satisfy the naming rules around constructors in many
languages.
Note that these rules prevent having a method or static function with the same
name as its resource type, leaving room for a constructor. This is to satisfy
the naming rules around constructors in many languages.

📡 Also note that, although `[get]foo` and `foo` are not considered
strongly-unique, `[set]foo` and `foo` are. However, since `[set]foo` separately
requires `[get]foo` to be present, there is no practical concern of a bindings
conflict between `[set]foo` and `foo`. The same goes for other uses of `[get]`
and `[set]` annotations.

📡 Note also that `[method][get]foo.prop` and `[method]foo.get-prop` are
considered strongly-unique even though in practice they may conflict. Likewise,
`[method][set]foo.prop` and `[method]foo.set-prop` are considered
strongly-unique. If this results in a conflict for a bindings generator, the
bindings generator may choose one of the two and ignore the other. This is a
temporary measure: in a later release, it is expected that both of these
examples will not be considered strongly-unique, so API designers are
encouraged to avoid this case.

and `[set]` attributes.

The strong uniqueness rules cannot absolutely prevent collisions in generated
bindings; in practice, conflicts are always possible. For example,
`[method][get]foo.prop` and `[method]foo.get-prop` are considered
strongly-unique, as are `[method][set]foo.prop` and `[method]foo.set-prop`.
Bindings generators are free to handle conflicts however they choose, e.g.
renaming a binding, throwing an error, or even ignoring an item. API designers
are encouraged to avoid overly similar or ambiguous naming.

#### 🔗 Canonical Interface Name

Expand Down
Loading