Skip to content

Commit ddcf648

Browse files
committed
cleanup
1 parent a0b7292 commit ddcf648

File tree

23 files changed

+247
-169
lines changed

23 files changed

+247
-169
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
# Build artifacts from examples
77
**/jco/bindings
88
**/examples/**/*.wasm
9+
.DS_Store

component-model/examples/tutorial/README.md

+25-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
# Building a Calculator of Wasm Components
22

33
This tutorial walks through how to compose a component to build a Wasm calculator.
4+
It is a rich example of using multiple components, targeting distinct worlds described across multiple WIT packages
5+
6+
The first package consists of addition operations
7+
8+
```wit
9+
//adder.wit
10+
package component-book:[email protected];
11+
12+
interface add {
13+
add: func(a: u32, b: u32) -> u32;
14+
}
15+
16+
world adder {
17+
export add;
18+
}
19+
```
20+
421
The WIT package for the calculator consists of a world for each mathematical operator
522
add an `op` enum that delineates each operator. The following example interface only
623
has an `add` operation:
724

825
```wit
9-
package bytecode-alliance:[email protected];
26+
package component-book:[email protected];
1027
1128
interface calculate {
1229
enum op {
@@ -15,17 +32,9 @@ interface calculate {
1532
eval-expression: func(op: op, x: u32, y: u32) -> u32;
1633
}
1734
18-
interface add {
19-
add: func(a: u32, b: u32) -> u32;
20-
}
21-
22-
world adder {
23-
export add;
24-
}
25-
2635
world calculator {
2736
export calculate;
28-
import add;
37+
import component-book:adder/add@0.1.0;
2938
}
3039
```
3140

@@ -43,6 +52,12 @@ wasm-tools compose calculator/target/wasm32-wasi/release/calculator.wasm -d adde
4352
wasm-tools compose command/target/wasm32-wasi/release/command.wasm -d composed.wasm -o command.wasm
4453
```
4554

55+
You can also use `wac` instead of `wasm-tools compose` if you would like to fetch these components from a registry instead:
56+
57+
```
58+
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
59+
```
60+
4661
Now, run the component with wasmtime:
4762

4863
```sh

component-model/examples/tutorial/adder/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
1111
crate-type = ["cdylib"]
1212

1313
[package.metadata.component]
14-
package = "bytecode-alliance:calculator"
14+
package = "component-book:calculator"
1515

1616
[package.metadata.component.target]
17-
path = "../wit/calculator.wit"
17+
path = "../wit/adder/adder.wit"
1818
world = "adder"
1919

2020
[package.metadata.component.dependencies]

