Allow type declarations at package scope - #699
Conversation
| ```wat | ||
| (component | ||
| (type (export "point") (record (field "x" u32) (field "y" u32))) | ||
| (type (export "api") (component | ||
| (export "local:demo/api" (instance | ||
| (type $point (record (field "x" u32) (field "y" u32))) | ||
| (export "move-to" (func (param "p" $point))) | ||
| )) | ||
| )) | ||
| ) | ||
| ``` |
There was a problem hiding this comment.
For myself this is the main point of question for me of how these top-level types would be encoded in WIT. This component as-is is not valid (doesn't pass wasm-tools validate), and I assume that you don't want to change the validation rules for components, so could this be updated with an encoding that's valid?
There was a problem hiding this comment.
Great question! Here's one idea I think might work:
For WIT interfaces like the above, we treat these dependencies on package-level types just like use of an interface that contains the analogous type definition, we just strip out the wrapping instance type:
(component $C
(type $Point (export "point") (record (field "x" u32) (field "y" u32)))
(type (export "api") (component
(import "local:demo/point" (type $Point' (eq $Point)))
(export "local:demo/api" (instance
(export "move-to" (func (param "p" $Point')))
))
))
)and then for the analogous WIT world (that exports move-to), we move the (import "local:demo/point" (type ...)) inside the inner component type so that it works like a type definition inside an imported interface, just not wrapped in an instance type:
(component $C
(type $Point (export "point") (record (field "x" u32) (field "y" u32)))
(type (export "api") (component
(export "local:demo/api" (component
(import "local:demo/point" (type $Point' (eq $Point)))
(export "move-to" (func (param "p" $Point')))
))
))
)Sharing a type vocabulary across interfaces requires inventing an interface to hold it, and that container reaches the artifact as an instance import that nothing calls. Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
f3c7c23 to
126b12a
Compare
This PR allows
type,record,variant,enumandflagsdeclarations at package scope in WIT, as proposed in #694. Today, sharing a type vocabulary across interfaces requires inventing an interface to hold it, and that container reaches the artifact as an instance import that nothing calls.resourceis deliberately excluded. Resources carry identity rather than structure, so an interface is what gives a resource its identity, and package scope has nothing to offer there. That is why the grammar names apackage-typedef-itemrather than reusingtypedef-item, which already includesresource-item.