Skip to content

Commit

Permalink
Extract "world elaboration" to a separate function (bytecodealliance…
Browse files Browse the repository at this point in the history
…#1800)

* Extract "world elaboration" to a separate function

This commit extracts the process of elaborating a worlds imports/exports
to a dedicated function. This function ensures the transitive closure of
all imports/exports are listed in the world in proper topograhical
order. This additionally validates that the world has a coherent
definition given WIT today, notably that imports don't accidentally try
to use exported types.

This functionality was all present previously during the process of
taking a WIT document and creating `Resolve`. This moves the logic
around and refactors it slightly given its new surroundings. This
reorders a few imports/exports in worlds from what was previously
present but the underlying meaning of worlds should be the same.

The goal of this commit is to enable `Resolve`-mutating transformations
to not need to preserve all these invariants each time a world is
modified. Instead a world can be modified and then this function can be
called to "clean up" the world afterwards.

* Simplify the `Resolve::importize` operation

This commit updates the `importize` operation to be a much simpler "just
move the exports to the imports" operation where care is only taken to
preserve imported types and to additionally error on overlap of imported
types and exported functions/interfaces. The previous `elaborate_world`
method helps make this method much simpler than before.
  • Loading branch information
alexcrichton authored Sep 18, 2024
1 parent 5a83828 commit e5132af
Show file tree
Hide file tree
Showing 19 changed files with 397 additions and 455 deletions.
52 changes: 26 additions & 26 deletions crates/wit-component/tests/interfaces/resources.wat
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,6 @@
(type (;0;)
(component
(type (;0;)
(instance
(export (;0;) "a" (type (sub resource)))
(type (;1;) (own 0))
(type (;2;) (func (result 1)))
(export (;0;) "[constructor]a" (func (type 2)))
(type (;3;) (func))
(export (;1;) "[static]a.b" (func (type 3)))
(type (;4;) (borrow 0))
(type (;5;) (func (param "self" 4)))
(export (;2;) "[method]a.c" (func (type 5)))
)
)
(import "anon" (instance (;0;) (type 0)))
(type (;1;)
(instance
(export (;0;) "bar" (type (sub resource)))
(export (;1;) "t" (type (eq 0)))
Expand All @@ -131,14 +117,14 @@
(export (;3;) "a" (func (type 9)))
)
)
(import "foo:bar/foo" (instance (;1;) (type 1)))
(alias export 1 "bar" (type (;2;)))
(alias export 1 "t" (type (;3;)))
(type (;4;)
(import "foo:bar/foo" (instance (;0;) (type 0)))
(alias export 0 "bar" (type (;1;)))
(alias export 0 "t" (type (;2;)))
(type (;3;)
(instance
(alias outer 1 2 (type (;0;)))
(alias outer 1 1 (type (;0;)))
(export (;1;) "bar" (type (eq 0)))
(alias outer 1 3 (type (;2;)))
(alias outer 1 2 (type (;2;)))
(export (;3;) "t" (type (eq 2)))
(type (;4;) (own 1))
(type (;5;) (func (param "x" 4) (result 4)))
Expand All @@ -148,15 +134,29 @@
(export (;1;) "b" (func (type 7)))
)
)
(import "foo:bar/baz" (instance (;2;) (type 4)))
(alias export 2 "bar" (type (;5;)))
(import "bar" (type (;6;) (eq 5)))
(import "a" (type (;7;) (sub resource)))
(type (;8;) (own 7))
(import "foo:bar/baz" (instance (;1;) (type 3)))
(alias export 1 "bar" (type (;4;)))
(import "bar" (type (;5;) (eq 4)))
(import "a" (type (;6;) (sub resource)))
(type (;7;)
(instance
(export (;0;) "a" (type (sub resource)))
(type (;1;) (own 0))
(type (;2;) (func (result 1)))
(export (;0;) "[constructor]a" (func (type 2)))
(type (;3;) (func))
(export (;1;) "[static]a.b" (func (type 3)))
(type (;4;) (borrow 0))
(type (;5;) (func (param "self" 4)))
(export (;2;) "[method]a.c" (func (type 5)))
)
)
(import "anon" (instance (;2;) (type 7)))
(type (;8;) (own 6))
(type (;9;) (func (result 8)))
(import "[constructor]a" (func (;0;) (type 9)))
(export (;1;) "x" (func (type 9)))
(type (;10;) (own 6))
(type (;10;) (own 5))
(type (;11;) (func (result 10)))
(export (;2;) "y" (func (type 11)))
)
Expand Down
4 changes: 2 additions & 2 deletions crates/wit-component/tests/interfaces/resources.wit.print
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ interface implicit-own-handles2 {
}

world some-world {
import foo;
import baz;
import anon: interface {
resource a {
constructor();
b: static func();
c: func();
}
}
import foo;
import baz;
use baz.{bar};

resource a {
Expand Down
14 changes: 7 additions & 7 deletions crates/wit-component/tests/interfaces/world-top-level.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
(component
(type (;0;)
(component
(type (;0;)
(type (;0;) (func))
(import "foo" (func (;0;) (type 0)))
(type (;1;) (func (param "arg" u32)))
(import "bar" (func (;1;) (type 1)))
(type (;2;)
(instance)
)
(import "some-interface" (instance (;0;) (type 0)))
(type (;1;) (func))
(import "foo" (func (;0;) (type 1)))
(type (;2;) (func (param "arg" u32)))
(import "bar" (func (;1;) (type 2)))
(export (;2;) "foo2" (func (type 1)))
(import "some-interface" (instance (;0;) (type 2)))
(export (;2;) "foo2" (func (type 0)))
(type (;3;) (func (result u32)))
(export (;3;) "bar2" (func (type 3)))
(type (;4;)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package foo:foo;

world foo {
import some-interface: interface {
}
import foo: func();
import bar: func(arg: u32);
import some-interface: interface {
}

export foo2: func();
export bar2: func() -> u32;
Expand Down
Loading

0 comments on commit e5132af

Please sign in to comment.