-
Notifications
You must be signed in to change notification settings - Fork 45
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
Registry tooling and wac #129
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
# Build artifacts from examples | ||
**/jco/bindings | ||
**/examples/**/*.wasm | ||
.DS_Store |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,12 +1,29 @@ | ||||||
# Building a Calculator of Wasm Components | ||||||
|
||||||
This tutorial walks through how to compose a component to build a Wasm calculator. | ||||||
It is a rich example of using multiple components, targeting distinct worlds described across multiple WIT packages | ||||||
|
||||||
The first package consists of addition operations | ||||||
|
||||||
```wit | ||||||
//adder.wit | ||||||
package component-book:[email protected]; | ||||||
|
||||||
interface add { | ||||||
add: func(a: u32, b: u32) -> u32; | ||||||
} | ||||||
|
||||||
world adder { | ||||||
export add; | ||||||
} | ||||||
``` | ||||||
|
||||||
The WIT package for the calculator consists of a world for each mathematical operator | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
add an `op` enum that delineates each operator. The following example interface only | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
has an `add` operation: | ||||||
|
||||||
```wit | ||||||
package docs:[email protected]; | ||||||
package component-book:[email protected]; | ||||||
|
||||||
interface calculate { | ||||||
enum op { | ||||||
|
@@ -15,17 +32,9 @@ interface calculate { | |||||
eval-expression: func(op: op, x: u32, y: u32) -> u32; | ||||||
} | ||||||
|
||||||
interface add { | ||||||
add: func(a: u32, b: u32) -> u32; | ||||||
} | ||||||
|
||||||
world adder { | ||||||
export add; | ||||||
} | ||||||
|
||||||
world calculator { | ||||||
export calculate; | ||||||
import add; | ||||||
import component-book:adder/add@0.1.0; | ||||||
} | ||||||
``` | ||||||
|
||||||
|
@@ -43,6 +52,12 @@ wasm-tools compose calculator/target/wasm32-wasi/release/calculator.wasm -d adde | |||||
wasm-tools compose command/target/wasm32-wasi/release/command.wasm -d composed.wasm -o command.wasm | ||||||
``` | ||||||
|
||||||
You can also use `wac` instead of `wasm-tools compose` if you would like to fetch these components from a registry instead: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense for us to remove |
||||||
|
||||||
``` | ||||||
wac plug component-book:calculator-impl --plug component-book:adder-impl -o composed.wasm && wac plug component-book:command-impl --plug ./composed.wasm -o command.wasm | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: let's break up the invocations on to two lines for readability. |
||||||
``` | ||||||
|
||||||
Now, run the component with wasmtime: | ||||||
|
||||||
```sh | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,9 @@ | |
#[allow(dead_code)] | ||
pub mod exports { | ||
#[allow(dead_code)] | ||
pub mod docs { | ||
pub mod component_book { | ||
#[allow(dead_code)] | ||
pub mod calculator { | ||
pub mod adder { | ||
#[allow(dead_code, clippy::all)] | ||
pub mod add { | ||
#[used] | ||
|
@@ -27,17 +27,17 @@ pub mod exports { | |
} | ||
#[doc(hidden)] | ||
|
||
macro_rules! __export_docs_calculator_add_0_1_0_cabi{ | ||
macro_rules! __export_component_book_adder_add_0_1_0_cabi{ | ||
($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = { | ||
|
||
#[export_name = "docs:calculator/[email protected]#add"] | ||
#[export_name = "component-book:adder/[email protected]#add"] | ||
unsafe extern "C" fn export_add(arg0: i32,arg1: i32,) -> i32 { | ||
$($path_to_types)*::_export_add_cabi::<$ty>(arg0, arg1) | ||
} | ||
};); | ||
} | ||
#[doc(hidden)] | ||
pub(crate) use __export_docs_calculator_add_0_1_0_cabi; | ||
pub(crate) use __export_component_book_adder_add_0_1_0_cabi; | ||
} | ||
} | ||
} | ||
|
@@ -142,7 +142,7 @@ mod _rt { | |
macro_rules! __export_adder_impl { | ||
($ty:ident) => (self::export!($ty with_types_in self);); | ||
($ty:ident with_types_in $($path_to_types_root:tt)*) => ( | ||
$($path_to_types_root)*::exports::docs::calculator::add::__export_docs_calculator_add_0_1_0_cabi!($ty with_types_in $($path_to_types_root)*::exports::docs::calculator::add); | ||
$($path_to_types_root)*::exports::component_book::adder::add::__export_component_book_adder_add_0_1_0_cabi!($ty with_types_in $($path_to_types_root)*::exports::component_book::adder::add); | ||
) | ||
} | ||
#[doc(inline)] | ||
|
@@ -151,12 +151,12 @@ pub(crate) use __export_adder_impl as export; | |
#[cfg(target_arch = "wasm32")] | ||
#[link_section = "component-type:wit-bindgen:0.24.0:adder:encoded world"] | ||
#[doc(hidden)] | ||
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 213] = *b"\ | ||
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07Z\x01A\x02\x01A\x02\x01\ | ||
B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x04\x01\x19docs:calculator/add@\ | ||
0.1.0\x05\0\x04\x01\x1bdocs:calculator/[email protected]\x04\0\x0b\x0b\x01\0\x05adder\x03\ | ||
\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.202.0\x10wit-\ | ||
bindgen-rust\x060.24.0"; | ||
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 223] = *b"\ | ||
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07d\x01A\x02\x01A\x02\x01\ | ||
B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x04\x01\x1ecomponent-book:adder\ | ||
/add@0.1.0\x05\0\x04\x01\x20component-book:adder/[email protected]\x04\0\x0b\x0b\x01\0\ | ||
\x05adder\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.\ | ||
202.0\x10wit-bindgen-rust\x060.24.0"; | ||
|
||
#[inline(never)] | ||
#[doc(hidden)] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,12 @@ wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] } | |
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: leave this line |
||
[package.metadata.component] | ||
package = "docs:calculator" | ||
|
||
[package.metadata.component.target.dependencies] | ||
"docs:adder" = { path = "../wit/local/adder.wit" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't this come from a registry instead of from a local definition? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely! I guess I was thinking that there's less to get wrong when using a registry than a local path (it's pretty easy to have a mistake in your path to wit files), so it might be more valuable to include an example of the tricker use case. Maybe I'll include two examples... one that uses the registry and one that points to local files. |
||
|
||
[package.metadata.component.target] | ||
path = "../wit/calculator.wit" | ||
path = "../wit/local/calculator.wit" | ||
world = "calculator" | ||
|
||
[package.metadata.component.dependencies] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
// Generated by `wit-bindgen` 0.24.0. DO NOT EDIT! | ||
// Options used: | ||
#[allow(dead_code)] | ||
pub mod docs { | ||
pub mod component_book { | ||
#[allow(dead_code)] | ||
pub mod calculator { | ||
pub mod adder { | ||
#[allow(dead_code, clippy::all)] | ||
pub mod add { | ||
#[used] | ||
|
@@ -16,7 +16,7 @@ pub mod docs { | |
pub fn add(a: u32, b: u32) -> u32 { | ||
unsafe { | ||
#[cfg(target_arch = "wasm32")] | ||
#[link(wasm_import_module = "docs:calculator/[email protected]")] | ||
#[link(wasm_import_module = "component-book:adder/[email protected]")] | ||
extern "C" { | ||
#[link_name = "add"] | ||
fn wit_import(_: i32, _: i32) -> i32; | ||
|
@@ -36,7 +36,7 @@ pub mod docs { | |
#[allow(dead_code)] | ||
pub mod exports { | ||
#[allow(dead_code)] | ||
pub mod docs { | ||
pub mod component_book { | ||
#[allow(dead_code)] | ||
pub mod calculator { | ||
#[allow(dead_code, clippy::all)] | ||
|
@@ -92,17 +92,17 @@ pub mod exports { | |
} | ||
#[doc(hidden)] | ||
|
||
macro_rules! __export_docs_calculator_calculate_0_1_0_cabi{ | ||
macro_rules! __export_component_book_calculator_calculate_0_1_0_cabi{ | ||
($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = { | ||
|
||
#[export_name = "docs:calculator/[email protected]#eval-expression"] | ||
#[export_name = "component-book:calculator/[email protected]#eval-expression"] | ||
unsafe extern "C" fn export_eval_expression(arg0: i32,arg1: i32,arg2: i32,) -> i32 { | ||
$($path_to_types)*::_export_eval_expression_cabi::<$ty>(arg0, arg1, arg2) | ||
} | ||
};); | ||
} | ||
#[doc(hidden)] | ||
pub(crate) use __export_docs_calculator_calculate_0_1_0_cabi; | ||
pub(crate) use __export_component_book_calculator_calculate_0_1_0_cabi; | ||
} | ||
} | ||
} | ||
|
@@ -207,7 +207,7 @@ mod _rt { | |
macro_rules! __export_calculator_impl { | ||
($ty:ident) => (self::export!($ty with_types_in self);); | ||
($ty:ident with_types_in $($path_to_types_root:tt)*) => ( | ||
$($path_to_types_root)*::exports::docs::calculator::calculate::__export_docs_calculator_calculate_0_1_0_cabi!($ty with_types_in $($path_to_types_root)*::exports::docs::calculator::calculate); | ||
$($path_to_types_root)*::exports::component_book::calculator::calculate::__export_component_book_calculator_calculate_0_1_0_cabi!($ty with_types_in $($path_to_types_root)*::exports::component_book::calculator::calculate); | ||
) | ||
} | ||
#[doc(inline)] | ||
|
@@ -216,14 +216,14 @@ pub(crate) use __export_calculator_impl as export; | |
#[cfg(target_arch = "wasm32")] | ||
#[link_section = "component-type:wit-bindgen:0.24.0:calculator:encoded world"] | ||
#[doc(hidden)] | ||
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 313] = *b"\ | ||
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xb8\x01\x01A\x02\x01\ | ||
A\x04\x01B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x03\x01\x19docs:calcul\ | ||
ator/[email protected]\x05\0\x01B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02op\ | ||
\x01\x01xy\x01yy\0y\x04\0\x0feval-expression\x01\x02\x04\x01\x1fdocs:calculator/\ | ||
[email protected]\x05\x01\x04\x01\x20docs:calculator/calculator@0.1.0\x04\0\x0b\x10\ | ||
\x01\0\x0acalculator\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-comp\ | ||
onent\x070.202.0\x10wit-bindgen-rust\x060.24.0"; | ||
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 338] = *b"\ | ||
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xd1\x01\x01A\x02\x01\ | ||
A\x04\x01B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x03\x01\x1ecomponent-b\ | ||
ook:adder/[email protected]\x05\0\x01B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02\ | ||
op\x01\x01xy\x01yy\0y\x04\0\x0feval-expression\x01\x02\x04\x01)component-book:ca\ | ||
lculator/[email protected]\x05\x01\x04\x01*component-book:calculator/calculator@0.\ | ||
1.0\x04\0\x0b\x10\x01\0\x0acalculator\x03\0\0\0G\x09producers\x01\x0cprocessed-b\ | ||
y\x02\x0dwit-component\x070.202.0\x10wit-bindgen-rust\x060.24.0"; | ||
|
||
#[inline(never)] | ||
#[doc(hidden)] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,15 @@ version = "0.1.0" | |
edition = "2021" | ||
|
||
[package.metadata.component] | ||
package = "docs:calculator" | ||
package = "component-book:calculator" | ||
|
||
[package.metadata.component.target] | ||
path = "../wit/calculator.wit" | ||
path = "../wit/local/calculator.wit" | ||
world = "app" | ||
|
||
[package.metadata.component.target.dependencies] | ||
"docs:adder" = { path = "../wit/local/adder.wit" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
|
||
[dependencies] | ||
anyhow = "1" | ||
clap = { version = "4.3.19", features = ["derive"] } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
// Generated by `wit-bindgen` 0.24.0. DO NOT EDIT! | ||
// Options used: | ||
#[allow(dead_code)] | ||
pub mod docs { | ||
pub mod component_book { | ||
#[allow(dead_code)] | ||
pub mod calculator { | ||
#[allow(dead_code, clippy::all)] | ||
|
@@ -43,7 +43,7 @@ pub mod docs { | |
pub fn eval_expression(op: Op, x: u32, y: u32) -> u32 { | ||
unsafe { | ||
#[cfg(target_arch = "wasm32")] | ||
#[link(wasm_import_module = "docs:calculator/[email protected]")] | ||
#[link(wasm_import_module = "component-book:calculator/[email protected]")] | ||
extern "C" { | ||
#[link_name = "eval-expression"] | ||
fn wit_import(_: i32, _: i32, _: i32) -> i32; | ||
|
@@ -136,13 +136,13 @@ mod _rt { | |
#[cfg(target_arch = "wasm32")] | ||
#[link_section = "component-type:wit-bindgen:0.24.0:app:encoded world"] | ||
#[doc(hidden)] | ||
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 246] = *b"\ | ||
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07}\x01A\x02\x01A\x02\x01\ | ||
B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02op\x01\x01xy\x01yy\0y\x04\0\ | ||
\x0feval-expression\x01\x02\x03\x01\x1fdocs:calculator/[email protected]\x05\0\x04\ | ||
\x01\x19docs:calculator/[email protected]\x04\0\x0b\x09\x01\0\x03app\x03\0\0\0G\x09produ\ | ||
cers\x01\x0cprocessed-by\x02\x0dwit-component\x070.202.0\x10wit-bindgen-rust\x06\ | ||
0.24.0"; | ||
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 267] = *b"\ | ||
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\x91\x01\x01A\x02\x01\ | ||
A\x02\x01B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02op\x01\x01xy\x01y\ | ||
y\0y\x04\0\x0feval-expression\x01\x02\x03\x01)component-book:calculator/calculat\ | ||
[email protected]\x05\0\x04\x01#component-book:calculator/[email protected]\x04\0\x0b\x09\x01\0\x03\ | ||
app\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.202.0\x10\ | ||
wit-bindgen-rust\x060.24.0"; | ||
|
||
#[inline(never)] | ||
#[doc(hidden)] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package component-book:[email protected]; | ||
|
||
interface add { | ||
add: func(a: u32, b: u32) -> u32; | ||
} | ||
|
||
world adder { | ||
export add; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
version = "0.1.0" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package docs:[email protected]; | ||
package component-book:[email protected]; | ||
|
||
interface calculate { | ||
enum op { | ||
|
@@ -7,17 +7,10 @@ interface calculate { | |
eval-expression: func(op: op, x: u32, y: u32) -> u32; | ||
} | ||
|
||
interface add { | ||
add: func(a: u32, b: u32) -> u32; | ||
} | ||
|
||
world adder { | ||
export add; | ||
} | ||
|
||
world calculator { | ||
import component-book:adder/[email protected]; | ||
export calculate; | ||
import add; | ||
} | ||
|
||
world app { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# This file is automatically generated by wit. | ||
# It is not intended for manual editing. | ||
version = 1 | ||
|
||
[[package]] | ||
name = "component-book:adder" | ||
|
||
[[package.version]] | ||
requirement = "^0.1.0" | ||
version = "0.1.0" | ||
digest = "sha256:6e82c02aac69186d371c2324e6c266a5b03a0a45015980a063e48bd56fe46f66" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
"component-book:adder" = "0.1.0" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package component-book:[email protected]; | ||
|
||
interface add { | ||
add: func(a: u32, b: u32) -> u32; | ||
} | ||
|
||
world adder { | ||
export add; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.