Skip to content

Commit 821bc51

Browse files
committed
Add component example and CI
Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
1 parent 0ea8191 commit 821bc51

10 files changed

Lines changed: 79 additions & 60 deletions

File tree

.github/workflows/dep_rust.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,12 @@ jobs:
7777
name: guest-modules
7878
path: ./x64/${{ matrix.config }}
7979

80-
- name: Build Rust Wasi examples
81-
run: just build-rust-wasi-examples ${{ matrix.config }}
80+
- name: Build Rust component model examples
81+
run: |
82+
# this must be build before the formatting and other jobs run
83+
# because the component model example depends on the wasm component built here
84+
just ensure-tools
85+
just build-rust-component-examples ${{ matrix.config }}
8286
8387
- name: Fmt
8488
run: just fmt-check
@@ -127,8 +131,8 @@ jobs:
127131
# required for gh cli when downloading
128132
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
129133

130-
- name: Test Wasi Examples
131-
run: just examples-wasi ${{ matrix.config }} ${{ matrix.hypervisor == 'mshv3' && 'mshv3' || ''}}
134+
- name: Test Component Model Examples
135+
run: just examples-components ${{ matrix.config }} ${{ matrix.hypervisor == 'mshv3' && 'mshv3' || ''}}
132136
working-directory: ./src/hyperlight_wasm
133137

134138
### Benchmarks ###

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,4 +478,4 @@ target/
478478

479479
# MSVC Windows builds of rustc generate these, which store debugging information
480480
*.pdb
481-
src/wasi_samples/wit/component-world.wasm
481+
src/component_sample/**/*.wasm

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
22
members = [ "src/hyperlight_wasm", "src/examples_common", "src/hyperlight_wasm_aot" ]
3-
exclude = [ "src/wasm_runtime", "src/rust_wasm_samples", "src/hyperlight_wasm_macro", "src/wasi_samples" ]
3+
exclude = [ "src/wasm_runtime", "src/rust_wasm_samples", "src/hyperlight_wasm_macro", "src/component_sample" ]
44
resolver = "2"
55

66
[workspace.dependencies]

Justfile

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ default-tag:= "latest"
33
build-wasm-examples-command := if os() == "windows" { "./src/hyperlight_wasm/scripts/build-wasm-examples.bat" } else { "./src/hyperlight_wasm/scripts/build-wasm-examples.sh" }
44
mkdir-arg := if os() == "windows" { "-Force" } else { "-p" }
55
latest-release:= if os() == "windows" {"$(git tag -l --sort=v:refname | select -last 2 | select -first 1)"} else {`git tag -l --sort=v:refname | tail -n 2 | head -n 1`}
6-
wit-world := if os() == "windows" { "$env:WIT_WORLD=\"" + justfile_directory() + "\\src\\wasi_samples\\wit\\component-world.wasm" + "\";" } else { "WIT_WORLD=" + justfile_directory() + "/src/wasi_samples/wit/component-world.wasm" }
6+
wit-world := if os() == "windows" { "$env:WIT_WORLD=\"" + justfile_directory() + "\\src\\component_sample\\wit\\component-world.wasm" + "\";" } else { "WIT_WORLD=" + justfile_directory() + "/src/component_sample/wit/component-world.wasm" }
77

88
set windows-shell := ["pwsh.exe", "-NoLogo", "-Command"]
99

10-
build-all target=default-target: (build target) (build-wasm-examples target) (build-rust-wasm-examples target) (build-wasm-runtime target) (build-rust-wasi-examples target)
10+
ensure-tools:
11+
cargo install --locked wasm-tools --version 1.235.0
12+
cargo install cargo-component --locked --version 0.21.1
13+
14+
build-all target=default-target: (build target) (build-wasm-examples target) (build-rust-wasm-examples target) (build-wasm-runtime target) (build-rust-component-examples target)
1115