component-model/examples/tutorial/adder/src/bindings.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#[allow(dead_code)]
44
pub mod exports {
55
#[allow(dead_code)]
6-
pub mod docs {
6+
pub mod component_book {
77
#[allow(dead_code)]
8-
pub mod calculator {
8+
pub mod adder {
99
#[allow(dead_code, clippy::all)]
1010
pub mod add {
1111
#[used]
@@ -27,17 +27,17 @@ pub mod exports {
2727
}
2828
#[doc(hidden)]
2929

30-
macro_rules! __export_docs_calculator_add_0_1_0_cabi{
30+
macro_rules! __export_component_book_adder_add_0_1_0_cabi{
3131
($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = {
3232

33-
#[export_name = "bytecode_alliance:calculator/[email protected]#add"]
33+
#[export_name = "component-book:adder/[email protected]#add"]
3434
unsafe extern "C" fn export_add(arg0: i32,arg1: i32,) -> i32 {
3535
$($path_to_types)*::_export_add_cabi::<$ty>(arg0, arg1)
3636
}
3737
};);
3838
}
3939
#[doc(hidden)]
40-
pub(crate) use __export_docs_calculator_add_0_1_0_cabi;
40+
pub(crate) use __export_component_book_adder_add_0_1_0_cabi;
4141
}
4242
}
4343
}
@@ -142,7 +142,7 @@ mod _rt {
142142
macro_rules! __export_adder_impl {
143143
($ty:ident) => (self::export!($ty with_types_in self););
144144
($ty:ident with_types_in $($path_to_types_root:tt)*) => (
145-
$($path_to_types_root)*::exports::bytecode_alliance::calculator::add::__export_docs_calculator_add_0_1_0_cabi!($ty with_types_in $($path_to_types_root)*::exports::bytecode_alliance::calculator::add);
145+
$($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);
146146
)
147147
}
148148
#[doc(inline)]
@@ -151,12 +151,12 @@ pub(crate) use __export_adder_impl as export;
151151
#[cfg(target_arch = "wasm32")]
152152
#[link_section = "component-type:wit-bindgen:0.24.0:adder:encoded world"]
153153
#[doc(hidden)]
154-
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 213] = *b"\
155-
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07Z\x01A\x02\x01A\x02\x01\
156-
B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x04\x01\x19bytecode_alliance:calculator/add@\
157-
0.1.0\x05\0\x04\x01\x1bbytecode_alliance:calculator/[email protected]\x04\0\x0b\x0b\x01\0\x05adder\x03\
158-
\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.202.0\x10wit-\
159-
bindgen-rust\x060.24.0";
154+
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 223] = *b"\
155+
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07d\x01A\x02\x01A\x02\x01\
156+
B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x04\x01\x1ecomponent-book:adder\
157+
/add@0.1.0\x05\0\x04\x01\x20component-book:adder/[email protected]\x04\0\x0b\x0b\x01\0\
158+
\x05adder\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.\
159+
202.0\x10wit-bindgen-rust\x060.24.0";
160160

161161
#[inline(never)]
162162
#[doc(hidden)]

component-model/examples/tutorial/adder/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod bindings;
22

3-
use crate::bindings::exports::bytecode_alliance::calculator::add::Guest;
3+
use crate::bindings::exports::component_book::adder::add::Guest;
44

55
struct Component;
66

component-model/examples/tutorial/calculator/Cargo.toml

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
99

1010
[lib]
1111
crate-type = ["cdylib"]
12-
1312
[package.metadata.component]
14-
package = "bytecode-alliance:calculator"
13+
package = "docs:calculator"
14+
15+
[package.metadata.component.target.dependencies]
16+
"docs:adder" = { path = "../wit/local/adder.wit" }
1517

1618
[package.metadata.component.target]
17-
path = "../wit/calculator.wit"
19+
path = "../wit/local/calculator.wit"
1820
world = "calculator"
19-
20-
[package.metadata.component.dependencies]

component-model/examples/tutorial/calculator/src/bindings.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Generated by `wit-bindgen` 0.24.0. DO NOT EDIT!
22
// Options used:
33
#[allow(dead_code)]
4-
pub mod docs {
4+
pub mod component_book {
55
#[allow(dead_code)]
6-
pub mod calculator {
6+
pub mod adder {
77
#[allow(dead_code, clippy::all)]
88
pub mod add {
99
#[used]
@@ -16,7 +16,7 @@ pub mod docs {
1616
pub fn add(a: u32, b: u32) -> u32 {
1717
unsafe {
1818
#[cfg(target_arch = "wasm32")]
19-
#[link(wasm_import_module = "bytecode_alliance:calculator/[email protected]")]
19+
#[link(wasm_import_module = "component-book:adder/[email protected]")]
2020
extern "C" {
2121
#[link_name = "add"]
2222
fn wit_import(_: i32, _: i32) -> i32;
@@ -36,7 +36,7 @@ pub mod docs {
3636
#[allow(dead_code)]
3737
pub mod exports {
3838
#[allow(dead_code)]
39-
pub mod docs {
39+
pub mod component_book {
4040
#[allow(dead_code)]
4141
pub mod calculator {
4242
#[allow(dead_code, clippy::all)]
@@ -92,17 +92,17 @@ pub mod exports {
9292
}
9393
#[doc(hidden)]
9494

95-
macro_rules! __export_docs_calculator_calculate_0_1_0_cabi{
95+
macro_rules! __export_component_book_calculator_calculate_0_1_0_cabi{
9696
($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = {
9797

98-
#[export_name = "bytecode_alliance:calculator/[email protected]#eval-expression"]
98+
#[export_name = "component-book:calculator/[email protected]#eval-expression"]
9999
unsafe extern "C" fn export_eval_expression(arg0: i32,arg1: i32,arg2: i32,) -> i32 {
100100
$($path_to_types)*::_export_eval_expression_cabi::<$ty>(arg0, arg1, arg2)
101101
}
102102
};);
103103
}
104104
#[doc(hidden)]
105-
pub(crate) use __export_docs_calculator_calculate_0_1_0_cabi;
105+
pub(crate) use __export_component_book_calculator_calculate_0_1_0_cabi;
106106
}
107107
}
108108
}
@@ -207,7 +207,7 @@ mod _rt {
207207
macro_rules! __export_calculator_impl {
208208
($ty:ident) => (self::export!($ty with_types_in self););
209209
($ty:ident with_types_in $($path_to_types_root:tt)*) => (
210-
$($path_to_types_root)*::exports::bytecode_alliance::calculator::calculate::__export_docs_calculator_calculate_0_1_0_cabi!($ty with_types_in $($path_to_types_root)*::exports::bytecode_alliance::calculator::calculate);
210+
$($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);
211211
)
212212
}
213213
#[doc(inline)]
@@ -216,14 +216,14 @@ pub(crate) use __export_calculator_impl as export;
216216
#[cfg(target_arch = "wasm32")]
217217
#[link_section = "component-type:wit-bindgen:0.24.0:calculator:encoded world"]
218218
#[doc(hidden)]
219-
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 313] = *b"\
220-
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xb8\x01\x01A\x02\x01\
221-
A\x04\x01B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x03\x01\x19bytecode_alliance:calcul\
222-
ator/[email protected]\x05\0\x01B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02op\
223-
\x01\x01xy\x01yy\0y\x04\0\x0feval-expression\x01\x02\x04\x01\x1fbytecode_alliance:calculator/\
224-
[email protected]\x05\x01\x04\x01\x20bytecode_alliance:calculator/calculator@0.1.0\x04\0\x0b\x10\
225-
\x01\0\x0acalculator\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-comp\
226-
onent\x070.202.0\x10wit-bindgen-rust\x060.24.0";
219+
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 338] = *b"\
220+
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xd1\x01\x01A\x02\x01\
221+
A\x04\x01B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x03\x01\x1ecomponent-b\
222+
ook:adder/[email protected]\x05\0\x01B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02\
223+
op\x01\x01xy\x01yy\0y\x04\0\x0feval-expression\x01\x02\x04\x01)component-book:ca\
224+
lculator/[email protected]\x05\x01\x04\x01*component-book:calculator/calculator@0.\
225+
1.0\x04\0\x0b\x10\x01\0\x0acalculator\x03\0\0\0G\x09producers\x01\x0cprocessed-b\
226+
y\x02\x0dwit-component\x070.202.0\x10wit-bindgen-rust\x060.24.0";
227227

228228
#[inline(never)]
229229
#[doc(hidden)]

component-model/examples/tutorial/calculator/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
mod bindings;
22

3-
use bindings::exports::bytecode_alliance::calculator::calculate::{Guest, Op};
3+
use bindings::exports::component_book::calculator::calculate::{Guest, Op};
44

55
// Bring the imported add function into scope
6-
use bindings::bytecode_alliance::calculator::add::add;
6+
use bindings::component_book::adder::add::add;
77

88
struct Component;
99

component-model/examples/tutorial/command/Cargo.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ version = "0.1.0"
44
edition = "2021"
55

66
[package.metadata.component]
7-
package = "bytecode-alliance:calculator"
7+
package = "component-book:calculator"
88

99
[package.metadata.component.target]
10-
path = "../wit/calculator.wit"
10+
path = "../wit/local/calculator.wit"
1111
world = "app"
1212

13+
[package.metadata.component.target.dependencies]
14+
"docs:adder" = { path = "../wit/local/adder.wit" }
15+
1316
[dependencies]
1417
anyhow = "1"
1518
clap = { version = "4.3.19", features = ["derive"] }

component-model/examples/tutorial/command/src/bindings.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Generated by `wit-bindgen` 0.24.0. DO NOT EDIT!
22
// Options used:
33
#[allow(dead_code)]
4-
pub mod docs {
4+
pub mod component_book {
55
#[allow(dead_code)]
66
pub mod calculator {
77
#[allow(dead_code, clippy::all)]
@@ -43,7 +43,7 @@ pub mod docs {
4343
pub fn eval_expression(op: Op, x: u32, y: u32) -> u32 {
4444
unsafe {
4545
#[cfg(target_arch = "wasm32")]
46-
#[link(wasm_import_module = "bytecode_alliance:calculator/[email protected]")]
46+
#[link(wasm_import_module = "component-book:calculator/[email protected]")]
4747
extern "C" {
4848
#[link_name = "eval-expression"]
4949
fn wit_import(_: i32, _: i32, _: i32) -> i32;
@@ -136,13 +136,13 @@ mod _rt {
136136
#[cfg(target_arch = "wasm32")]
137137
#[link_section = "component-type:wit-bindgen:0.24.0:app:encoded world"]
138138
#[doc(hidden)]
139-
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 246] = *b"\
140-
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07}\x01A\x02\x01A\x02\x01\
141-
B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02op\x01\x01xy\x01yy\0y\x04\0\
142-
\x0feval-expression\x01\x02\x03\x01\x1fbytecode_alliance:calculator/[email protected]\x05\0\x04\
143-
\x01\bytecode_alliance:calculator/[email protected]\x04\0\x0b\x09\x01\0\x03app\x03\0\0\0G\x09produ\
144-
cers\x01\x0cprocessed-by\x02\x0dwit-component\x070.202.0\x10wit-bindgen-rust\x06\
145-
0.24.0";
139+
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 267] = *b"\
140+
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\x91\x01\x01A\x02\x01\
141+
A\x02\x01B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02op\x01\x01xy\x01y\
142+
y\0y\x04\0\x0feval-expression\x01\x02\x03\x01)component-book:calculator/calculat\
143+
[email protected]\x05\0\x04\x01#component-book:calculator/[email protected]\x04\0\x0b\x09\x01\0\x03\
144+
app\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.202.0\x10\
145+
wit-bindgen-rust\x060.24.0";
146146

147147
#[inline(never)]
148148
#[doc(hidden)]

component-model/examples/tutorial/command/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod bindings;
33
use clap::Parser;
44
use std::fmt;
55

6-
use bindings::bytecode_alliance::calculator::{calculate, calculate::Op};
6+
use bindings::component_book::calculator::{calculate, calculate::Op};
77

88
fn parse_operator(op: &str) -> anyhow::Result<Op> {
99
match op {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package component-book:adder@0.1.0;
2+
3+
interface add {
4+
add: func(a: u32, b: u32) -> u32;
5+
}
6+
7+
world adder {
8+
export add;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version = "0.1.0"

component-model/examples/tutorial/wit/calculator.wit component-model/examples/tutorial/wit/calculator/calculator.wit

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package bytecode-alliance:calculator@0.1.0;
1+
package component-book:calculator@0.1.0;
22

33
interface calculate {
44
enum op {
@@ -7,17 +7,10 @@ interface calculate {
77
eval-expression: func(op: op, x: u32, y: u32) -> u32;
88
}
99

10-
interface add {
11-
add: func(a: u32, b: u32) -> u32;
12-
}
13-
14-
world adder {
15-
export add;
16-
}
1710

1811
world calculator {
12+
import component-book:adder/add@0.1.0;
1913
export calculate;
20-
import add;
2114
}
2215

2316
world app {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file is automatically generated by wit.
2+
# It is not intended for manual editing.
3+
version = 1
4+
5+
[[package]]
6+
name = "component-book:adder"
7+
8+
[[package.version]]
9+
requirement = "^0.1.0"
10+
version = "0.1.0"
11+
digest = "sha256:6e82c02aac69186d371c2324e6c266a5b03a0a45015980a063e48bd56fe46f66"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version = "0.1.0"
2+
3+
[dependencies]
4+
"component-book:adder" = "0.1.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package component-book:adder@0.1.0;
2+
3+
interface add {
4+
add: func(a: u32, b: u32) -> u32;
5+
}
6+
7+
world adder {
8+
export add;
9+
}

0 commit comments

Comments
 (0)