Files
wasmtime/tests/all/component_model/bindgen.rs
Dan Gohman c19b742d1c Change the name of wit-bindgen's host implementation traits. (#5890)
* Change the name of wit-bindgen's host implementation traits.

Instead of naming the host implementation trait something like
`wasi_filesystem::WasiFilesystem`, name it `wasi_filesystem::Host`, and
avoid using the identifier `Host` in other places.

This fixes a collision when generating bindings for the current
wasi-clock API, which contains an interface `wall-clock` which contains
a type `wall-clock`, which created a naming collision on the name
`WallClock`.

* Update tests to use the new trait name.

* Fix one more.

* Add the new test interface to the simple-wasi world.
2023-02-27 23:14:55 +00:00

116 lines
2.9 KiB
Rust

use super::engine;
use anyhow::Result;
use wasmtime::{
component::{Component, Linker},
Store,
};
mod results;
mod no_imports {
use super::*;
wasmtime::component::bindgen!({
inline: "
default world no-imports {
export foo: interface {
foo: func()
}
export bar: func()
}
",
});
#[test]
fn run() -> Result<()> {
let engine = engine();
let component = Component::new(
&engine,
r#"
(component
(core module $m
(func (export ""))
)
(core instance $i (instantiate $m))
(func $f (export "bar") (canon lift (core func $i "")))
(instance $i (export "foo" (func $f)))
(export "foo" (instance $i))
)
"#,
)?;
let linker = Linker::new(&engine);
let mut store = Store::new(&engine, ());
let (no_imports, _) = NoImports::instantiate(&mut store, &component, &linker)?;
no_imports.call_bar(&mut store)?;
no_imports.foo().call_foo(&mut store)?;
Ok(())
}
}
mod one_import {
use super::*;
wasmtime::component::bindgen!({
inline: "
default world one-import {
import foo: interface {
foo: func()
}
export bar: func()
}
",
});
#[test]
fn run() -> Result<()> {
let engine = engine();
let component = Component::new(
&engine,
r#"
(component
(import "foo" (instance $i
(export "foo" (func))
))
(core module $m
(import "" "" (func))
(export "" (func 0))
)
(core func $f (canon lower (func $i "foo")))
(core instance $i (instantiate $m
(with "" (instance (export "" (func $f))))
))
(func $f (export "bar") (canon lift (core func $i "")))
)
"#,
)?;
#[derive(Default)]
struct MyImports {
hit: bool,
}
impl foo::Host for MyImports {
fn foo(&mut self) -> Result<()> {
self.hit = true;
Ok(())
}
}
let mut linker = Linker::new(&engine);
foo::add_to_linker(&mut linker, |f: &mut MyImports| f)?;
let mut store = Store::new(&engine, MyImports::default());
let (one_import, _) = OneImport::instantiate(&mut store, &component, &linker)?;
one_import.call_bar(&mut store)?;
assert!(store.data().hit);
Ok(())
}
}