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

Add basic example of wac CLI and programatic API #107

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions Cargo.lock

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

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ wat = ["wac-resolver/wat"]
wit = ["wac-resolver/wit"]
registry = ["wac-resolver/registry", "indicatif"]

[workspace]
members = ["examples/programmatic"]

[workspace.dependencies]
wac-parser = { path = "crates/wac-parser", version = "0.1.0", default-features = false }
wac-resolver = { path = "crates/wac-resolver", version = "0.1.0", default-features = false }
Expand All @@ -61,7 +64,10 @@ clap = { version = "4.5.4", features = ["derive"] }
semver = { version = "1.0.22", features = ["serde"] }
pretty_env_logger = "0.5.0"
log = "0.4.21"
tokio = { version = "1.37.0", default-features = false, features = ["macros", "rt-multi-thread"] }
tokio = { version = "1.37.0", default-features = false, features = [
"macros",
"rt-multi-thread",
] }
owo-colors = { version = "4.0.0", features = ["supports-colors"] }
indexmap = { version = "2.2.6", features = ["serde"] }
id-arena = "2.2.1"
Expand Down
55 changes: 55 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Examples

This example is composed of two different but equal ways for composing two example components together
* using the `wac` CLI
* using the `wac-graph` library crate

The example uses two input components (both located in the `deps` directory) that will be composed together:

The `hello` component exports a function `hello` which returns a string:

```bash
# Print the wit for the hello component
$ wasm-tools component wit deps/example/hello.wasm
package root:component;

world root {
export hello: func() -> string;
}
```

The `hello` exported function from `hello.wasm` will be plugged into the `hello` import of the `greeter` component which has the same signature as the `hello` exported function:

```bash
# Print the wit for the greeter component
$ wasm-tools component wit deps/example/greeter.wasm
package root:component;

world root {
import hello: func() -> string;

export greet: func() -> string;
}
```

The resulting composed component will therefore only have the exported `greet` function originally from the `greeter` component.

## CLI

`wac` can be used as a CLI tool. The `wac encode` command takes a wac script as input which defines how two components are composed together.

Running the following command should produce a new component that is the composition of the `hello` and `greeter` components.

```
wac encode composition.wac -o composed.wasm
rylev marked this conversation as resolved.
Show resolved Hide resolved
```

*Note*: `wac encode` expects to find any input components inside of a `deps` folder in a directory named after the namespace part of the input component's name. In our example, the wac script uses the `example:greeter` and `example:hello` input components so `wac encode` expects to find those components in the `deps/example` directory.
rylev marked this conversation as resolved.
Show resolved Hide resolved

## Programmatic Graph API

You can also build the composition using the programmatic API used by the `programmatic` example binary. This can be done by running the following inside the `programmatic` directory:

```
$ cargo run
```
Binary file added examples/deps/example/greeter.wasm
Binary file not shown.
Binary file added examples/deps/example/hello.wasm
Binary file not shown.
7 changes: 7 additions & 0 deletions examples/programmatic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "programmatic-example"
version = "0.1.0"
edition = "2021"

[dependencies]
wac-graph = { path = "../../crates/wac-graph" }
46 changes: 46 additions & 0 deletions examples/programmatic/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use wac_graph::{types::Package, CompositionGraph, EncodeOptions};

fn main() {
let mut graph = CompositionGraph::new();

// Register the package dependencies into the graph
let package = Package::from_file(
"hello",
None,
"../deps/example/hello.wasm",
graph.types_mut(),
)
.unwrap();
let hello = graph.register_package(package).unwrap();
let package = Package::from_file(
"greeter",
None,
"../deps/example/greeter.wasm",
graph.types_mut(),
)
.unwrap();
let greeter = graph.register_package(package).unwrap();

// Instantiate the hello instance which does not have any arguments
let hello_instance = graph.instantiate(hello);

// Instantiate the greeter instance which has a single argument "hello" which is exported by the hello instance
let greeter_instance = graph.instantiate(greeter);
let hello_export = graph
.alias_instance_export(hello_instance, "hello")
.unwrap();
graph
.set_instantiation_argument(greeter_instance, "hello", hello_export)
.unwrap();

// Alias the "greet" export from the greeter instance
let greet_export = graph
.alias_instance_export(greeter_instance, "greet")
.unwrap();
// Export the "greet" function from the composition
graph.export(greet_export, "greet").unwrap();

// Encode the graph into a WASM binary
let encoding = graph.encode(EncodeOptions::default()).unwrap();
std::fs::write("composition.wasm", encoding).unwrap();
}
13 changes: 13 additions & 0 deletions examples/script.wac
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package example:composition;

// Instantiate the `hello` component
let hello = new example:hello {};

// Instantiate the `greeter` component plugging its one `hello` import with
// the `hello` export of the `hello` component.
let greeter = new example:greeter {
hello: hello.hello,
};

// Export the greet function from the greeter component
export greeter.greet;
Loading