Skip to content

feat(isolatedwit): Moving the adder wit to a separate package. #136

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

Merged
merged 9 commits into from
May 28, 2024
Merged
34 changes: 23 additions & 11 deletions component-model/examples/tutorial/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@ The WIT package for the calculator consists of a world for each mathematical ope
add an `op` enum that delineates each operator. The following example interface only
has an `add` operation:

```wit
package docs:calculator@0.1.0;
```wit adder
package docs:adder@0.1.0;

interface calculate {
enum op {
add,
}
eval-expression: func(op: op, x: u32, y: u32) -> u32;
}

interface add {
add: func(a: u32, b: u32) -> u32;
Expand All @@ -22,13 +16,30 @@ interface add {
world adder {
export add;
}
```


```wit calculator
package docs:[email protected];

interface calculate {
enum op {
add,
}
eval-expression: func(op: op, x: u32, y: u32) -> u32;
}

world calculator {
export calculate;
import add;
import docs:adder/add;
}

world app {
import calculate;
}
```


To expand the exercise to add more components, add another operator world, expand the enum, and modify the `command` component to call it.

## Building and running the example
Expand All @@ -39,13 +50,14 @@ To compose a calculator component with an add operator, run the following:
(cd calculator && cargo component build --release)
(cd adder && cargo component build --release)
(cd command && cargo component build --release)
cd ..
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
wasm-tools compose command/target/wasm32-wasi/release/command.wasm -d composed.wasm -o final.wasm
```

Now, run the component with wasmtime:

```sh
wasmtime run command.wasm 1 2 add
wasmtime run final.wasm 1 2 add
1 + 2 = 3
```
5 changes: 2 additions & 3 deletions component-model/examples/tutorial/adder/Cargo.lock

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

9 changes: 4 additions & 5 deletions component-model/examples/tutorial/adder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ version = "0.1.0"
edition = "2021"

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

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

[package.metadata.component]
package = "docs:calculator"
package = "docs:adder"

[package.metadata.component.dependencies]

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

[package.metadata.component.dependencies]
22 changes: 11 additions & 11 deletions component-model/examples/tutorial/adder/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod exports {
#[allow(dead_code)]
pub mod docs {
#[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_docs_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 = "docs: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_docs_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::docs::adder::add::__export_docs_adder_add_0_1_0_cabi!($ty with_types_in $($path_to_types_root)*::exports::docs::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; 203] = *b"\
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07P\x01A\x02\x01A\x02\x01\
B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x04\x01\x14docs:adder/add@0.1.0\
\x05\0\x04\x01\x16docs: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
3 changes: 2 additions & 1 deletion component-model/examples/tutorial/adder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[allow(warnings)]
mod bindings;

use crate::bindings::exports::docs::calculator::add::Guest;
use bindings::exports::docs::adder::add::Guest;

struct Component;

Expand Down
1 change: 0 additions & 1 deletion component-model/examples/tutorial/calculator/Cargo.lock

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

6 changes: 4 additions & 2 deletions component-model/examples/tutorial/calculator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ version = "0.1.0"
edition = "2021"

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

[lib]
Expand All @@ -13,8 +12,11 @@ crate-type = ["cdylib"]
[package.metadata.component]
package = "docs:calculator"

[package.metadata.component.target.dependencies]
"docs:adder" = { path = "../wit/adder" } # directory containing the WIT package

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

[package.metadata.component.dependencies]
20 changes: 10 additions & 10 deletions component-model/examples/tutorial/calculator/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[allow(dead_code)]
pub mod docs {
#[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 = "docs:adder/[email protected]")]
extern "C" {
#[link_name = "add"]
fn wit_import(_: i32, _: i32) -> i32;
Expand Down Expand Up @@ -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/\
calculate@0.1.0\x05\x01\x04\x01\x20docs:calculator/[email protected]\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; 308] = *b"\
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xb3\x01\x01A\x02\x01\
A\x04\x01B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x03\x01\x14docs:adder/\
[email protected]\x05\0\x01B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02op\x01\x01\
xy\x01yy\0y\x04\0\x0feval-expression\x01\x02\x04\x01\x1fdocs:calculator/calculat\
e@0.1.0\x05\x01\x04\x01\x20docs:calculator/[email protected]\x04\0\x0b\x10\x01\0\x0a\
calculator\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
3 changes: 2 additions & 1 deletion component-model/examples/tutorial/calculator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#[allow(warnings)]
mod bindings;

use bindings::exports::docs::calculator::calculate::{Guest, Op};

// Bring the imported add function into scope
use bindings::docs::calculator::add::add;
use bindings::docs::adder::add::add;

struct Component;

Expand Down
17 changes: 8 additions & 9 deletions component-model/examples/tutorial/command/Cargo.lock

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

15 changes: 7 additions & 8 deletions component-model/examples/tutorial/command/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ name = "command"
version = "0.1.0"
edition = "2021"

[package.metadata.component]
package = "docs:calculator"
[dependencies]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any reason for the reordering here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the package name to command to show that these components can be isolated in their own packages. Do you prefer to keep the package "docs:calculator"? @kate-goldenring

anyhow = "1"
wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
clap = { version = "4.3.19", features = ["derive"] }

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

[dependencies]
anyhow = "1"
clap = { version = "4.3.19", features = ["derive"] }
bitflags = "2.4.2"
wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
[package.metadata.component.target.dependencies]
"docs:adder" = { path = "../wit/adder" }
9 changes: 9 additions & 0 deletions component-model/examples/tutorial/wit/adder/world.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package docs:[email protected];

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

world adder {
export add;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,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 docs:adder/add@0.1.0;
}

world app {
Expand Down
Loading