Skip to content

Commit 9b5fa7d

Browse files
committed
address comments, local and registy examples, prod components instead of preview in examples
1 parent ddcf648 commit 9b5fa7d

File tree

34 files changed

+1083
-60
lines changed

34 files changed

+1083
-60
lines changed

component-model/examples/tutorial/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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
4+
This example uses multiple components that target distinct worlds defined across multiple WIT packages.
55

66
The first package consists of addition operations
77

@@ -18,8 +18,8 @@ world adder {
1818
}
1919
```
2020

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

2525
```wit
@@ -42,17 +42,17 @@ To expand the exercise to add more components, add another operator world, expan
4242

4343
## Building and running the example
4444

45-
To compose a calculator component with an add operator, run the following:
45+
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:
4646

4747
```sh
4848
(cd calculator && cargo component build --release)
4949
(cd adder && cargo component build --release)
5050
(cd command && cargo component build --release)
51-
wasm-tools compose calculator/target/wasm32-wasi/release/calculator.wasm -d adder/target/wasm32-wasi/release/adder.wasm -o composed.wasm
52-
wasm-tools compose command/target/wasm32-wasi/release/command.wasm -d composed.wasm -o command.wasm
51+
wac plug local/calculator/target/wasm32-wasi/release/calculator.wasm --plug local/adder/target/wasm32-wasi/release/adder.wasm -o composed.wasm
52+
wac plug command/target/wasm32-wasi/release/command.wasm --plug composed.wasm -o command.wasm
5353
```
5454

55-
You can also use `wac` instead of `wasm-tools compose` if you would like to fetch these components from a registry instead:
55+
For the `wac` commands, if you'd like to fetch example components from the registry for your composition, you can use the following instead:
5656

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

component-model/examples/tutorial/adder/Cargo.toml renamed to component-model/examples/tutorial/local/adder/Cargo.toml

Lines changed: 2 additions & 2 deletions
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 = "component-book:calculator"
14+
package = "component-book:adder-impl"
1515

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

2020
[package.metadata.component.dependencies]

component-model/examples/tutorial/calculator/Cargo.toml renamed to component-model/examples/tutorial/local/calculator/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
99

1010
[lib]
1111
crate-type = ["cdylib"]
12+
1213
[package.metadata.component]
13-
package = "docs:calculator"
14+
package = "component-book:calculator-impl"
1415

1516
[package.metadata.component.target.dependencies]
16-
"docs:adder" = { path = "../wit/local/adder.wit" }
17+
"component-book:adder" = { path = "../../wit/local/adder.wit" }
1718

1819
[package.metadata.component.target]
19-
path = "../wit/local/calculator.wit"
20+
path = "../../wit/local/calculator.wit"
2021
world = "calculator"

component-model/examples/tutorial/command/Cargo.toml renamed to component-model/examples/tutorial/local/command/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ version = "0.1.0"
44
edition = "2021"
55

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

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

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

1616
[dependencies]
1717
anyhow = "1"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file is automatically generated by cargo-component.
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:2afffac0a89b4f6add89903754bb5a09a51378ef14f159283c1a6408abb43147"

