Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
macovedj committed May 15, 2024
1 parent a0b7292 commit 10e3dc4
Show file tree
Hide file tree
Showing 23 changed files with 247 additions and 169 deletions.
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

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

```wit
package bytecode-alliance:[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:

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

```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 = "bytecode-alliance: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 = "bytecode_alliance: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::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);
$($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\x19bytecode_alliance:calculator/add@\
0.1.0\x05\0\x04\x01\x1bbytecode_alliance: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::bytecode_alliance::calculator::add::Guest;
use crate::bindings::exports::component_book::adder::add::Guest;

struct Component;

Expand Down
10 changes: 5 additions & 5 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"]

[package.metadata.component]
package = "bytecode-alliance:calculator"
package = "docs:calculator"

[package.metadata.component.target.dependencies]
"docs: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]
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 = "bytecode_alliance: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 = "bytecode_alliance: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::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);
$($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\x19bytecode_alliance: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\x1fbytecode_alliance:calculator/\
[email protected]\x05\x01\x04\x01\x20bytecode_alliance: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::bytecode_alliance::calculator::calculate::{Guest, Op};
use bindings::exports::component_book::calculator::calculate::{Guest, Op};

// Bring the imported add function into scope
use bindings::bytecode_alliance::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 = "bytecode-alliance: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" }

[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 = "bytecode_alliance: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\x1fbytecode_alliance:calculator/[email protected]\x05\0\x04\
\x01\bytecode_alliance: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::bytecode_alliance::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:adder@0.1.0;

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 bytecode-alliance:calculator@0.1.0;
package component-book:calculator@0.1.0;

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/add@0.1.0;
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:adder@0.1.0;

interface add {
add: func(a: u32, b: u32) -> u32;
}

world adder {
export add;
}
Loading

0 comments on commit 10e3dc4

Please sign in to comment.