Skip to content
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

feat(composition): Adding an example that shows composition of isolated components #135

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions component-model/examples/composition/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## Summary:
This directory contains an example where component can be written and built separately in different languages and then composed together.
There are multiple components in this repo :
add, calculator and app are components written in RUST.
sub is a component written in tinygo.

calculator is a component that composes the components add and sub at compile time.
calculator is a library component and cannot be used as a standalone app.
Hence, the app component is built as a command component that composes the calculator component.

## Prerequisites:

### Install Wasmtime
curl https://wasmtime.dev/install.sh -sSf | bash

### Update PATH
Wasmtime binary gets deployed at ~/.wasmtime directory.

Update bash profile with wasmtime
```export PATH="~/.wasmtime/bin:$PATH"```


### Install cargo component
```cargo install cargo-component```


## Build and test

### Build the components

```
cd calculator
cargo component build --release
cd ..
cd add
cargo component build --release
cd ..

cd app
cargo component build --release
cd ..
```

### Subtractor component is a go component. Build it according to the readme in the sub directory to generate output component- sub-component.wasm.
```
cd sub
```

### Compose the components add, sub and calculator
```
cd ..
wasm-tools compose calculator/target/wasm32-wasi/release/calculator.wasm -d add/target/wasm32-wasi/release/add.wasm -d sub/sub-component.wasm -o composed-add-sub.wasm
```

### Generate app component using composed-add-sub.wasm
```
wasm-tools compose app/target/wasm32-wasi/release/app.wasm -d composed-add-sub.wasm -o command.wasm
```


### Run the command component using wasmtime to test it
```
wasmtime run command.wasm 1 2 add

wasmtime run command.wasm 1 2 sub
```
25 changes: 25 additions & 0 deletions component-model/examples/composition/add/Cargo.lock

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

17 changes: 17 additions & 0 deletions component-model/examples/composition/add/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "add"
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.24.0", features = ["bitflags"] }

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

[package.metadata.component]
package = "component:add"

[package.metadata.component.dependencies]
14 changes: 14 additions & 0 deletions component-model/examples/composition/add/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#[allow(warnings)]
mod bindings;

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

struct Component;

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

bindings::export!(Component with_types_in bindings);
10 changes: 10 additions & 0 deletions component-model/examples/composition/add/wit/world.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package docs:adder;


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

world adder {
export add;
}
253 changes: 253 additions & 0 deletions component-model/examples/composition/app/Cargo.lock

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

Loading