1216
build target=default-target features="": (build-wasm-runtime target) (fmt-check)
1317
cargo build {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --verbose --profile={{ if target == "debug" {"dev"} else { target } }}
@@ -32,39 +36,37 @@ build-rust-wasm-examples target=default-target: (mkdir-redist target)
3236
cargo run -p hyperlight-wasm-aot compile ./src/rust_wasm_samples/target/wasm32-unknown-unknown/{{ target }}/rust_wasm_samples.wasm ./x64/{{ target }}/rust_wasm_samples.aot
3337
cp ./x64/{{ target }}/rust_wasm_samples.aot ./x64/{{ target }}/rust_wasm_samples.wasm
3438

35-
build-rust-wasi-examples target=default-target:
36-
cargo install --locked wasm-tools
37-
cargo install cargo-component --locked
38-
wasm-tools component wit ./src/wasi_samples/wit/example.wit -w -o ./src/wasi_samples/wit/component-world.wasm
39+
build-rust-component-examples target=default-target:
40+
wasm-tools component wit ./src/component_sample/wit/example.wit -w -o ./src/component_sample/wit/component-world.wasm
3941
# use cargo component so we don't get all the wasi imports https://github.com/bytecodealliance/cargo-component?tab=readme-ov-file#relationship-with-wasm32-wasip2
4042
# we also explicitly target wasm32-unknown-unknown since cargo component might try to pull in wasi imports https://github.com/bytecodealliance/cargo-component/issues/290
4143
rustup target add wasm32-unknown-unknown
42-
cd ./src/wasi_samples && cargo component build --target wasm32-unknown-unknown --profile={{ if target == "debug" {"dev"} else { target } }}
43-
cargo run -p hyperlight-wasm-aot compile --component ./src/wasi_samples/target/wasm32-unknown-unknown/{{ target }}/wasi_samples.wasm ./x64/{{ target }}/wasi_samples.aot
44-
cp ./x64/{{ target }}/wasi_samples.aot ./x64/{{ target }}/wasi_samples.wasm
44+
cd ./src/component_sample && cargo component build --target wasm32-unknown-unknown --profile={{ if target == "debug" {"dev"} else { target } }}
45+
cargo run -p hyperlight-wasm-aot compile --component ./src/component_sample/target/wasm32-unknown-unknown/{{ target }}/component_sample.wasm ./x64/{{ target }}/component_sample.aot
46+
cp ./x64/{{ target }}/component_sample.aot ./x64/{{ target }}/component_sample.wasm
4547

4648
check target=default-target:
4749
cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
4850
cd src/rust_wasm_samples && cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
49-
cd src/wasi_samples && cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
51+
cd src/component_sample && cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
5052
cd src/wasm_runtime && cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
5153

5254
fmt-check:
5355
rustup toolchain install nightly -c rustfmt && cargo +nightly fmt -v --all -- --check
5456
cd src/rust_wasm_samples && rustup toolchain install nightly -c rustfmt && cargo +nightly fmt -v --all -- --check
55-
cd src/wasi_samples && rustup toolchain install nightly -c rustfmt && cargo +nightly fmt -v --all -- --check
57+
cd src/component_sample && rustup toolchain install nightly -c rustfmt && cargo +nightly fmt -v --all -- --check
5658
cd src/wasm_runtime && rustup toolchain install nightly -c rustfmt && cargo +nightly fmt -v --all -- --check
5759
fmt:
5860
rustup toolchain install nightly -c rustfmt
5961
cargo +nightly fmt --all
60-
cd src/rust_wasm_samples && cargo +nightly fmt
61-
cd src/wasi_samples && cargo +nightly fmt
62-
cd src/wasm_runtime && cargo +nightly fmt
62+
cd src/rust_wasm_samples && cargo +nightly fmt -v --all
63+
cd src/component_sample && cargo +nightly fmt -v --all
64+
cd src/wasm_runtime && cargo +nightly fmt -v --all
6365

6466
clippy target=default-target: (check target)
6567
cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings
6668
cd src/rust_wasm_samples && cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings
67-
cd src/wasi_samples && cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings
69+
cd src/component_sample && cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings
6870
cd src/wasm_runtime && cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings
6971

7072
# TESTING
@@ -88,8 +90,8 @@ examples-ci target=default-target features="": (build-rust-wasm-examples target)
8890
cargo run {{ if features =="" {''} else {"--no-default-features -F function_call_metrics," + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} --example metrics
8991
cargo run {{ if features =="" {"--no-default-features --features kvm,mshv2"} else {"--no-default-features -F function_call_metrics," + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} --example metrics
9092

91-
examples-wasi target=default-target features="": (build-rust-wasi-examples target)
92-
{{ wit-world }} cargo run {{ if features =="" {''} else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} --example wasi_examples
93+
examples-components target=default-target features="": (build-rust-component-examples target)
94+
{{ wit-world }} cargo run {{ if features =="" {''} else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} --example component_example
9395

9496
# warning, compares to and then OVERWRITES the given baseline
9597
bench-ci baseline target=default-target features="":
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "wasi_samples"
2+
name = "component_sample"
33
version = "0.1.0"
44
edition = "2024"
55

@@ -10,7 +10,7 @@ wit-bindgen-rt = { version = "0.41.0", features = ["bitflags"] }
1010
crate-type = ["cdylib"]
1111

1212
[package.metadata.component]
13-
package = "wasi-sample:example"
13+
package = "component-sample:example"
1414

1515
[package.metadata.component.target]
1616
path = "wit/example.wit"
Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// * runtime_path: "wit_bindgen_rt"
44
#[rustfmt::skip]
55
#[allow(dead_code, clippy::all)]
6-
pub mod wasi_sample {
6+
pub mod component_sample {
77
pub mod example {
88
#[allow(dead_code, async_fn_in_trait, unused_imports, clippy::all)]
99
pub mod host {
@@ -18,7 +18,7 @@ pub mod wasi_sample {
1818
let ptr0 = vec0.as_ptr().cast::<u8>();
1919
let len0 = vec0.len();
2020
#[cfg(target_arch = "wasm32")]
21-
#[link(wasm_import_module = "wasi-sample:example/host")]
21+
#[link(wasm_import_module = "component-sample:example/host")]
2222
unsafe extern "C" {
2323
#[link_name = "print"]
2424
fn wit_import1(_: *mut u8, _: usize);
@@ -49,7 +49,7 @@ pub mod wasi_sample {
4949
let len0 = vec0.len();
5050
let ptr1 = ret_area.0.as_mut_ptr().cast::<u8>();
5151
#[cfg(target_arch = "wasm32")]
52-
#[link(wasm_import_module = "wasi-sample:example/host")]
52+
#[link(wasm_import_module = "component-sample:example/host")]
5353
unsafe extern "C" {
5454
#[link_name = "host-function"]
5555
fn wit_import2(_: *mut u8, _: usize, _: *mut u8);
@@ -75,7 +75,7 @@ pub mod wasi_sample {
7575
#[rustfmt::skip]
7676
#[allow(dead_code, clippy::all)]
7777
pub mod exports {
78-
pub mod wasi_sample {
78+
pub mod component_sample {
7979
pub mod example {
8080
#[allow(dead_code, async_fn_in_trait, unused_imports, clippy::all)]
8181
pub mod adder {
@@ -123,24 +123,29 @@ pub mod exports {
123123
fn call_host(input: _rt::String) -> _rt::String;
124124
}
125125
#[doc(hidden)]
126-
macro_rules! __export_wasi_sample_example_adder_cabi {
126+
macro_rules! __export_component_sample_example_adder_cabi {
127127
($ty:ident with_types_in $($path_to_types:tt)*) => {
128128
const _ : () = { #[unsafe (export_name =
129-
"wasi-sample:example/adder#add")] unsafe extern "C" fn
129+
"component-sample:example/adder#add")] unsafe extern "C" fn
130130
export_add(arg0 : i32, arg1 : i32,) -> i32 { unsafe {
131131
$($path_to_types)*:: _export_add_cabi::<$ty > (arg0, arg1) } }
132-
#[unsafe (export_name = "wasi-sample:example/adder#call-host")]
133-
unsafe extern "C" fn export_call_host(arg0 : * mut u8, arg1 :
134-
usize,) -> * mut u8 { unsafe { $($path_to_types)*::
135-
_export_call_host_cabi::<$ty > (arg0, arg1) } } #[unsafe
136-
(export_name = "cabi_post_wasi-sample:example/adder#call-host")]
137-
unsafe extern "C" fn _post_return_call_host(arg0 : * mut u8,) {
138-
unsafe { $($path_to_types)*:: __post_return_call_host::<$ty >
139-
(arg0) } } };
132+
#[unsafe (export_name =
133+
"component-sample:example/adder#call-host")] unsafe extern "C" fn
134+
export_call_host(arg0 : * mut u8, arg1 : usize,) -> * mut u8 {
135+
unsafe { $($path_to_types)*:: _export_call_host_cabi::<$ty >
136+
(arg0, arg1) } } #[unsafe (export_name =
137+
"cabi_post_component-sample:example/adder#call-host")] unsafe
138+
extern "C" fn _post_return_call_host(arg0 : * mut u8,) { unsafe {
139+
$($path_to_types)*:: __post_return_call_host::<$ty > (arg0) } }
140+
#[unsafe (export_name =
141+
"component-sample:example/adder#do-something")] unsafe extern "C"
142+
fn export_do_something(arg0 : i32,) { unsafe {
143+
$($path_to_types)*:: _export_do_something_cabi::<$ty > (arg0) } }
144+
};
140145
};
141146
}
142147
#[doc(hidden)]
143-
pub(crate) use __export_wasi_sample_example_adder_cabi;
148+
pub(crate) use __export_component_sample_example_adder_cabi;
144149
#[cfg_attr(target_pointer_width = "64", repr(align(8)))]
145150
#[cfg_attr(target_pointer_width = "32", repr(align(4)))]
146151
struct _RetArea(
@@ -265,24 +270,28 @@ macro_rules! __export_example_impl {
265270
};
266271
($ty:ident with_types_in $($path_to_types_root:tt)*) => {
267272
$($path_to_types_root)*::
268-
exports::wasi_sample::example::adder::__export_wasi_sample_example_adder_cabi!($ty
269-
with_types_in $($path_to_types_root)*:: exports::wasi_sample::example::adder);
273+
exports::component_sample::example::adder::__export_component_sample_example_adder_cabi!($ty
274+
with_types_in $($path_to_types_root)*::
275+
exports::component_sample::example::adder);
270276
};
271277
}
272278
#[doc(inline)]
273279
pub(crate) use __export_example_impl as export;
274280
#[cfg(target_arch = "wasm32")]
275-
#[unsafe(link_section = "component-type:wit-bindgen:0.41.0:wasi-sample:example:example:encoded world")]
281+
#[unsafe(
282+
link_section = "component-type:wit-bindgen:0.41.0:component-sample:example:example:encoded world"
283+
)]
276284
#[doc(hidden)]
277285
#[allow(clippy::octal_escapes)]
278-
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 335] = *b"\
279-
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xd1\x01\x01A\x02\x01\
286+
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 380] = *b"\
287+
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xfe\x01\x01A\x02\x01\
280288
A\x04\x01B\x04\x01@\x01\x07messages\x01\0\x04\0\x05print\x01\0\x01@\x01\x05input\
281-
s\0s\x04\0\x0dhost-function\x01\x01\x03\0\x18wasi-sample:example/host\x05\0\x01B\
282-
\x04\x01@\x02\x04lefty\x05righty\0y\x04\0\x03add\x01\0\x01@\x01\x05inputs\0s\x04\
283-
\0\x09call-host\x01\x01\x04\0\x19wasi-sample:example/adder\x05\x01\x04\0\x1bwasi\
284-
-sample:example/example\x04\0\x0b\x0d\x01\0\x07example\x03\0\0\0G\x09producers\x01\
285-
\x0cprocessed-by\x02\x0dwit-component\x070.227.1\x10wit-bindgen-rust\x060.41.0";
289+
s\0s\x04\0\x0dhost-function\x01\x01\x03\0\x1dcomponent-sample:example/host\x05\0\
290+
\x01B\x06\x01@\x02\x04lefty\x05righty\0y\x04\0\x03add\x01\0\x01@\x01\x05inputs\0\
291+
s\x04\0\x09call-host\x01\x01\x01@\x01\x06numbery\x01\0\x04\0\x0cdo-something\x01\
292+
\x02\x04\0\x1ecomponent-sample:example/adder\x05\x01\x04\0\x20component-sample:e\
293+
xample/example\x04\0\x0b\x0d\x01\0\x07example\x03\0\0\0G\x09producers\x01\x0cpro\
294+
cessed-by\x02\x0dwit-component\x070.227.1\x10wit-bindgen-rust\x060.41.0";
286295
#[inline(never)]
287296
#[doc(hidden)]
288297
pub fn __link_custom_section_describing_imports() {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#[rustfmt::skip]
33
mod bindings;
44

5-
use bindings::exports::wasi_sample::example::adder::Guest;
6-
use bindings::wasi_sample::example::host::{host_function, print};
5+
use bindings::component_sample::example::host::{host_function, print};
6+
use bindings::exports::component_sample::example::adder::Guest;
77

88
struct Component {}
99

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package wasi-sample:example;
1+
package component-sample:example;
22

33
world example {
44
import host;

src/hyperlight_wasm/examples/wasi_examples/main.rs renamed to src/hyperlight_wasm/examples/component_example/main.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
use bindings::wasi_sample::example::Adder;
1+
#![allow(renamed_and_removed_lints)]
2+
#![allow(unknown_lints)]
3+
#![allow(unused_unit)]
4+
5+
use bindings::component_sample::example::Adder;
26
use examples_common::get_wasm_module_path;
37

48
extern crate alloc;
59
mod bindings {
6-
hyperlight_component_macro::host_bindgen!("../wasi_samples/wit/component-world.wasm");
10+
hyperlight_component_macro::host_bindgen!("../component_sample/wit/component-world.wasm");
711
}
812

913
pub struct State {}
@@ -19,7 +23,7 @@ impl Default for State {
1923
}
2024
}
2125

22-
impl bindings::wasi_sample::example::Host for State {
26+
impl bindings::component_sample::example::Host for State {
2327
fn r#print(&mut self, message: alloc::string::String) {
2428
println!("Logged from component: {message}");
2529
}
@@ -30,7 +34,7 @@ impl bindings::wasi_sample::example::Host for State {
3034
}
3135

3236
#[allow(refining_impl_trait)]
33-
impl bindings::wasi_sample::example::ExampleImports for State {
37+
impl bindings::component_sample::example::ExampleImports for State {
3438
type Host = State;
3539

3640
fn r#host(&mut self) -> &mut Self {
@@ -50,12 +54,12 @@ fn main() {
5054

5155
let sb = sb.load_runtime().unwrap();
5256

53-
let mod_path = get_wasm_module_path("wasi_samples.wasm").unwrap();
57+
let mod_path = get_wasm_module_path("component_sample.wasm").unwrap();
5458
let sb = sb.load_module(mod_path).unwrap();
5559

5660
let mut wrapped = bindings::ExampleSandbox { sb, rt };
5761

58-
let instance = bindings::wasi_sample::example::ExampleExports::adder(&mut wrapped);
62+
let instance = bindings::component_sample::example::ExampleExports::adder(&mut wrapped);
5963
let result = instance.add(1, 2);
6064
assert_eq!(3, result);
6165
println!("Add result is {result}");

0 commit comments

Comments
 (0)