Add more complexity to example with custom record and parameters

This commit is contained in:
2025-01-09 16:34:26 +10:30
parent fca84b9240
commit 90dc149f6a
7 changed files with 29 additions and 20 deletions

16
Cargo.lock generated
View File

@@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 4
[[package]] [[package]]
name = "addr2line" name = "addr2line"
@@ -673,6 +673,13 @@ dependencies = [
"smallvec", "smallvec",
] ]
[[package]]
name = "runner"
version = "0.1.0"
dependencies = [
"wasmtime",
]
[[package]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.24" version = "0.1.24"
@@ -1157,13 +1164,6 @@ version = "20.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca6585868f5c427c3e9d2a8c0c3354e6d7d4518a0d17723ab25a0c1eebf5d5b4" checksum = "ca6585868f5c427c3e9d2a8c0c3354e6d7d4518a0d17723ab25a0c1eebf5d5b4"
[[package]]
name = "wasmtime-test"
version = "0.1.0"
dependencies = [
"wasmtime",
]
[[package]] [[package]]
name = "wasmtime-types" name = "wasmtime-types"
version = "20.0.2" version = "20.0.2"

View File

@@ -1,6 +1,6 @@
[workspace] [workspace]
members = [ members = [
"component", "component",
"runner", "runner",
] ]
resolver = "2"

View File

@@ -2,12 +2,13 @@
mod bindings; mod bindings;
use bindings::{hello, Guest}; use bindings::{hello, Guest};
use crate::bindings::RecordString;
struct Component; struct Component;
impl Guest for Component { impl Guest for Component {
fn greet() { fn greet(hello_string: RecordString) {
hello(); hello(&hello_string);
} }
} }

View File

@@ -1,6 +1,10 @@
package my:project; package my:project;
world test { world test {
import hello: func(); record record-string {
export greet: func(); hello-string: string,
}
import hello: func(hello-string: record-string);
export greet: func(hello-string: record-string);
} }

View File

@@ -1,5 +1,5 @@
[package] [package]
name = "wasmtime-test" name = "runner"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

View File

@@ -12,8 +12,8 @@ struct TestState {
} }
impl TestImports for TestState { impl TestImports for TestState {
fn hello(&mut self) -> std::result::Result<(), wasmtime::Error> { fn hello(&mut self, hello_string: RecordString) -> std::result::Result<(), wasmtime::Error> {
println!("{}", self.name); println!("{} - {}", self.name, hello_string.hello_string);
Ok(()) Ok(())
} }
} }
@@ -22,7 +22,7 @@ fn main() -> wasmtime::Result<()> {
let mut config = Config::new(); let mut config = Config::new();
config.wasm_component_model(true); config.wasm_component_model(true);
let engine = Engine::new(&config)?; let engine = Engine::new(&config)?;
let component = Component::from_file(&engine, "target/wasm32-wasi/release/component.wasm")?; let component = Component::from_file(&engine, "target/wasm32-wasip1/release/component.wasm")?;
let mut linker = Linker::new(&engine); let mut linker = Linker::new(&engine);
Test::add_to_linker(&mut linker, |state: &mut TestState| state)?; Test::add_to_linker(&mut linker, |state: &mut TestState| state)?;
let mut store = Store::new( let mut store = Store::new(
@@ -32,6 +32,6 @@ fn main() -> wasmtime::Result<()> {
}, },
); );
let (bindings, _) = Test::instantiate(&mut store, &component, &linker)?; let (bindings, _) = Test::instantiate(&mut store, &component, &linker)?;
bindings.call_greet(&mut store)?; bindings.call_greet(&mut store, &RecordString { hello_string: "Hello".to_owned() })?;
Ok(()) Ok(())
} }

View File

@@ -1,6 +1,10 @@
package my:project; package my:project;
world test { world test {
import hello: func(); record record-string {
export greet: func(); hello-string: string,
}
import hello: func(hello-string: record-string);
export greet: func(hello-string: record-string);
} }