feat: add a histrical wit-bindgen
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "wit-bindgen-gen-wasmtime"
|
||||
version = "0.1.0"
|
||||
authors = ["Alex Crichton <alex@alexcrichton.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
test = false
|
||||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
wit-bindgen-gen-core = { path = '../gen-core', version = '0.1.0' }
|
||||
wit-bindgen-gen-rust = { path = '../gen-rust', version = '0.1.0' }
|
||||
heck = "0.3"
|
||||
structopt = { version = "0.3", default-features = false, optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
anyhow = "1.0"
|
||||
test-helpers = { path = '../test-helpers', features = ['wit-bindgen-gen-wasmtime'] }
|
||||
wasmtime = "0.38.0"
|
||||
wasmtime-wasi = "0.38.0"
|
||||
wit-bindgen-wasmtime = { path = '../wasmtime', features = ['tracing', 'async'] }
|
||||
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
// this build script is currently only here so OUT_DIR is set for testing.
|
||||
}
|
||||
2217
__wasm/wit-bindgen-sample/wit-bindgen/crates/gen-wasmtime/src/lib.rs
Normal file
2217
__wasm/wit-bindgen-sample/wit-bindgen/crates/gen-wasmtime/src/lib.rs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,108 @@
|
||||
#![allow(dead_code, type_alias_bounds)]
|
||||
|
||||
fn main() {
|
||||
println!("compiled successfully!")
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
mod exports {
|
||||
test_helpers::codegen_wasmtime_export!(
|
||||
"*.wit"
|
||||
|
||||
// TODO: implement async support
|
||||
"!async-functions.wit"
|
||||
|
||||
// If you want to exclude a specific test you can include it here with
|
||||
// gitignore glob syntax:
|
||||
//
|
||||
// "!wasm.wit"
|
||||
// "!host.wit"
|
||||
//
|
||||
//
|
||||
// Similarly you can also just remove the `*.wit` glob and list tests
|
||||
// individually if you're debugging.
|
||||
);
|
||||
}
|
||||
|
||||
mod imports {
|
||||
test_helpers::codegen_wasmtime_import!(
|
||||
"*.wit"
|
||||
|
||||
// TODO: implement async support
|
||||
"!async-functions.wit"
|
||||
|
||||
// TODO: these use push/pull buffer which isn't implemented in the test
|
||||
// generator just yet
|
||||
"!wasi-next.wit"
|
||||
"!host.wit"
|
||||
);
|
||||
}
|
||||
|
||||
mod async_tests {
|
||||
mod not_async {
|
||||
wit_bindgen_wasmtime::export!({
|
||||
src["x"]: "foo: func()",
|
||||
async: ["bar"],
|
||||
});
|
||||
|
||||
struct Me;
|
||||
|
||||
impl x::X for Me {
|
||||
fn foo(&mut self) {}
|
||||
}
|
||||
}
|
||||
mod one_async {
|
||||
wit_bindgen_wasmtime::export!({
|
||||
src["x"]: "
|
||||
foo: func() -> list<u8>
|
||||
bar: func()
|
||||
",
|
||||
async: ["bar"],
|
||||
});
|
||||
|
||||
struct Me;
|
||||
|
||||
#[wit_bindgen_wasmtime::async_trait]
|
||||
impl x::X for Me {
|
||||
fn foo(&mut self) -> Vec<u8> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
async fn bar(&mut self) {}
|
||||
}
|
||||
}
|
||||
mod one_async_export {
|
||||
wit_bindgen_wasmtime::import!({
|
||||
src["x"]: "
|
||||
foo: func(x: list<string>)
|
||||
bar: func()
|
||||
",
|
||||
async: ["bar"],
|
||||
});
|
||||
}
|
||||
mod resource_with_none_async {
|
||||
wit_bindgen_wasmtime::export!({
|
||||
src["x"]: "
|
||||
resource y {
|
||||
z: func() -> string
|
||||
}
|
||||
",
|
||||
async: [],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
mod custom_errors {
|
||||
wit_bindgen_wasmtime::export!({
|
||||
src["x"]: "
|
||||
foo: func()
|
||||
bar: func() -> expected<unit, u32>
|
||||
enum errno {
|
||||
bad1,
|
||||
bad2,
|
||||
}
|
||||
baz: func() -> expected<u32, errno>
|
||||
",
|
||||
custom_error: true,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
use anyhow::{Context as _, Result};
|
||||
use wasmtime::{Config, Engine, Instance, Linker, Module, Store};
|
||||
|
||||
test_helpers::runtime_tests_wasmtime!();
|
||||
|
||||
fn default_config() -> Result<Config> {
|
||||
// Create an engine with caching enabled to assist with iteration in this
|
||||
// project.
|
||||
let mut config = Config::new();
|
||||
config.cache_config_load_default()?;
|
||||
config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable);
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
fn default_wasi() -> wasmtime_wasi::WasiCtx {
|
||||
wasmtime_wasi::sync::WasiCtxBuilder::new()
|
||||
.inherit_stdio()
|
||||
.build()
|
||||
}
|
||||
|
||||
struct Context<I, E> {
|
||||
wasi: wasmtime_wasi::WasiCtx,
|
||||
imports: I,
|
||||
exports: E,
|
||||
}
|
||||
|
||||
fn instantiate<I: Default, E: Default, T>(
|
||||
wasm: &str,
|
||||
add_imports: impl FnOnce(&mut Linker<Context<I, E>>) -> Result<()>,
|
||||
mk_exports: impl FnOnce(
|
||||
&mut Store<Context<I, E>>,
|
||||
&Module,
|
||||
&mut Linker<Context<I, E>>,
|
||||
) -> Result<(T, Instance)>,
|
||||
) -> Result<(T, Store<Context<I, E>>)> {
|
||||
let engine = Engine::new(&default_config()?)?;
|
||||
let module = Module::from_file(&engine, wasm)?;
|
||||
|
||||
let mut linker = Linker::new(&engine);
|
||||
add_imports(&mut linker)?;
|
||||
wasmtime_wasi::add_to_linker(&mut linker, |cx| &mut cx.wasi)?;
|
||||
|
||||
let mut store = Store::new(
|
||||
&engine,
|
||||
Context {
|
||||
wasi: default_wasi(),
|
||||
imports: I::default(),
|
||||
exports: E::default(),
|
||||
},
|
||||
);
|
||||
let (exports, _instance) = mk_exports(&mut store, &module, &mut linker)?;
|
||||
Ok((exports, store))
|
||||
}
|
||||
|
||||
// TODO: This function needs to be updated to use the component model once it's ready. See
|
||||
// https://github.com/bytecodealliance/wit-bindgen/issues/259 for details.
|
||||
//
|
||||
// Also, rename the ignore_host.rs files under the tests/runtime/smw_{functions|lists|strings} to host.rs and
|
||||
// remove the leading underscore from this function's name to re-enable the Spidermonkey tests.
|
||||
fn _instantiate_smw<I: Default, E: Default, T>(
|
||||
wasm: &str,
|
||||
add_imports: impl FnOnce(&mut Linker<Context<I, E>>) -> Result<()>,
|
||||
mk_exports: impl FnOnce(
|
||||
&mut Store<Context<I, E>>,
|
||||
&Module,
|
||||
&mut Linker<Context<I, E>>,
|
||||
) -> Result<(T, Instance)>,
|
||||
) -> Result<(T, Store<Context<I, E>>)> {
|
||||
let mut config = default_config()?;
|
||||
config.wasm_multi_memory(true);
|
||||
let engine = Engine::new(&config)?;
|
||||
|
||||
println!("reading wasms...");
|
||||
let wasm = std::fs::read(wasm).context(format!("failed to read {}", wasm))?;
|
||||
let smw = std::fs::read("../gen-spidermonkey/spidermonkey-wasm/spidermonkey.wasm")
|
||||
.context("failed to read `spidermonkey.wasm`")?;
|
||||
println!("compiling input wasm...");
|
||||
let module = Module::new(&engine, &wasm)?;
|
||||
println!("compiling spidermonkey.wasm...");
|
||||
let smw = Module::new(&engine, &smw)?;
|
||||
|
||||
let mut linker = Linker::new(&engine);
|
||||
add_imports(&mut linker)?;
|
||||
wasmtime_wasi::add_to_linker(&mut linker, |cx| &mut cx.wasi)?;
|
||||
|
||||
let mut store = Store::new(
|
||||
&engine,
|
||||
Context {
|
||||
wasi: default_wasi(),
|
||||
imports: I::default(),
|
||||
exports: E::default(),
|
||||
},
|
||||
);
|
||||
|
||||
println!("instantiating spidermonkey.wasm...");
|
||||
let _smw_instance = linker
|
||||
.instantiate(&mut store, &smw)
|
||||
.context("failed to instantiate `spidermonkey.wasm`")?;
|
||||
// TODO: replace this with a component model equivalent:
|
||||
// linker.define_name("spidermonkey", smw_instance)?;
|
||||
|
||||
println!("instantiating input wasm...");
|
||||
let (exports, instance) = mk_exports(&mut store, &module, &mut linker)?;
|
||||
|
||||
println!("running wizer.initialize");
|
||||
let init = instance.get_typed_func::<(), (), _>(&mut store, "wizer.initialize")?;
|
||||
init.call(&mut store, ())
|
||||
.context("failed to call wizer.initialize")?;
|
||||
Ok((exports, store))
|
||||
}
|
||||
Reference in New Issue
Block a user