component-model/examples/tutorial/registry/adder-impl/Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "adder-impl"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
wit-bindgen-rt = { version = "0.25.0", features = ["bitflags"] }
10+
11+
[lib]
12+
crate-type = ["cdylib"]
13+
14+
[profile.release]
15+
codegen-units = 1
16+
opt-level = "s"
17+
debug = false
18+
strip = true
19+
lto = true
20+
21+
[package.metadata.component]
22+
package = "component-book:adder-impl"
23+
target = "component-book:adder/[email protected]"
24+
25+
[package.metadata.component.dependencies]
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Generated by `wit-bindgen` 0.24.0. DO NOT EDIT!
2+
// Options used:
3+
#[allow(dead_code)]
4+
pub mod exports {
5+
#[allow(dead_code)]
6+
pub mod component_book {
7+
#[allow(dead_code)]
8+
pub mod adder {
9+
#[allow(dead_code, clippy::all)]
10+
pub mod add {
11+
#[used]
12+
#[doc(hidden)]
13+
#[cfg(target_arch = "wasm32")]
14+
static __FORCE_SECTION_REF: fn() =
15+
super::super::super::super::__link_custom_section_describing_imports;
16+
use super::super::super::super::_rt;
17+
#[doc(hidden)]
18+
#[allow(non_snake_case)]
19+
pub unsafe fn _export_add_cabi<T: Guest>(arg0: i32, arg1: i32) -> i32 {
20+
#[cfg(target_arch = "wasm32")]
21+
_rt::run_ctors_once();
22+
let result0 = T::add(arg0 as u32, arg1 as u32);
23+
_rt::as_i32(result0)
24+
}
25+
pub trait Guest {
26+
fn add(a: u32, b: u32) -> u32;
27+
}
28+
#[doc(hidden)]
29+
30+
macro_rules! __export_component_book_adder_add_0_1_0_cabi{
31+
($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = {
32+
33+
#[export_name = "component-book:adder/[email protected]#add"]
34+
unsafe extern "C" fn export_add(arg0: i32,arg1: i32,) -> i32 {
35+
$($path_to_types)*::_export_add_cabi::<$ty>(arg0, arg1)
36+
}
37+
};);
38+
}
39+
#[doc(hidden)]
40+
pub(crate) use __export_component_book_adder_add_0_1_0_cabi;
41+
}
42+
}
43+
}
44+
}
45+
mod _rt {
46+
47+
#[cfg(target_arch = "wasm32")]
48+
pub fn run_ctors_once() {
49+
wit_bindgen_rt::run_ctors_once();
50+
}
51+
52+
pub fn as_i32<T: AsI32>(t: T) -> i32 {
53+
t.as_i32()
54+
}
55+
56+
pub trait AsI32 {
57+
fn as_i32(self) -> i32;
58+
}
59+
60+
impl<'a, T: Copy + AsI32> AsI32 for &'a T {
61+
fn as_i32(self) -> i32 {
62+
(*self).as_i32()
63+
}
64+
}
65+
66+
impl AsI32 for i32 {
67+
#[inline]
68+
fn as_i32(self) -> i32 {
69+
self as i32
70+
}
71+
}
72+
73+
impl AsI32 for u32 {
74+
#[inline]
75+
fn as_i32(self) -> i32 {
76+
self as i32
77+
}
78+
}
79+
80+
impl AsI32 for i16 {
81+
#[inline]
82+
fn as_i32(self) -> i32 {
83+
self as i32
84+
}
85+
}
86+
87+
impl AsI32 for u16 {
88+
#[inline]
89+
fn as_i32(self) -> i32 {
90+
self as i32
91+
}
92+
}
93+
94+
impl AsI32 for i8 {
95+
#[inline]
96+
fn as_i32(self) -> i32 {
97+
self as i32
98+
}
99+
}
100+
101+
impl AsI32 for u8 {
102+
#[inline]
103+
fn as_i32(self) -> i32 {
104+
self as i32
105+
}
106+
}
107+
108+
impl AsI32 for char {
109+
#[inline]
110+
fn as_i32(self) -> i32 {
111+
self as i32
112+
}
113+
}
114+
115+
impl AsI32 for usize {
116+
#[inline]
117+
fn as_i32(self) -> i32 {
118+
self as i32
119+
}
120+
}
121+
}
122+
123+
/// Generates `#[no_mangle]` functions to export the specified type as the
124+
/// root implementation of all generated traits.
125+
///
126+
/// For more information see the documentation of `wit_bindgen::generate!`.
127+
///
128+
/// ```rust
129+
/// # macro_rules! export{ ($($t:tt)*) => (); }
130+
/// # trait Guest {}
131+
/// struct MyType;
132+
///
133+
/// impl Guest for MyType {
134+
/// // ...
135+
/// }
136+
///
137+
/// export!(MyType);
138+
/// ```
139+
#[allow(unused_macros)]
140+
#[doc(hidden)]
141+
142+
macro_rules! __export_adder_impl {
143+
($ty:ident) => (self::export!($ty with_types_in self););
144+
($ty:ident with_types_in $($path_to_types_root:tt)*) => (
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);
146+
)
147+
}
148+
#[doc(inline)]
149+
pub(crate) use __export_adder_impl as export;
150+
151+
#[cfg(target_arch = "wasm32")]
152+
#[link_section = "component-type:wit-bindgen:0.24.0:adder:encoded world"]
153+
#[doc(hidden)]
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+
/[email protected]\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";
160+
161+
#[inline(never)]
162+
#[doc(hidden)]
163+
#[cfg(target_arch = "wasm32")]
164+
pub fn __link_custom_section_describing_imports() {
165+
wit_bindgen_rt::maybe_link_cabi_realloc();
166+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[allow(warnings)]
2+
mod bindings;
3+
4+
use bindings::exports::component_book::adder::add::Guest;
5+
6+
struct Component;
7+
8+
impl Guest for Component {
9+
fn add(a: u32, b: u32) -> u32 {
10+
a + b
11+
}
12+
}
13+
14+
bindings::export!(Component with_types_in bindings);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file is automatically generated by cargo-component.
2+
# It is not intended for manual editing.
3+
version = 1
4+
5+
[[package]]
6+
name = "component-book:calculator"
7+
8+
[[package.version]]
9+
requirement = "^0.1.0"
10+
version = "0.1.0"
11+
digest = "sha256:1f75d599bae47f96644c797b824de58f161e1da7e20461da4c881568b4fd6462"

component-model/examples/tutorial/registry/calculator-impl/Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "calculator-impl"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
wit-bindgen-rt = { version = "0.25.0", features = ["bitflags"] }
10+
11+
[lib]
12+
crate-type = ["cdylib"]
13+
14+
[profile.release]
15+
codegen-units = 1
16+
opt-level = "s"
17+
debug = false
18+
strip = true
19+
lto = true
20+
21+
[package.metadata.component]
22+
package = "component-book:calculator-impl"
23+
target = "component-book:calculator/[email protected]"
24+
25+
[package.metadata.component.dependencies]

0 commit comments

Comments
 (0)