Skip to content
This repository was archived by the owner on Aug 17, 2022. It is now read-only.

Commit ff45ea4

Browse files
author
Luke Wagner
committed
Change alias sugar, remove indices from aliases/instantiate (#19 #20)
1 parent 0a9b92e commit ff45ea4

File tree

5 files changed

+254
-277
lines changed

5 files changed

+254
-277
lines changed

proposals/module-linking/Binary.md

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ A new section defining local instances
115115
```
116116
instancesec ::= i*:section_15(vec(instancedef)) -> i*
117117
118-
instancedef ::= 0x00 m:moduleidx e*:vec(exportdesc) -> {instantiate m, imports e*}
118+
instancedef ::= 0x00 m:moduleidx arg*:vec(export) -> {instantiate m, imports arg*}
119119
```
120120

121121
This defines instances in the module, appending to the instance index space.
@@ -130,18 +130,9 @@ the future we'll likely want this binary value to match that.
130130
* The module index `m` must be in bounds.
131131
* Indices of items referred to by `exportdesc` are all in-bounds. Can only refer
132132
to imports/previous aliases, since only those sections can precede.
133-
* The `e*` list is validated against the module type's declared list
134-
of [imports pairwise and in-order](Explainer.md#module-imports-and-nested-instances).
135-
The type of each item must be a subtype of the expected type listed in the
136-
module's type.
133+
* The `arg*` list is validated against `m`'s imports according to
134+
[module subtyping](Subtyping.md#instantiation) rules.
137135

138-
**Execution notes**
139-
140-
* The actual module being instantiated does not need to list imports in the
141-
exact same order as its type declaration. The `e*` has names based on the
142-
local module type's declaration.
143-
* Be sure to read up on [subtyping](./Subtyping.md) to ensure that instances
144-
with a single name can be used to match imports with a two-level namespace.
145136

146137
## Alias section (16)
147138

@@ -151,39 +142,22 @@ A new module section is added
151142
aliassec ::= a*:section_16(vec(alias)) -> a*
152143
153144
alias ::=
154-
0x00 i:instanceidx 0x00 e:exportidx -> (alias (instance $i) (func $e))
155-
0x00 i:instanceidx 0x01 e:exportidx -> (alias (instance $i) (table $e))
156-
0x00 i:instanceidx 0x02 e:exportidx -> (alias (instance $i) (memory $e))
157-
0x00 i:instanceidx 0x03 e:exportidx -> (alias (instance $i) (global $e))
158-
0x00 i:instanceidx 0x05 e:exportidx -> (alias (instance $i) (module $e))
159-
0x00 i:instanceidx 0x06 e:exportidx -> (alias (instance $i) (instance $e))
160-
0x01 0x05 m:moduleidx -> (alias parent (module $m))
161-
0x01 0x07 t:typeidx -> (alias parent (type $t))
145+
0x00 i:instanceidx nm:name -> (alias (* $i nm))
146+
0x01 ct:varu32 0x05 m:moduleidx -> (alias (module outer ct m))
147+
0x01 ct:varu32 0x07 t:typeidx -> (alias (type outer ct t))
162148
```
163149

164150
**Validation**
165151

166-
* Aliased instance indexes are all in bounds. Remember "in bounds" here means it
167-
can't refer to instances defined after the `alias` item.
168-
* Aliased instance export indices are in bounds relative to the instance's
169-
*locally-declared* (via module or instance type) list of exports.
170-
* Export indices match the actual type of the export
171-
* Aliases append to the respective index space.
172-
* Parent aliases can only happen in submodules (not the top-level module) and
173-
the index specifies is the entry, in the parent's raw index space, for that
174-
item.
175-
* Parent aliases can only refer to preceeding module/type definitions, relative
176-
to the binary location where the inline module's type was declared. Note that
177-
when the module code section is defined all of the parent's modules/types are
178-
available, but inline modules still may not have access to all of these if the
179-
items were declared after the module's type in the corresponding module
180-
section.
181-
182-
**Execution notes**
183-
184-
* Note for child aliases that while the export is referred to by index it's
185-
actually loaded from the specified instance by name. The name loaded
186-
corresponds to the `i`th export's name in the locally defined type.
152+
* The instance, module and type indices are all in bounds. Remember that "in bounds"
153+
means only those definitions preceding this alias definition in binary format
154+
order. In the case of outer aliases, this means the position of the nested module
155+
definition in the outer module.
156+
* Aliased instance export names must be found in the instance `$i`.
157+
* For instance export aliases, the index space into which the alias is
158+
injected is determined by the definition kind of the named export.
159+
* The `ct` of an outer alias must be *less than* the number of enclosing modules
160+
which implicitly prevents outer aliases from appearing in top-level modules.
187161

188162
## Function section
189163

proposals/module-linking/Example-LinkTimeVirtualization.md

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@ We start with a child module that has been written and compiled separately,
88
without regard to the parent module. It imports `wasi_file` to do file I/O:
99

1010
```wasm
11-
;; child.wat
12-
(module
13-
(type $WasiFile (instance
14-
(export "read" (func $read (param i32 i32 i32) (result i32)))
15-
(export "write" (func $write (param i32 i32 i32) (result i32)))
11+
(module $CHILD
12+
(import "wasi_file" (instance $wasi-file
13+
(export "read" (func (param i32 i32 i32) (result i32)))
14+
(export "write" (func (param i32 i32 i32) (result i32)))
1615
))
17-
(import "wasi_file" (instance $wasi-file (type $WasiFile)))
1816
(func $play (export "play")
1917
...
20-
call $wasi-file.$read
18+
call (func $wasi-file "read")
2119
...
2220
)
2321
)
@@ -31,8 +29,11 @@ as a separate module that imports a "real" `wasi_file` instance and uses it
3129
to implement a new virtualized `wasi_file` instance:
3230

3331
```wasm
34-
;; virtualize.wat
35-
(module
32+
(module $VIRTUALIZE
33+
(type $WasiFile (instance
34+
(export "read" (func (param i32 i32 i32) (result i32)))
35+
(export "write" (func (param i32 i32 i32) (result i32)))
36+
))
3637
(import "wasi_file" (instance $wasi-file (type $WasiFile)))
3738
3839
(func (export "read") (param i32 i32 i32) (result i32)
@@ -44,44 +45,41 @@ to implement a new virtualized `wasi_file` instance:
4445
)
4546
```
4647

47-
We can now write the parent module by composing `virtualize.wasm` and
48-
`child.wasm` appropriately:
48+
We can now write the parent module by composing `$VIRTUALIZE` and `$CHILD`:
4949

5050
```wasm
51-
;; parent.wat
52-
(module
51+
(module $PARENT
5352
(type $WasiFile (instance
54-
(export "read" (func $read (param i32 i32 i32) (result i32)))
55-
(export "write" (func $write (param i32 i32 i32) (result i32)))
53+
(export "read" (func (param i32 i32 i32) (result i32)))
54+
(export "write" (func (param i32 i32 i32) (result i32)))
5655
))
5756
(import "wasi_file" (instance $real-wasi (type $WasiFile)))
5857
59-
(import "./virtualize.wasm" (module $VIRTUALIZE
60-
(import "wasi_file" (instance (type $WasiFile)))
61-
(export $WasiFile)
58+
(import "virtualize" (module $VIRTUALIZE
59+
(import "wasi_file" (instance (type outer $PARENT $WasiFile)))
60+
(export (type outer $PARENT $WasiFile))
6261
))
63-
(import "./child.wasm" (module $CHILD
64-
(import "wasi_file" (instance (type $WasiFile)))
62+
(import "child" (module $CHILD
63+
(import "wasi_file" (instance (type outer $PARENT $WasiFile)))
6564
(export "play" (func))
6665
))
6766
68-
(instance $virt-wasi (instantiate $VIRTUALIZE (instance $real-wasi)))
69-
(instance $child (instantiate $CHILD (instance $virt-wasi)))
67+
(instance $virt-wasi (instantiate $VIRTUALIZE "wasi_file" (instance $real-wasi)))
68+
(instance $child (instantiate $CHILD "wasi_file" (instance $virt-wasi)))
7069
7170
(func (export "work")
7271
...
73-
call $child.$play
72+
call (func $child "play")
7473
...
7574
)
7675
)
7776
```
7877

7978
Here, we assume the host understands relative file paths, but we could also bundle
80-
all 3 files into one compound `parent-bundle.wasm`:
79+
all 3 files into one compound `$PARENT-BUNDLE` module:
8180

8281
```wasm
83-
;; parent-bundled.wat
84-
(module
82+
(module $PARENT-BUNDLE
8583
(type $WasiFile ...same as above)
8684
(import "wasi_file" ...same as above)
8785

0 commit comments

Comments
 (0)