From f4f736669bbda39952951666158a290d50f24b86 Mon Sep 17 00:00:00 2001 From: vato007 Date: Wed, 15 Jan 2025 21:37:46 +1030 Subject: [PATCH] Add example of a resource with hidden immutable data --- component/src/lib.rs | 5 +++-- component/wit/test.wit | 13 ++++++++++-- runner/src/main.rs | 47 ++++++++++++++++++++++++++++++++++-------- runner/wit/test.wit | 13 ++++++++++-- 4 files changed, 63 insertions(+), 15 deletions(-) diff --git a/component/src/lib.rs b/component/src/lib.rs index 97b2b54..e0b6052 100644 --- a/component/src/lib.rs +++ b/component/src/lib.rs @@ -1,14 +1,15 @@ #[allow(warnings)] mod bindings; -use bindings::{hello, Guest}; +use bindings::{hello, Guest, Testresource}; use crate::bindings::RecordString; struct Component; impl Guest for Component { - fn greet(hello_string: RecordString) { + fn greet(hello_string: RecordString, some_resource: Testresource) { hello(&hello_string); + some_resource.hello(); } } diff --git a/component/wit/test.wit b/component/wit/test.wit index 6bbb02f..f753715 100644 --- a/component/wit/test.wit +++ b/component/wit/test.wit @@ -1,10 +1,19 @@ package my:project; -world test { +interface types { + resource testresource { + hello: func(); + } + + record record-string { hello-string: string, } +} + +world test { + use types.{testresource, record-string}; import hello: func(hello-string: record-string); - export greet: func(hello-string: record-string); + export greet: func(hello-string: record-string, input-resource: testresource); } \ No newline at end of file diff --git a/runner/src/main.rs b/runner/src/main.rs index a1bb827..349dd6b 100644 --- a/runner/src/main.rs +++ b/runner/src/main.rs @@ -1,14 +1,25 @@ +use my::project::types::Host; use wasmtime::{ - component::{bindgen, Component, Linker}, + component::{bindgen, Component, Linker, ResourceTable}, Config, Engine, Store, }; +use crate::my::project::types::HostTestresource; // Defaults to 'wit' folder adjacent to cargo.toml. // Change folder by using: bindgen!(in "other/with/folder") -bindgen!(); +bindgen!({ + with: { + "my:project/types/testresource": TestResourceData, + } +}); + +pub struct TestResourceData { + some_data: String, +} struct TestState { name: String, + resources: ResourceTable, } impl TestImports for TestState { @@ -18,20 +29,38 @@ impl TestImports for TestState { } } +impl HostTestresource for TestState { + + fn hello(&mut self,self_:wasmtime::component::Resource,) -> wasmtime::Result<()> { + println!("HostTestResource - {} - {}", self.name, self.resources.get(&self_).unwrap().some_data); + Ok(()) + } + + fn drop(&mut self,rep:wasmtime::component::Resource) -> wasmtime::Result<()> { + self.resources.delete(rep).unwrap(); + Ok(()) + } +} + +impl Host for TestState { + +} + fn main() -> wasmtime::Result<()> { - let mut config = Config::new(); - config.wasm_component_model(true); - let engine = Engine::new(&config)?; + let engine = Engine::new(Config::new().wasm_component_model(true))?; 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 test_state = TestState { + name: "Hello World".to_string(), + resources: ResourceTable::new(), + }; let mut store = Store::new( &engine, - TestState { - name: "Hello World".to_string(), - }, + test_state ); let (bindings, _) = Test::instantiate(&mut store, &component, &linker)?; - bindings.call_greet(&mut store, &RecordString { hello_string: "Hello".to_owned() })?; + let resource = store.data_mut().resources.push(TestResourceData{some_data: "I'm a component with hidden data".to_owned()}).unwrap(); + bindings.call_greet(&mut store, &RecordString { hello_string: "Hello".to_owned() }, resource)?; Ok(()) } diff --git a/runner/wit/test.wit b/runner/wit/test.wit index 6bbb02f..f753715 100644 --- a/runner/wit/test.wit +++ b/runner/wit/test.wit @@ -1,10 +1,19 @@ package my:project; -world test { +interface types { + resource testresource { + hello: func(); + } + + record record-string { hello-string: string, } +} + +world test { + use types.{testresource, record-string}; import hello: func(hello-string: record-string); - export greet: func(hello-string: record-string); + export greet: func(hello-string: record-string, input-resource: testresource); } \ No newline at end of file