Skip to content

Commit

Permalink
Implement exports-introduce-indice semantics (#871)
Browse files Browse the repository at this point in the history
* Implement exports-introduce-indice semantics

This commit implements part of WebAssembly/component-model#129,
specifically the portion where an `export` item now introduces a fresh
item into the relevant index space. This is going to be used by #867 to
implement encoding/decoding of wit documents, specifically for `use`
statements.

This commit mostly implements the infrastructure necessary for this
change rather than any other major changes internally. Functionally
`export` statements now basically behave the same as `alias` statements
within the validator. Eventually more validation will be required to
ensure that functions use transitively-named types in all locations.
Additionally the name that a type index is exported at will be relevant
and will need exposing, but that'll come at a later date as well. It's
hoped that exposing this information, as required for WIT
encoding/decoding, will be a relatively small layer on the validator
from this commit.

* Update more tests and tools for types-define-aliases

* Update the version number header of components

This commit performs the first update of the binary header version
number of the component binary encoding. The 4-byte header value, for
core WebAssembly, is 0x00000001. This 4-byte header value is proposed to
be reinterpreted as two 16-bit integers. The upper 16-bit integer of the
32-bit header version is reinterpreted as:

* 0x0000 - this is a core module
* 0x0001 - this is a component

The lower 16-bits are then the version within the format. For core wasm
the only supported version is 0x0001 and for components the previous
supported version was 0x000a (with the intention to move to 0x0001
on stabilization). This 0xa header value has never changed throughout
the lifetime of components but I would personally like to start using it
for breaking changes to the binary format during development. In the
spirit of that I've bumped it here to 0xb due to the change of how
`export` introduces an alias. This should break almost all preexisting
component binaries, indicating that a recompilation with newer tooling
is required. It's hoped that the error message produced here will be
more actionable than obscure indexing error messages.

* Update with review comments

* Update expected error message

* Fix another test expectation
  • Loading branch information
alexcrichton committed Jan 5, 2023
1 parent b6233ed commit edaf07e
Show file tree
Hide file tree
Showing 113 changed files with 1,643 additions and 1,502 deletions.
10 changes: 7 additions & 3 deletions crates/wasm-compose/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,7 @@ impl<'a> CompositionGraphEncoder<'a> {
&mut self,
aliases: &mut ComponentAliasSection,
instance: u32,
export: &str,
name: &str,
kind: ComponentExportKind,
) -> u32 {
let (desc, count) = match kind {
Expand All @@ -1427,9 +1427,13 @@ impl<'a> CompositionGraphEncoder<'a> {
ComponentExportKind::Component => ("component", &mut self.components),
};

log::debug!("aliasing {desc} export `{export}` from encoded index {instance} (encoded index {count}) in composed component");
log::debug!("aliasing {desc} export `{name}` from encoded index {instance} (encoded index {count}) in composed component");

aliases.instance_export(instance, kind, export);
aliases.alias(Alias::InstanceExport {
instance,
kind,
name,
});

let index = *count;
*count += 1;
Expand Down
8 changes: 4 additions & 4 deletions crates/wasm-compose/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1291,10 +1291,10 @@ mod test {
(module)
)
(import "i4" (core module (;0;) (type 0)))
(export "e1" (instance 0))
(export "e2" (func 0))
(export "e3" (component 0))
(export "e4" (core module 0))
(export (;1;) "e1" (instance 0))
(export (;1;) "e2" (func 0))
(export (;1;) "e3" (component 0))
(export (;1;) "e4" (core module 0))
)
(component (;2;)
(type (;0;)
Expand Down
110 changes: 55 additions & 55 deletions crates/wasm-compose/tests/compositions/complex-import/composed.wat
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,33 @@
(type (;13;) (record (field "a" s8) (field "b" u8) (field "c" s16) (field "d" u16) (field "e" s32) (field "f" u32) (field "g" s64) (field "h" u64) (field "i" float32) (field "j" float64) (field "k" bool) (field "l" string)))
(export "record1" (type (eq 13)))
(type (;14;) (func (param "x" 13)))
(export "n" (func (type 14)))
(export "n" (func (type 15)))
(type (;15;) (list 13))
(type (;16;) (func (param "x" 15)))
(export "o" (func (type 16)))
(type (;16;) (func (param "x" 16)))
(export "o" (func (type 17)))
(type (;17;) (tuple 13 string))
(type (;18;) (func (param "x" 17)))
(export "p" (func (type 18)))
(type (;18;) (func (param "x" 18)))
(export "p" (func (type 19)))
(type (;19;) (flags "a" "b" "c"))
(export "flags1" (type (eq 19)))
(type (;20;) (func (param "x" 19)))
(export "q" (func (type 20)))
(export "flags1" (type (eq 20)))
(type (;20;) (func (param "x" 20)))
(export "q" (func (type 22)))
(type (;21;) (enum "a" "b" "c"))
(export "enum1" (type (eq 21)))
(type (;22;) (func (param "x" 21)))
(export "r" (func (type 22)))
(export "enum1" (type (eq 23)))
(type (;22;) (func (param "x" 23)))
(export "r" (func (type 25)))
(type (;23;) (union s8 string 13))
(export "union1" (type (eq 23)))
(type (;24;) (func (param "x" 23)))
(export "s" (func (type 24)))
(export "union1" (type (eq 26)))
(type (;24;) (func (param "x" 26)))
(export "s" (func (type 28)))
(type (;25;) (variant (case "a" s8) (case "b" u8) (case "c" s16) (case "d" u16) (case "e" s32) (case "f" u32) (case "g" s64) (case "h" u64) (case "i" float32) (case "j" float64) (case "k" bool) (case "l" string) (case "m" 13)))
(export "variant1" (type (eq 25)))
(type (;26;) (option 25))
(type (;27;) (func (param "x" 26)))
(export "t" (func (type 27)))
(export "variant1" (type (eq 29)))
(type (;26;) (option 29))
(type (;27;) (func (param "x" 31)))
(export "t" (func (type 32)))
(type (;28;) (result 13 (error string)))
(type (;29;) (func (result 28)))
(export "u" (func (type 29)))
(type (;29;) (func (result 33)))
(export "u" (func (type 34)))
)
)
(import "a" (instance (type 0)))
Expand All @@ -91,59 +91,59 @@
(type (;0;) (record (field "a" s8) (field "b" u8) (field "c" s16) (field "d" u16) (field "e" s32) (field "f" u32) (field "g" s64) (field "h" u64) (field "i" float32) (field "j" float64) (field "k" bool) (field "l" string)))
(export "record1" (type (eq 0)))
(type (;1;) (flags "a" "b" "c"))
(export "flags1" (type (eq 1)))
(export "flags1" (type (eq 2)))
(type (;2;) (enum "a" "b" "c"))
(export "enum1" (type (eq 2)))
(export "enum1" (type (eq 4)))
(type (;3;) (union s8 string 0))
(export "union1" (type (eq 3)))
(export "union1" (type (eq 6)))
(type (;4;) (variant (case "a" s8) (case "b" u8) (case "c" s16) (case "d" u16) (case "e" s32) (case "f" u32) (case "g" s64) (case "h" u64) (case "i" float32) (case "j" float64) (case "k" bool) (case "l" string) (case "m" 0)))
(export "variant1" (type (eq 4)))
(export "variant1" (type (eq 8)))
(type (;5;) (func))
(export "a" (func (type 5)))
(export "a" (func (type 10)))
(type (;6;) (func (param "x" s8)))
(export "b" (func (type 6)))
(export "b" (func (type 11)))
(type (;7;) (func (param "x" u8)))
(export "c" (func (type 7)))
(export "c" (func (type 12)))
(type (;8;) (func (param "x" s16)))
(export "d" (func (type 8)))
(export "d" (func (type 13)))
(type (;9;) (func (param "x" u16)))
(export "e" (func (type 9)))
(export "e" (func (type 14)))
(type (;10;) (func (param "x" s32)))
(export "f" (func (type 10)))
(export "f" (func (type 15)))
(type (;11;) (func (param "x" u32)))
(export "g" (func (type 11)))
(export "g" (func (type 16)))
(type (;12;) (func (param "x" s64)))
(export "h" (func (type 12)))
(export "h" (func (type 17)))
(type (;13;) (func (param "x" u64)))
(export "i" (func (type 13)))
(export "i" (func (type 18)))
(type (;14;) (func (param "x" float32)))
(export "j" (func (type 14)))
(export "j" (func (type 19)))
(type (;15;) (func (param "x" float64)))
(export "k" (func (type 15)))
(export "k" (func (type 20)))
(type (;16;) (func (param "x" bool)))
(export "l" (func (type 16)))
(export "l" (func (type 21)))
(type (;17;) (func (param "x" string)))
(export "m" (func (type 17)))
(export "m" (func (type 22)))
(type (;18;) (func (param "x" 0)))
(export "n" (func (type 18)))
(export "n" (func (type 23)))
(type (;19;) (list 0))
(type (;20;) (func (param "x" 19)))
(export "o" (func (type 20)))
(type (;20;) (func (param "x" 24)))
(export "o" (func (type 25)))
(type (;21;) (tuple 0 string))
(type (;22;) (func (param "x" 21)))
(export "p" (func (type 22)))
(type (;23;) (func (param "x" 1)))
(export "q" (func (type 23)))
(type (;24;) (func (param "x" 2)))
(export "r" (func (type 24)))
(type (;25;) (func (param "x" 3)))
(export "s" (func (type 25)))
(type (;26;) (option 4))
(type (;27;) (func (param "x" 26)))
(export "t" (func (type 27)))
(type (;22;) (func (param "x" 26)))
(export "p" (func (type 27)))
(type (;23;) (func (param "x" 2)))
(export "q" (func (type 28)))
(type (;24;) (func (param "x" 4)))
(export "r" (func (type 29)))
(type (;25;) (func (param "x" 6)))
(export "s" (func (type 30)))
(type (;26;) (option 8))
(type (;27;) (func (param "x" 31)))
(export "t" (func (type 32)))
(type (;28;) (result 0 (error string)))
(type (;29;) (func (result 28)))
(export "u" (func (type 29)))
(type (;29;) (func (result 33)))
(export "u" (func (type 34)))
)
)
(import "input" (component (;0;) (type 0)))
Expand All @@ -167,6 +167,6 @@
)
(alias export 5 "m1" (func (;0;)))
(alias export 5 "m2" (func (;1;)))
(export "m1" (func 0))
(export "m2" (func 1))
(export (;2;) "m1" (func 0))
(export (;3;) "m2" (func 1))
)
Loading

0 comments on commit edaf07e

Please sign in to comment.