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

Registry tooling and wac #129

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
# Build artifacts from examples
**/jco/bindings
**/examples/**/*.wasm
.DS_Store
46 changes: 31 additions & 15 deletions component-model/examples/tutorial/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
# Building a Calculator of Wasm Components

This tutorial walks through how to compose a component to build a Wasm calculator.
The WIT package for the calculator consists of a world for each mathematical operator
add an `op` enum that delineates each operator. The following example interface only
has an `add` operation:
This example uses multiple components that target distinct worlds defined across multiple WIT packages.

```wit
package docs:[email protected];
The first package consists of addition operations

interface calculate {
enum op {
add,
}
eval-expression: func(op: op, x: u32, y: u32) -> u32;
}
```wit
//adder.wit
package component-book:[email protected];

interface add {
add: func(a: u32, b: u32) -> u32;
Expand All @@ -22,25 +16,47 @@ interface add {
world adder {
export add;
}
```

The second WIT package defines the calculator which consists of a world for each mathematical operator
and an `op` enum that delineates each operator. The following example interface only
has an `add` operation:

```wit
package component-book:[email protected];

interface calculate {
enum op {
add,
}
eval-expression: func(op: op, x: u32, y: u32) -> u32;
}

world calculator {
export calculate;
import add;
import component-book:adder/add@0.1.0;
}
```

To expand the exercise to add more components, add another operator world, expand the enum, and modify the `command` component to call it.

## Building and running the example

To compose a calculator component with an add operator, run the following:
To compose a calculator component with an add operator, you'll first need to install [wac](https://github.com/bytecodealliance/wac), and then you can run the following:

```sh
(cd calculator && cargo component build --release)
(cd adder && cargo component build --release)
(cd command && cargo component build --release)
wasm-tools compose calculator/target/wasm32-wasi/release/calculator.wasm -d adder/target/wasm32-wasi/release/adder.wasm -o composed.wasm
wasm-tools compose command/target/wasm32-wasi/release/command.wasm -d composed.wasm -o command.wasm
wac plug local/calculator/target/wasm32-wasi/release/calculator.wasm --plug local/adder/target/wasm32-wasi/release/adder.wasm -o composed.wasm
wac plug command/target/wasm32-wasi/release/command.wasm --plug composed.wasm -o command.wasm
```

For the `wac` commands, if you'd like to fetch example components from the registry for your composition, you can use the following instead:

```
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
```

Now, run the component with wasmtime:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
crate-type = ["cdylib"]

[package.metadata.component]
package = "docs:calculator"
package = "component-book:adder-impl"

[package.metadata.component.target]
path = "../wit/calculator.wit"
path = "../../wit/local/adder.wit"
world = "adder"

[package.metadata.component.dependencies]
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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;
}
}
}
Expand Down Expand Up @@ -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)]
Expand All @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod bindings;

use crate::bindings::exports::docs::calculator::add::Guest;
use crate::bindings::exports::component_book::adder::add::Guest;

struct Component;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
crate-type = ["cdylib"]

[package.metadata.component]
package = "docs:calculator"
package = "component-book:calculator-impl"

[package.metadata.component.target.dependencies]
"component-book:adder" = { path = "../../wit/local/adder.wit" }

[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]
Expand All @@ -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;
Expand All @@ -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)]
Expand Down Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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)]
Expand All @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod bindings;

use bindings::exports::docs::calculator::calculate::{Guest, Op};
use bindings::exports::component_book::calculator::calculate::{Guest, Op};

// Bring the imported add function into scope
use bindings::docs::calculator::add::add;
use bindings::component_book::adder::add::add;

struct Component;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ version = "0.1.0"
edition = "2021"

[package.metadata.component]
package = "docs:calculator"
package = "component-book:command-impl"

[package.metadata.component.target]
path = "../wit/calculator.wit"
path = "../../wit/local/calculator.wit"
world = "app"

[package.metadata.component.target.dependencies]
"component-model:adder" = { path = "../../wit/local/adder.wit" }

[dependencies]
anyhow = "1"
clap = { version = "4.3.19", features = ["derive"] }
Expand Down
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)]
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod bindings;
use clap::Parser;
use std::fmt;

use bindings::docs::calculator::{calculate, calculate::Op};
use bindings::component_book::calculator::{calculate, calculate::Op};

fn parse_operator(op: &str) -> anyhow::Result<Op> {
match op {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file is automatically generated by cargo-component.
# 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:2afffac0a89b4f6add89903754bb5a09a51378ef14f159283c1a6408abb43147"
25 changes: 25 additions & 0 deletions component-model/examples/tutorial/registry/adder-impl/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading