feat: add a histrical wit-bindgen

This commit is contained in:
2023-01-01 00:25:48 +08:00
parent 01e8f5a959
commit aa50d63aec
419 changed files with 45283 additions and 1 deletions

View File

@@ -0,0 +1,7 @@
test-imports: func()
f1: func(s: string)
f2: func() -> string
// TODO: should re-enable when fixed
//f3: func(a: string, b:string, c: string) -> tuple<string, string, string>

View File

@@ -0,0 +1,75 @@
use anyhow::Context;
wit_bindgen_wasmtime::export!("../../tests/runtime/smw_strings/imports.wit");
#[derive(Default)]
pub struct Host {
pub f1_s: String,
pub f2_called: bool,
// pub f3_a: String,
// pub f3_b: String,
// pub f3_c: String,
}
impl imports::Imports for Host {
fn f1(&mut self, s: &str) {
self.f1_s = s.to_string();
}
fn f2(&mut self) -> String {
self.f2_called = true;
"36 chambers".into()
}
// fn f3(&mut self, a: &str, b: &str, c: &str) -> (String, String, String) {
// self.f3_a = a.into();
// self.f3_b = b.into();
// self.f3_c = c.into();
// (a.into(), b.into(), c.into())
// }
}
wit_bindgen_wasmtime::import!("../../tests/runtime/smw_strings/exports.wit");
fn run(wasm: &str) -> anyhow::Result<()> {
let (exports, mut store) = crate::instantiate_smw(
wasm,
|linker| imports::add_to_linker(linker, |cx| -> &mut Host { &mut cx.imports }),
|store, module, linker| {
exports::Exports::instantiate(store, module, linker, |cx| &mut cx.exports)
},
)?;
// Test that the import instance called the functions we made available with
// the expected arguments.
exports.test_imports(&mut store)?;
assert_eq!(store.data().imports.f1_s, "Hello, WIT!");
assert!(store.data().imports.f2_called, "JS should have called `f2`");
// assert_eq!(store.data().imports.f3_a, "");
// assert_eq!(store.data().imports.f3_b, "🚀");
// assert_eq!(store.data().imports.f3_c, "hello");
// Test that the export instance behaves as we expect it to.
exports
.f1(&mut store, "Hello, WIT!")
.context("calling the `f1` export should succeed")?;
let s = exports
.f2(&mut store)
.context("calling the `f2` export should succeed")?;
assert_eq!(s, "36 chambers");
// let (a, b, c) = exports
// .f3(&mut store, "", "🚀", "hello")
// .context("calling the `f3` export should succeed")?;
// assert_eq!(a, "");
// assert_eq!(b, "🚀");
// assert_eq!(c, "hello");
Ok(())
}

View File

@@ -0,0 +1,4 @@
f1: func(s: string)
f2: func() -> string
// TODO: should re-enable when fixed
//f3: func(a: string, b:string, c: string) -> tuple<string, string, string>

View File

@@ -0,0 +1,40 @@
import * as imports from "imports";
function assert(condition, message) {
if (!condition) {
throw new Error(message);
}
}
function assertEq(a, b) {
assert(a == b, `assertEq failed: ${a} != ${b}`);
}
export function test_imports() {
const { f1, f2 } = imports;
// const { f1, f2, f3 } = imports;
f1("Hello, WIT!");
const s = f2();
assertEq(s, "36 chambers");
// const [a, b, c] = f3("", "🚀", "hello");
// assertEq(a, "");
// assertEq(b, "🚀");
// assertEq(c, "hello");
}
export function f1(s) {
assertEq(s, "Hello, WIT!");
}
export function f2() {
return "36 chambers";
}
export function f3(a, b, c) {
assertEq(a, "");
assertEq(b, "🚀");
assertEq(c, "hello");
return [a, b, c];
}