Add example of a resource with hidden immutable data
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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<TestResourceData>,) -> wasmtime::Result<()> {
|
||||
println!("HostTestResource - {} - {}", self.name, self.resources.get(&self_).unwrap().some_data);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn drop(&mut self,rep:wasmtime::component::Resource<TestResourceData>) -> 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(())
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user