Add a wasmtime::component::bindgen! macro (#5317)
* Import Wasmtime support from the `wit-bindgen` repo This commit imports the `wit-bindgen-gen-host-wasmtime-rust` crate from the `wit-bindgen` repository into the upstream Wasmtime repository. I've chosen to not import the full history here since the crate is relatively small and doesn't have a ton of complexity. While the history of the crate is quite long the current iteration of the crate's history is relatively short so there's not a ton of import there anyway. The thinking is that this can now continue to evolve in-tree. * Refactor `wasmtime-component-macro` a bit Make room for a `wit_bindgen` macro to slot in. * Add initial support for a `bindgen` macro * Add tests for `wasmtime::component::bindgen!` * Improve error forgetting `async` feature * Add end-to-end tests for bindgen * Add an audit of `unicase` * Add a license to the test-helpers crate * Add vet entry for `pulldown-cmark` * Update publish script with new crate * Try to fix publish script * Update audits * Update lock file
This commit is contained in:
117
tests/all/component_model/bindgen.rs
Normal file
117
tests/all/component_model/bindgen.rs
Normal file
@@ -0,0 +1,117 @@
|
||||
use super::engine;
|
||||
use anyhow::Result;
|
||||
use wasmtime::{
|
||||
component::{Component, Linker},
|
||||
Store,
|
||||
};
|
||||
|
||||
mod no_imports {
|
||||
use super::*;
|
||||
|
||||
wasmtime::component::bindgen!({
|
||||
inline: "
|
||||
world no-imports {
|
||||
export foo: interface {
|
||||
foo: func()
|
||||
}
|
||||
|
||||
default export interface {
|
||||
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.bar(&mut store)?;
|
||||
no_imports.foo().foo(&mut store)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
mod one_import {
|
||||
use super::*;
|
||||
|
||||
wasmtime::component::bindgen!({
|
||||
inline: "
|
||||
world one-import {
|
||||
import foo: interface {
|
||||
foo: func()
|
||||
}
|
||||
|
||||
default export interface {
|
||||
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::Foo 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.bar(&mut store)?;
|
||||
assert!(store.data().hit);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user