Skip to content

Commit

Permalink
address comments, local and registy examples, prod components instead…
Browse files Browse the repository at this point in the history
… of preview in examples
  • Loading branch information
macovedj committed May 21, 2024
1 parent ddcf648 commit 5d60c5a
Show file tree
Hide file tree
Showing 34 changed files with 1,085 additions and 61 deletions.
17 changes: 9 additions & 8 deletions component-model/examples/tutorial/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 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
This example uses multiple components that target distinct worlds defined across multiple WIT packages.

The first package consists of addition operations

Expand All @@ -18,8 +18,8 @@ world adder {
}
```

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
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
Expand All @@ -42,20 +42,21 @@ To expand the exercise to add more components, add another operator world, expan

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

You can also use `wac` instead of `wasm-tools compose` if you would like to fetch these components from a registry instead:
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
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 = "component-book:calculator"
package = "component-book:adder-impl"

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

[package.metadata.component.dependencies]
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }

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

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

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

[package.metadata.component.target]
path = "../wit/local/calculator.wit"
path = "../../wit/local/calculator.wit"
world = "calculator"
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ version = "0.1.0"
edition = "2021"

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

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

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

[dependencies]
anyhow = "1"
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.

25 changes: 25 additions & 0 deletions component-model/examples/tutorial/registry/adder-impl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "adder-impl"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
wit-bindgen-rt = { version = "0.25.0", features = ["bitflags"] }

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

[profile.release]
codegen-units = 1
opt-level = "s"
debug = false
strip = true
lto = true

[package.metadata.component]
package = "component-book:adder-impl"
target = "component-book:adder/[email protected]"

[package.metadata.component.dependencies]
166 changes: 166 additions & 0 deletions component-model/examples/tutorial/registry/adder-impl/src/bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Generated by `wit-bindgen` 0.24.0. DO NOT EDIT!
// Options used:
#[allow(dead_code)]
pub mod exports {
#[allow(dead_code)]
pub mod component_book {
#[allow(dead_code)]
pub mod adder {
#[allow(dead_code, clippy::all)]
pub mod add {
#[used]
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
static __FORCE_SECTION_REF: fn() =
super::super::super::super::__link_custom_section_describing_imports;
use super::super::super::super::_rt;
#[doc(hidden)]
#[allow(non_snake_case)]
pub unsafe fn _export_add_cabi<T: Guest>(arg0: i32, arg1: i32) -> i32 {
#[cfg(target_arch = "wasm32")]
_rt::run_ctors_once();
let result0 = T::add(arg0 as u32, arg1 as u32);
_rt::as_i32(result0)
}
pub trait Guest {
fn add(a: u32, b: u32) -> u32;
}
#[doc(hidden)]

macro_rules! __export_component_book_adder_add_0_1_0_cabi{
($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = {

#[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_component_book_adder_add_0_1_0_cabi;
}
}
}
}
mod _rt {

#[cfg(target_arch = "wasm32")]
pub fn run_ctors_once() {
wit_bindgen_rt::run_ctors_once();
}

pub fn as_i32<T: AsI32>(t: T) -> i32 {
t.as_i32()
}

pub trait AsI32 {
fn as_i32(self) -> i32;
}

impl<'a, T: Copy + AsI32> AsI32 for &'a T {
fn as_i32(self) -> i32 {
(*self).as_i32()
}
}

impl AsI32 for i32 {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}

impl AsI32 for u32 {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}

impl AsI32 for i16 {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}

impl AsI32 for u16 {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}

impl AsI32 for i8 {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}

impl AsI32 for u8 {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}

impl AsI32 for char {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}

impl AsI32 for usize {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}
}

/// Generates `#[no_mangle]` functions to export the specified type as the
/// root implementation of all generated traits.
///
/// For more information see the documentation of `wit_bindgen::generate!`.
///
/// ```rust
/// # macro_rules! export{ ($($t:tt)*) => (); }
/// # trait Guest {}
/// struct MyType;
///
/// impl Guest for MyType {
/// // ...
/// }
///
/// export!(MyType);
/// ```
#[allow(unused_macros)]
#[doc(hidden)]

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::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)]
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; 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\
/[email protected]\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)]
#[cfg(target_arch = "wasm32")]
pub fn __link_custom_section_describing_imports() {
wit_bindgen_rt::maybe_link_cabi_realloc();
}
14 changes: 14 additions & 0 deletions component-model/examples/tutorial/registry/adder-impl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#[allow(warnings)]
mod bindings;

use bindings::exports::component_book::adder::add::Guest;

struct Component;

impl Guest for Component {
fn add(a: u32, b: u32) -> u32 {
a + b
}
}

bindings::export!(Component with_types_in bindings);
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:calculator"

[[package.version]]
requirement = "^0.1.0"
version = "0.1.0"
digest = "sha256:1f75d599bae47f96644c797b824de58f161e1da7e20461da4c881568b4fd6462"

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

Loading

0 comments on commit 5d60c5a

Please sign in to comment.