Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wit-component: Eagerly define all exported types #2091

Merged
merged 2 commits into from
Mar 11, 2025
Merged
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
78 changes: 42 additions & 36 deletions crates/wit-component/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,10 +571,10 @@ impl<'a> EncodingState<'a> {
// Encode a shim instantiation if needed
let shims = self.encode_shim_instantiation()?;

// Next declare all exported resource types. This populates
// `export_type_map` and will additionally be used for imports to
// modules instantiated below.
self.declare_exported_resources(&shims);
// Next declare any types needed for imported intrinsics. This
// populates `export_type_map` and will additionally be used for
// imports to modules instantiated below.
self.declare_types_for_imported_intrinsics(&shims)?;

// Next instantiate the main module. This provides the linear memory to
// use for all future adapters and enables creating indirect lowerings
Expand Down Expand Up @@ -1420,19 +1420,18 @@ impl<'a> EncodingState<'a> {
Ok(type_index)
}

/// This is a helper function that will declare, in the component itself,
/// all exported resources.
/// This is a helper function that will declare any types necessary for
/// declaring intrinsics that are imported into the module or adapter.
///
/// These resources later on get packaged up into instances and such. The
/// main thing that this handles is that it registers the right destructor
/// from `shims`, if needed, for each resource.
fn declare_exported_resources(&mut self, shims: &Shims<'_>) {
/// For example resources must be declared to generate
/// destructors/constructors/etc. Additionally types must also be declared
/// for `task.return` with the component model async feature.
fn declare_types_for_imported_intrinsics(&mut self, shims: &Shims<'_>) -> Result<()> {
let resolve = &self.info.encoder.metadata.resolve;
let world = &resolve.worlds[self.info.encoder.metadata.world];

// Iterate over the main module's exports and the exports of all
// adapters. Look for exported interfaces that themselves have
// resources.
// adapters. Look for exported interfaces.
let main_module_keys = self.info.encoder.main_module_exports.iter();
let main_module_keys = main_module_keys.map(|key| (CustomModule::Main, key));
let adapter_keys = self.info.encoder.adapters.iter().flat_map(|(name, info)| {
Expand All @@ -1448,32 +1447,39 @@ impl<'a> EncodingState<'a> {
};

for ty in resolve.interfaces[id].types.values() {
match resolve.types[*ty].kind {
TypeDefKind::Resource => {}
_ => continue,
}

// Load the destructor, previously detected in module
// validation, if one is present.
let exports = self.info.exports_for(for_module);
let dtor = exports.resource_dtor(*ty).map(|name| {
let name = &shims.shims[&ShimKind::ResourceDtor {
module: for_module,
export: name,
}]
.name;
let shim = self.shim_instance_index.unwrap();
self.core_alias_export(shim, name, ExportKind::Func)
});
match &resolve.types[*ty].kind {
// Declare exported resources specially as they generally
// need special treatment for later handling exports and
// such.
TypeDefKind::Resource => {
// Load the destructor, previously detected in module
// validation, if one is present.
let exports = self.info.exports_for(for_module);
let dtor = exports.resource_dtor(*ty).map(|name| {
let name = &shims.shims[&ShimKind::ResourceDtor {
module: for_module,
export: name,
}]
.name;
let shim = self.shim_instance_index.unwrap();
self.core_alias_export(shim, name, ExportKind::Func)
});

// Declare the resource with this destructor and register it in
// our internal map. This should be the first and only time this
// type is inserted into this map.
let resource_idx = self.component.type_resource(ValType::I32, dtor);
let prev = self.export_type_map.insert(*ty, resource_idx);
assert!(prev.is_none());
// Declare the resource with this destructor and register it in
// our internal map. This should be the first and only time this
// type is inserted into this map.
let resource_idx = self.component.type_resource(ValType::I32, dtor);
let prev = self.export_type_map.insert(*ty, resource_idx);
assert!(prev.is_none());
}
_other => {
self.root_export_type_encoder(Some(id))
.encode_valtype(resolve, &Type::Id(*ty))?;
}
}
}
}
Ok(())
}

/// Helper to instantiate the main module and record various results of its
Expand Down Expand Up @@ -1607,7 +1613,7 @@ impl<'a> EncodingState<'a> {
// an exported resource the component still provides necessary
// intrinsics for manipulating resource state. These are all
// handled here using the resource types created during
// `declare_exported_resources` above.
// `declare_types_for_imported_intrinsics` above.
Import::ExportedResourceDrop(_key, id) => {
let index = self.component.resource_drop(self.export_type_map[id]);
Ok((ExportKind::Func, index))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
(processed-by "my-fake-bindgen" "123.45")
)
)
(alias export 0 "r" (type (;1;)))
(core instance (;0;) (instantiate 0))
(alias export 0 "f" (type (;1;)))
(alias export 0 "r" (type (;2;)))
(alias export 0 "f" (type (;2;)))
(alias export 0 "r" (type (;3;)))
(component (;0;)
(type (;0;) (record (field "f" u32)))
(import "import-type-f" (type (;1;) (eq 0)))
Expand All @@ -25,8 +26,8 @@
(export (;4;) "r" (type 3))
)
(instance (;1;) (instantiate 0
(with "import-type-f" (type 1))
(with "import-type-r" (type 2))
(with "import-type-f" (type 2))
(with "import-type-r" (type 3))
)
)
(export (;2;) "x" (instance 1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
(processed-by "my-fake-bindgen" "123.45")
)
)
(type (;0;) (record (field "f" u32)))
(core instance (;0;) (instantiate 0))
(component (;0;)
(type (;0;) (record (field "f" u32)))
(export (;1;) "foo" (type 0))
)
(instance (;0;) (instantiate 0))
(export (;1;) "foo:foo/name" (instance 0))
(alias export 1 "foo" (type (;0;)))
(type (;1;) (func (param "f" 0)))
(alias core export 0 "name#a" (core func (;0;)))
(func (;0;) (type 1) (canon lift (core func 0)))
(alias export 1 "foo" (type (;2;)))
(component (;1;)
(type (;0;) (record (field "f" u32)))
(import "import-type-foo" (type (;1;) (eq 0)))
Expand All @@ -31,7 +32,7 @@
)
(instance (;2;) (instantiate 1
(with "import-func-a" (func 0))
(with "import-type-foo" (type 0))
(with "import-type-foo" (type 2))
(with "import-type-foo0" (type 0))
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
(processed-by "my-fake-bindgen" "123.45")
)
)
(core instance (;0;) (instantiate 0))
(alias export 0 "foo" (type (;1;)))
(core instance (;0;) (instantiate 0))
(type (;2;) (func (result 1)))
(alias core export 0 "bar#foo" (core func (;0;)))
(func (;0;) (type 2) (canon lift (core func 0)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
(processed-by "my-fake-bindgen" "123.45")
)
)
(core instance (;0;) (instantiate 0))
(type (;0;) u8)
(core instance (;0;) (instantiate 0))
(type (;1;) (func (param "a" 0) (result 0)))
(alias core export 0 "foo:foo/foo#c" (core func (;0;)))
(func (;0;) (type 1) (canon lift (core func 0)))
Expand Down
34 changes: 17 additions & 17 deletions crates/wit-component/tests/components/exports/component.wat
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,28 @@
(processed-by "my-fake-bindgen" "123.45")
)
)
(type (;0;) (flags "a" "b" "c"))
(type (;1;) (variant (case "a") (case "b" string) (case "c" s64)))
(core instance (;0;) (instantiate 0))
(alias core export 0 "memory" (core memory (;0;)))
(type (;0;) (func))
(type (;2;) (func))
(alias core export 0 "a" (core func (;0;)))
(alias core export 0 "cabi_realloc" (core func (;1;)))
(func (;0;) (type 0) (canon lift (core func 0)))
(func (;0;) (type 2) (canon lift (core func 0)))
(export (;1;) "a" (func 0))
(type (;1;) (func (param "a" s8) (param "b" s16) (param "c" s32) (param "d" s64) (result string)))
(type (;3;) (func (param "a" s8) (param "b" s16) (param "c" s32) (param "d" s64) (result string)))
(alias core export 0 "b" (core func (;2;)))
(alias core export 0 "cabi_post_b" (core func (;3;)))
(func (;2;) (type 1) (canon lift (core func 2) (memory 0) string-encoding=utf8 (post-return 3)))
(func (;2;) (type 3) (canon lift (core func 2) (memory 0) string-encoding=utf8 (post-return 3)))
(export (;3;) "b" (func 2))
(type (;2;) (tuple s8 s16 s32 s64))
(type (;3;) (func (result 2)))
(type (;4;) (tuple s8 s16 s32 s64))
(type (;5;) (func (result 4)))
(alias core export 0 "c" (core func (;4;)))
(func (;4;) (type 3) (canon lift (core func 4) (memory 0)))
(func (;4;) (type 5) (canon lift (core func 4) (memory 0)))
(export (;5;) "c" (func 4))
(type (;4;) (flags "a" "b" "c"))
(type (;5;) (func (param "x" 4)))
(type (;6;) (func (param "x" 0)))
(alias core export 0 "bar#a" (core func (;5;)))
(func (;6;) (type 5) (canon lift (core func 5)))
(func (;6;) (type 6) (canon lift (core func 5)))
(component (;0;)
(type (;0;) (flags "a" "b" "c"))
(import "import-type-x" (type (;1;) (eq 0)))
Expand All @@ -91,19 +92,18 @@
)
(instance (;0;) (instantiate 0
(with "import-func-a" (func 6))
(with "import-type-x" (type 4))
(with "import-type-x" (type 0))
)
)
(export (;1;) "bar" (instance 0))
(type (;6;) (func))
(type (;7;) (func))
(alias core export 0 "foo#a" (core func (;6;)))
(func (;7;) (type 6) (canon lift (core func 6)))
(type (;7;) (variant (case "a") (case "b" string) (case "c" s64)))
(type (;8;) (func (param "x" string) (result 7)))
(func (;7;) (type 7) (canon lift (core func 6)))
(type (;8;) (func (param "x" string) (result 1)))
(alias core export 0 "foo#b" (core func (;7;)))
(alias core export 0 "cabi_post_foo#b" (core func (;8;)))
(func (;8;) (type 8) (canon lift (core func 7) (memory 0) (realloc 1) string-encoding=utf8 (post-return 8)))
(type (;9;) (func (param "x" 7) (result string)))
(type (;9;) (func (param "x" 1) (result string)))
(alias core export 0 "foo#c" (core func (;9;)))
(alias core export 0 "cabi_post_foo#c" (core func (;10;)))
(func (;9;) (type 9) (canon lift (core func 9) (memory 0) (realloc 1) string-encoding=utf8 (post-return 10)))
Expand All @@ -129,7 +129,7 @@
(with "import-func-a" (func 7))
(with "import-func-b" (func 8))
(with "import-func-c" (func 9))
(with "import-type-x" (type 7))
(with "import-type-x" (type 1))
)
)
(export (;3;) "foo" (instance 2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
(processed-by "my-fake-bindgen" "123.45")
)
)
(type (;3;) (list string))
(type (;4;) (variant (case "num" u16) (case "strs" 3)))
(type (;5;) (variant (case "strs" 3)))
(core instance (;0;) (instantiate 0))
(component (;0;)
(type (;0;) (list string))
Expand Down
48 changes: 24 additions & 24 deletions crates/wit-component/tests/components/lift-options/component.wat
Original file line number Diff line number Diff line change
Expand Up @@ -97,37 +97,37 @@
(processed-by "my-fake-bindgen" "123.45")
)
)
(type (;0;) (record (field "s" string)))
(type (;1;) (record (field "s" u32)))
(type (;2;) (variant (case "s" string)))
(type (;3;) (variant (case "s" u32)))
(core instance (;0;) (instantiate 0))
(alias core export 0 "memory" (core memory (;0;)))
(type (;0;) (func))
(type (;4;) (func))
(alias core export 0 "foo:foo/my-default#a" (core func (;0;)))
(alias core export 0 "cabi_realloc" (core func (;1;)))
(func (;0;) (type 0) (canon lift (core func 0)))
(type (;1;) (list string))
(type (;2;) (func (param "x" 1)))
(func (;0;) (type 4) (canon lift (core func 0)))
(type (;5;) (list string))
(type (;6;) (func (param "x" 5)))
(alias core export 0 "foo:foo/my-default#b" (core func (;2;)))
(func (;1;) (type 2) (canon lift (core func 2) (memory 0) (realloc 1) string-encoding=utf8))
(type (;3;) (record (field "s" string)))
(type (;4;) (func (param "x" 3)))
(func (;1;) (type 6) (canon lift (core func 2) (memory 0) (realloc 1) string-encoding=utf8))
(type (;7;) (func (param "x" 0)))
(alias core export 0 "foo:foo/my-default#c" (core func (;3;)))
(func (;2;) (type 4) (canon lift (core func 3) (memory 0) (realloc 1) string-encoding=utf8))
(type (;5;) (variant (case "s" string)))
(type (;6;) (func (param "x" 5)))
(func (;2;) (type 7) (canon lift (core func 3) (memory 0) (realloc 1) string-encoding=utf8))
(type (;8;) (func (param "x" 2)))
(alias core export 0 "foo:foo/my-default#d" (core func (;4;)))
(func (;3;) (type 6) (canon lift (core func 4) (memory 0) (realloc 1) string-encoding=utf8))
(type (;7;) (record (field "s" u32)))
(type (;8;) (func (param "x" 7)))
(func (;3;) (type 8) (canon lift (core func 4) (memory 0) (realloc 1) string-encoding=utf8))
(type (;9;) (func (param "x" 1)))
(alias core export 0 "foo:foo/my-default#e" (core func (;5;)))
(func (;4;) (type 8) (canon lift (core func 5)))
(type (;9;) (variant (case "s" u32)))
(type (;10;) (func (param "x" 9)))
(func (;4;) (type 9) (canon lift (core func 5)))
(type (;10;) (func (param "x" 3)))
(alias core export 0 "foo:foo/my-default#f" (core func (;6;)))
(func (;5;) (type 10) (canon lift (core func 6)))
(type (;11;) (list 3))
(type (;11;) (list 0))
(type (;12;) (func (param "x" 11)))
(alias core export 0 "foo:foo/my-default#g" (core func (;7;)))
(func (;6;) (type 12) (canon lift (core func 7) (memory 0) (realloc 1) string-encoding=utf8))
(type (;13;) (list 5))
(type (;13;) (list 2))
(type (;14;) (func (param "x" 13)))
(alias core export 0 "foo:foo/my-default#h" (core func (;8;)))
(func (;7;) (type 14) (canon lift (core func 8) (memory 0) (realloc 1) string-encoding=utf8))
Expand All @@ -153,11 +153,11 @@
(type (;22;) (func (result u32)))
(alias core export 0 "foo:foo/my-default#n" (core func (;16;)))
(func (;13;) (type 22) (canon lift (core func 16)))
(type (;23;) (func (result 5)))
(type (;23;) (func (result 2)))
(alias core export 0 "foo:foo/my-default#o" (core func (;17;)))
(alias core export 0 "cabi_post_foo:foo/my-default#o" (core func (;18;)))
(func (;14;) (type 23) (canon lift (core func 17) (memory 0) string-encoding=utf8 (post-return 18)))
(type (;24;) (list 9))
(type (;24;) (list 3))
(type (;25;) (func (result 24)))
(alias core export 0 "foo:foo/my-default#p" (core func (;19;)))
(alias core export 0 "cabi_post_foo:foo/my-default#p" (core func (;20;)))
Expand Down Expand Up @@ -273,10 +273,10 @@
(with "import-func-n" (func 13))
(with "import-func-o" (func 14))
(with "import-func-p" (func 15))
(with "import-type-r" (type 3))
(with "import-type-v" (type 5))
(with "import-type-r-no-string" (type 7))
(with "import-type-v-no-string" (type 9))
(with "import-type-r" (type 0))
(with "import-type-v" (type 2))
(with "import-type-r-no-string" (type 1))
(with "import-type-v-no-string" (type 3))
)
)
(export (;1;) "foo:foo/my-default" (instance 0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
(processed-by "my-fake-bindgen" "123.45")
)
)
(type (;0;) (record (field "f" u8)))
(type (;1;) (record (field "x" 0)))
(core instance (;0;) (instantiate 0))
(component (;0;)
(type (;0;) (record (field "f" u8)))
Expand All @@ -17,11 +19,11 @@
)
(instance (;0;) (instantiate 0))
(export (;1;) "foo:foo/name" (instance 0))
(type (;0;) (func))
(type (;2;) (func))
(alias core export 0 "name#a" (core func (;0;)))
(func (;0;) (type 0) (canon lift (core func 0)))
(alias export 1 "r1" (type (;1;)))
(alias export 1 "r2" (type (;2;)))
(func (;0;) (type 2) (canon lift (core func 0)))
(alias export 1 "r1" (type (;3;)))
(alias export 1 "r2" (type (;4;)))
(component (;1;)
(type (;0;) (record (field "f" u8)))
(import "import-type-r1" (type (;1;) (eq 0)))
Expand All @@ -35,8 +37,8 @@
)
(instance (;2;) (instantiate 1
(with "import-func-a" (func 0))
(with "import-type-r1" (type 1))
(with "import-type-r2" (type 2))
(with "import-type-r1" (type 3))
(with "import-type-r2" (type 4))
)
)
(export (;3;) "name" (instance 2))
Expand Down
Loading