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.
# It is not intended for manual editing.
version = 3
version = 4
[[package]]
name = "addr2line"
@@ -673,6 +673,13 @@ dependencies = [
"smallvec",
]
[[package]]
name = "runner"
version = "0.1.0"
dependencies = [
"wasmtime",
]
[[package]]
name = "rustc-demangle"
version = "0.1.24"
@@ -1157,13 +1164,6 @@ version = "20.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca6585868f5c427c3e9d2a8c0c3354e6d7d4518a0d17723ab25a0c1eebf5d5b4"
[[package]]
name = "wasmtime-test"
version = "0.1.0"
dependencies = [
"wasmtime",
]
[[package]]
name = "wasmtime-types"
version = "20.0.2"

View File

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

View File

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

View File

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

View File

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

View File

@@ -12,8 +12,8 @@ struct TestState {
}
impl TestImports for TestState {
fn hello(&mut self) -> std::result::Result<(), wasmtime::Error> {
println!("{}", self.name);
fn hello(&mut self, hello_string: RecordString) -> std::result::Result<(), wasmtime::Error> {
println!("{} - {}", self.name, hello_string.hello_string);
Ok(())
}
}
@@ -22,7 +22,7 @@ fn main() -> wasmtime::Result<()> {
let mut config = Config::new();
config.wasm_component_model(true);
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);
Test::add_to_linker(&mut linker, |state: &mut TestState| state)?;
let mut store = Store::new(
@@ -32,6 +32,6 @@ fn main() -> wasmtime::Result<()> {
},
);
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(())
}

View File

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