Skip to content

Commit

Permalink
refactor: Update homestar-functions to use cargo component (#576)
Browse files Browse the repository at this point in the history
# Description

This PR makes the following changes:

- [x] Update `homestar-functions` to use `cargo-component`
- [x] Update Wasms, their CID references, and workflows that use them
- [x] Update nix flake `wasmTest` and `wasmAdd` scripts
- [x] Add log message when `homestar-wasm` upgrades a Wasm to a Wasm
component

## Link to issue

Closes #533 

## Type of change

- [x] Refactor (non-breaking change that updates existing functionality)

## Test plan (required)

Tests added for the new `cargo-component` generated Wasm components.
  • Loading branch information
bgins authored Feb 16, 2024
1 parent b30d01e commit 513b038
Show file tree
Hide file tree
Showing 38 changed files with 7,258 additions and 168 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ repos:
args: ["-b", "main"]
- id: check-merge-conflict
- id: trailing-whitespace
exclude: bindings\.rs$
- id: end-of-file-fixer
exclude: \.(txt|json)$
- id: check-yaml
Expand Down
1 change: 1 addition & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
edition = "2021"
imports_granularity = "Crate"
format_generated_files = false
ignore = ["homestar-functions/**/src/bindings.rs"]
82 changes: 7 additions & 75 deletions Cargo.lock

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

Binary file modified examples/websocket-relay/example_test.wasm
Binary file not shown.
12 changes: 6 additions & 6 deletions examples/websocket-relay/relay-app/src/lib/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export const workflowOnePromised = WorkflowBuilder.workflow({
WorkflowBuilder.crop({
name: "crop",
resource:
"ipfs://bafybeidfzi27nkgybei6wjzs5ao7w3y5f57mqrkkk4776fbajm25tfzl54",
"ipfs://bafybeidpmgamv4i6jqrlwbnkrm3kzvvu7hj3jnoolcswub27mkda6p75la",
args: {
data: "{{ cid:bafybeiejevluvtoevgk66plh5t6xiy3ikyuuxg3vgofuvpeckb6eadresm }}",
x: 150,
Expand All @@ -268,15 +268,15 @@ export const workflowOnePromised = WorkflowBuilder.workflow({
WorkflowBuilder.rotate90({
name: "rotate90",
resource:
"ipfs://bafybeidfzi27nkgybei6wjzs5ao7w3y5f57mqrkkk4776fbajm25tfzl54",
"ipfs://bafybeidpmgamv4i6jqrlwbnkrm3kzvvu7hj3jnoolcswub27mkda6p75la",
args: {
data: "{{needs.crop.output}}",
},
}),
WorkflowBuilder.blur({
name: "blur",
resource:
"ipfs://bafybeidfzi27nkgybei6wjzs5ao7w3y5f57mqrkkk4776fbajm25tfzl54",
"ipfs://bafybeidpmgamv4i6jqrlwbnkrm3kzvvu7hj3jnoolcswub27mkda6p75la",
args: {
data: "{{needs.rotate90.output}}",
sigma: 20.2,
Expand All @@ -293,7 +293,7 @@ export const workflowTwoPromised = WorkflowBuilder.workflow({
WorkflowBuilder.crop({
name: "crop",
resource:
"ipfs://bafybeidfzi27nkgybei6wjzs5ao7w3y5f57mqrkkk4776fbajm25tfzl54",
"ipfs://bafybeidpmgamv4i6jqrlwbnkrm3kzvvu7hj3jnoolcswub27mkda6p75la",
args: {
data: "{{ cid:bafybeiejevluvtoevgk66plh5t6xiy3ikyuuxg3vgofuvpeckb6eadresm }}",
x: 150,
Expand All @@ -305,15 +305,15 @@ export const workflowTwoPromised = WorkflowBuilder.workflow({
WorkflowBuilder.rotate90({
name: "rotate90",
resource:
"ipfs://bafybeidfzi27nkgybei6wjzs5ao7w3y5f57mqrkkk4776fbajm25tfzl54",
"ipfs://bafybeidpmgamv4i6jqrlwbnkrm3kzvvu7hj3jnoolcswub27mkda6p75la",
args: {
data: "{{needs.crop.output}}",
},
}),
WorkflowBuilder.grayscale({
name: "grayscale",
resource:
"ipfs://bafybeidfzi27nkgybei6wjzs5ao7w3y5f57mqrkkk4776fbajm25tfzl54",
"ipfs://bafybeidpmgamv4i6jqrlwbnkrm3kzvvu7hj3jnoolcswub27mkda6p75la",
args: {
data: "{{needs.rotate90.output}}",
},
Expand Down
7 changes: 7 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@

wasmTest = pkgs.writeScriptBin "wasm-ex-test" ''
#!${pkgs.stdenv.shell}
cargo component build -p homestar-functions-test --target wasm32-unknown-unknown --profile release-wasm-fn
cp target/wasm32-unknown-unknown/release-wasm-fn/homestar_functions_test.wasm homestar-wasm/fixtures/example_test_cargo_component.wasm
cargo component build -p homestar-functions-test --profile release-wasm-fn
cp target/wasm32-wasi/release-wasm-fn/homestar_functions_test.wasm homestar-wasm/fixtures/example_test_cargo_component_wasi.wasm
cargo build -p homestar-functions-test --target wasm32-unknown-unknown --profile release-wasm-fn
cp target/wasm32-unknown-unknown/release-wasm-fn/homestar_functions_test.wasm homestar-wasm/fixtures/example_test.wasm
wasm-tools component new homestar-wasm/fixtures/example_test.wasm -o homestar-wasm/fixtures/example_test_component.wasm
Expand All @@ -229,6 +233,9 @@

wasmAdd = pkgs.writeScriptBin "wasm-ex-add" ''
#!${pkgs.stdenv.shell}
cargo component build -p homestar-functions-add --profile release-wasm-fn
cp target/wasm32-wasi/release-wasm-fn/homestar_functions_add.wasm homestar-wasm/fixtures/example_add_cargo_component_wasi.wasm
wasm-tools print homestar-wasm/fixtures/example_add_cargo_component_wasi.wasm -o homestar-wasm/fixtures/example_add_cargo_component_wasi.wat
cargo build -p homestar-functions-add --target wasm32-unknown-unknown --profile release-wasm-fn
cp target/wasm32-unknown-unknown/release-wasm-fn/homestar_functions_add.wasm homestar-wasm/fixtures/example_add.wasm
wasm-tools component new homestar-wasm/fixtures/example_add.wasm -o homestar-wasm/fixtures/example_add_component.wasm
Expand Down
10 changes: 5 additions & 5 deletions homestar-functions/add/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ edition = { workspace = true }
rust-version = { workspace = true }

[dependencies]
wit-bindgen = "0.17"
wit-bindgen = { version = "0.18.0", default-features = false, features = [
"realloc",
] }

[lib]
doc = false
bench = false
crate-type = ["cdylib"]

[features]
default = []
[package.metadata.component]
package = "component:add"
70 changes: 70 additions & 0 deletions homestar-functions/add/src/bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Generated by `wit-bindgen` 0.16.0. DO NOT EDIT!
const _: () = {

#[doc(hidden)]
#[export_name = "add-one"]
#[allow(non_snake_case)]
unsafe extern "C" fn __export_add_one(arg0: i32,) -> i32 {
#[allow(unused_imports)]
use wit_bindgen::rt::{alloc, vec::Vec, string::String};

// Before executing any other code, use this function to run all static
// constructors, if they have not yet been run. This is a hack required
// to work around wasi-libc ctors calling import functions to initialize
// the environment.
//
// This functionality will be removed once rust 1.69.0 is stable, at which
// point wasi-libc will no longer have this behavior.
//
// See
// https://github.com/bytecodealliance/preview2-prototyping/issues/99
// for more details.
#[cfg(target_arch="wasm32")]
wit_bindgen::rt::run_ctors_once();

let result0 = <_GuestImpl as Guest>::add_one(arg0);
wit_bindgen::rt::as_i32(result0)
}
};
const _: () = {

#[doc(hidden)]
#[export_name = "add-two"]
#[allow(non_snake_case)]
unsafe extern "C" fn __export_add_two(arg0: i32,) -> i32 {
#[allow(unused_imports)]
use wit_bindgen::rt::{alloc, vec::Vec, string::String};

// Before executing any other code, use this function to run all static
// constructors, if they have not yet been run. This is a hack required
// to work around wasi-libc ctors calling import functions to initialize
// the environment.
//
// This functionality will be removed once rust 1.69.0 is stable, at which
// point wasi-libc will no longer have this behavior.
//
// See
// https://github.com/bytecodealliance/preview2-prototyping/issues/99
// for more details.
#[cfg(target_arch="wasm32")]
wit_bindgen::rt::run_ctors_once();

let result0 = <_GuestImpl as Guest>::add_two(arg0);
wit_bindgen::rt::as_i32(result0)
}
};
use super::Component as _GuestImpl;
pub trait Guest {
fn add_one(input: i32,) -> i32;
fn add_two(input: i32,) -> i32;
}

#[cfg(target_arch = "wasm32")]
#[link_section = "component-type:add"]
#[doc(hidden)]
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 190] = [3, 0, 3, 97, 100, 100, 0, 97, 115, 109, 13, 0, 1, 0, 7, 73, 1, 65, 2, 1, 65, 3, 1, 64, 1, 5, 105, 110, 112, 117, 116, 122, 0, 122, 4, 0, 7, 97, 100, 100, 45, 111, 110, 101, 1, 0, 4, 0, 7, 97, 100, 100, 45, 116, 119, 111, 1, 0, 4, 1, 26, 104, 111, 109, 101, 115, 116, 97, 114, 45, 102, 117, 110, 99, 116, 105, 111, 110, 115, 58, 97, 100, 100, 47, 97, 100, 100, 4, 0, 11, 9, 1, 0, 3, 97, 100, 100, 3, 0, 0, 0, 16, 12, 112, 97, 99, 107, 97, 103, 101, 45, 100, 111, 99, 115, 0, 123, 125, 0, 70, 9, 112, 114, 111, 100, 117, 99, 101, 114, 115, 1, 12, 112, 114, 111, 99, 101, 115, 115, 101, 100, 45, 98, 121, 2, 13, 119, 105, 116, 45, 99, 111, 109, 112, 111, 110, 101, 110, 116, 6, 48, 46, 49, 56, 46, 50, 16, 119, 105, 116, 45, 98, 105, 110, 100, 103, 101, 110, 45, 114, 117, 115, 116, 6, 48, 46, 49, 54, 46, 48];

#[inline(never)]
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
pub fn __link_section() {}
13 changes: 6 additions & 7 deletions homestar-functions/add/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
wit_bindgen::generate!({
world: "add",
exports: {
world: Component,
}
});
#[allow(clippy::all)]
#[rustfmt::skip]
mod bindings;

use bindings::Guest;

pub struct Component;
struct Component;

impl Guest for Component {
fn add_one(input: i32) -> i32 {
Expand Down
11 changes: 10 additions & 1 deletion homestar-functions/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ rust-version = { workspace = true }
[dependencies]
base64 = "0.21"
image = { version = "0.24", default-features = false, features = ["png"] }
wit-bindgen = "0.17"
wit-bindgen = { version = "0.18.0", default-features = false, features = [
"realloc",
] }

[dev-dependencies]
image = { version = "0.24", default-features = false, features = [
Expand All @@ -25,3 +27,10 @@ crate-type = ["cdylib"]
[features]
default = []
run-image-tests = []

[package.metadata.component]
package = "component:homestar-functions-test"

[package.metadata.component.target.dependencies]
"wasi:logging" = { path = "wit/deps/logging" }
"homestar:host" = { path = "wit/deps/helpers" }
Loading

0 comments on commit 513b038

Please sign in to comment.