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 3 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
35 changes: 25 additions & 10 deletions component-model/examples/tutorial/README.md
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It is a rich example of using multiple components, targeting distinct worlds described across multiple WIT packages
This example uses multiple components that target distinct worlds defined 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The WIT package for the calculator consists of a world for each mathematical operator
The second WIT package defines the calculator which consists of a world for each mathematical operator

add an `op` enum that delineates each operator. The following example interface only
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
and an `op` enum that delineates each operator. The following example interface only

has an `add` operation:

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

interface calculate {
enum op {
Expand All @@ -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;
}
```

Expand All @@ -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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense for us to remove wasm-tools compose completely given bytecodealliance/wasm-tools#1564?


```
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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Down
4 changes: 2 additions & 2 deletions component-model/examples/tutorial/adder/Cargo.toml
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:calculator"

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

[package.metadata.component.dependencies]
24 changes: 12 additions & 12 deletions component-model/examples/tutorial/adder/src/bindings.rs
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
2 changes: 1 addition & 1 deletion component-model/examples/tutorial/adder/src/lib.rs
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
8 changes: 4 additions & 4 deletions component-model/examples/tutorial/calculator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }

[lib]
crate-type = ["cdylib"]

Copy link
Collaborator

Choose a reason for hiding this comment

The 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" }
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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]
32 changes: 16 additions & 16 deletions component-model/examples/tutorial/calculator/src/bindings.rs
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
4 changes: 2 additions & 2 deletions component-model/examples/tutorial/calculator/src/lib.rs
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
7 changes: 5 additions & 2 deletions component-model/examples/tutorial/command/Cargo.toml
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: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" }
Copy link
Collaborator

Choose a reason for hiding this comment

The 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"] }
Expand Down
18 changes: 9 additions & 9 deletions component-model/examples/tutorial/command/src/bindings.rs
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
2 changes: 1 addition & 1 deletion component-model/examples/tutorial/command/src/main.rs
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
9 changes: 9 additions & 0 deletions component-model/examples/tutorial/wit/adder/adder.wit
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;
}
1 change: 1 addition & 0 deletions component-model/examples/tutorial/wit/adder/wit.toml
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 {
Expand All @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions component-model/examples/tutorial/wit/calculator/wit.lock
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"
4 changes: 4 additions & 0 deletions component-model/examples/tutorial/wit/calculator/wit.toml
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"
9 changes: 9 additions & 0 deletions component-model/examples/tutorial/wit/local/adder.wit
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;
}
Loading