Skip to content

Commit 9248a5f

Browse files
committed
Add <core:alias> to <core:moduledecl>
1 parent 5a3d256 commit 9248a5f

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

design/mvp/Binary.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ core:deftype ::= ft:<core:functype> => ft (
131131
core:moduletype ::= 0x50 md*:vec(<core:moduledecl>) => (module md*)
132132
core:moduledecl ::= 0x00 i:<core:import> => i
133133
| 0x01 t:<core:type> => t
134+
| 0x02 a:<core:alias> => a
134135
| 0x03 e:<core:exportdecl> => e
135136
core:importdecl ::= i:<core:import> => i
136137
core:exportdecl ::= n:<name> d:<core:importdesc> => (export n d)
@@ -140,8 +141,11 @@ Notes:
140141
* Validation of `core:moduledecl` (currently) rejects `core:moduletype` definitions
141142
inside `type` declarators (i.e., nested core module types).
142143
* As described in the explainer, each module type is validated with an
143-
initially-empty type index space. Outer aliases can be used to pull
144-
in type definitions from containing components.
144+
initially-empty type index space.
145+
* Validation of `alias` declarators only allows `outer` `type` aliases.
146+
Validation of these aliases cannot see beyond the enclosing core type index
147+
space. Since core modules and core module types cannot nest in the MVP, this
148+
means that the maximum `ct` in an MVP `alias` declarator is `1`.
145149

146150
```
147151
type ::= dt:<deftype> => (type dt)

design/mvp/Explainer.md

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ core:deftype ::= <core:functype> (WebAssembly 1.0)
357357
core:moduletype ::= (module <core:moduledecl>*)
358358
core:moduledecl ::= <core:importdecl>
359359
| <core:type>
360+
| <core:alias>
360361
| <core:exportdecl>
361362
core:importdecl ::= (import <name> <name> <core:importdesc>)
362363
core:exportdecl ::= (export <name> <core:exportdesc>)
@@ -387,7 +388,7 @@ type imports local to the module type itself. For example, in the future, the
387388
following module type would be expressible:
388389
```
389390
(component $C
390-
(type $M (module
391+
(core type $M (module
391392
(import "" "T" (type $T))
392393
(type $PairT (struct (field (ref $T)) (field (ref $T))))
393394
(export "make_pair" (func (param (ref $T)) (result (ref $PairT))))
@@ -398,6 +399,36 @@ In this example, `$M` has a distinct type index space from `$C`, where element
398399
0 is the imported type, element 1 is the `struct` type, and element 2 is an
399400
implicitly-created `func` type referring to both.
400401

402+
Lastly, the `core:alias` module declarator allows a module type definition to
403+
reuse (rather than redefine) type definitions in the enclosing component's core
404+
type index space via `outer` `type` alias. In the MVP, validation restricts
405+
`core:alias` module declarators to *only* allow `outer` `type` aliases but,
406+
in the future, more kinds of aliases would be meaningful and allowed.
407+
408+
As an example, the following component defines two semantically-equivalent
409+
module types, where the former defines the function type via `type` declarator
410+
and the latter refers via `alias` declarator. Note that, since core type
411+
definitions are validated in a Core WebAssembly context that doesn't "know"
412+
anything about components, the module type `$C2` can't name `$C` directly in
413+
the text format but must instead use the appropriate [de Bruijn] index (`1`).
414+
In both cases, the defined/aliased function type is given index `0` since
415+
module types always start with an empty type index space.
416+
```wasm
417+
(component $C
418+
(core type $C1 (module
419+
(type (func (param i32) (result i32)))
420+
(import "a" (func (type 0)))
421+
(export "b" (func (type 0)))
422+
))
423+
(core type $F (func (param i32) (result i32)))
424+
(core type $C2 (module
425+
(alias outer 1 $F (type))
426+
(import "a" (func (type 0)))
427+
(export "b" (func (type 0)))
428+
))
429+
)
430+
```
431+
401432
Component-level type definitions are symmetric to core-level type definitions,
402433
but use a completely different set of value types. Unlike [`core:valtype`]
403434
which is low-level and assumes a shared linear memory for communicating
@@ -542,28 +573,6 @@ When [resource and handle types] are added to the explainer, `typebound` will
542573
be extended with a `sub` option (symmetric to the [type-imports] proposal) that
543574
allows importing and exporting *abstract* types.
544575

545-
Lastly, component and instance types also include an `alias` declarator for
546-
projecting the exports out of imported instances and sharing types with outer
547-
components. As an example, the following component defines two equivalent
548-
component types, where the former defines the function type via `type`
549-
declarator and the latter via `alias` declarator. In both cases, the type is
550-
given index `0` since component types start with an empty type index space.
551-
```wasm
552-
(component $C
553-
(type $C1 (component
554-
(type (func (param string) (result string)))
555-
(import "a" (func (type 0)))
556-
(export "b" (func (type 0)))
557-
))
558-
(type $F (func (param string) (result string)))
559-
(type $C2 (component
560-
(alias outer $C $F (type))
561-
(import "a" (func (type 0)))
562-
(export "b" (func (type 0)))
563-
))
564-
)
565-
```
566-
567576
With what's defined so far, we can define component types using a mix of inline
568577
and out-of-line type definitions:
569578
```wasm

0 commit comments

Comments
 (0)