Create WebAssembly Command Components

In the “Learn How wasmtime Works with WebAssembly Modules and Components” Shortcut, I showed you some of the mechanics that go into a host application consuming services from WebAssembly components. You saw the host looking specifically for a method called add that took two integers and returned another one. The ability to define behavior and invoke it makes it possible for you to create whatever kind of plug-in interface you might like to build around these composable units. It would get a little old to have to go to all that work for each new interface though.

Remember, in WebAssembly Interface Type (WIT) files, a world is a coordinated set of imports and exports that define a particular shape of a component. Thanks to the standards process, we are starting to see reusable worlds that behave consistently and have common interfaces. WebAssembly command components are one such example. In this Shortcut, I am going to show you how to create them.

A command component adheres to the wasi:cli/command world, which looks like this:

package wasi:cli@0.2.0;
 
world command {
  include imports;
 
  export run;
}

Basically, the command component is provided with common interfaces through the include imports statement above. To avoid redundancy, they are not shown everywhere they are imported, but I will show you momentarily what is included. As per the command world definition, it also exports ...

Get Create WebAssembly Command Components now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.