feat: add a histrical wit-bindgen
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
test-imports: func()
|
||||
|
||||
f1: func()
|
||||
f2: func(a: u32)
|
||||
f3: func(a: u32, b: u32)
|
||||
|
||||
f4: func() -> u32
|
||||
|
||||
// TODO: shoudl re-enable when re-implemented
|
||||
//f5: func() -> tuple<u32, u32>
|
||||
//
|
||||
//f6: func(a: u32, b: u32, c: u32) -> tuple<u32, u32, u32>
|
||||
@@ -0,0 +1,127 @@
|
||||
use anyhow::Context;
|
||||
|
||||
wit_bindgen_wasmtime::export!("../../tests/runtime/smw_functions/imports.wit");
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Host {
|
||||
pub f1_called: bool,
|
||||
pub f2_arg: u32,
|
||||
pub f3_a: u32,
|
||||
pub f3_b: u32,
|
||||
pub f4_called: bool,
|
||||
// pub f5_called: bool,
|
||||
// pub f6_a: u32,
|
||||
// pub f6_b: u32,
|
||||
// pub f6_c: u32,
|
||||
}
|
||||
|
||||
impl imports::Imports for Host {
|
||||
fn f1(&mut self) {
|
||||
self.f1_called = true;
|
||||
}
|
||||
|
||||
fn f2(&mut self, arg: u32) {
|
||||
self.f2_arg = arg;
|
||||
}
|
||||
|
||||
fn f3(&mut self, a: u32, b: u32) {
|
||||
self.f3_a = a;
|
||||
self.f3_b = b;
|
||||
}
|
||||
|
||||
fn f4(&mut self) -> u32 {
|
||||
self.f4_called = true;
|
||||
1337
|
||||
}
|
||||
|
||||
// fn f5(&mut self) -> (u32, u32) {
|
||||
// self.f5_called = true;
|
||||
// (1, 2)
|
||||
// }
|
||||
|
||||
// fn f6(&mut self, a: u32, b: u32, c: u32) -> (u32, u32, u32) {
|
||||
// self.f6_a = a;
|
||||
// self.f6_b = b;
|
||||
// self.f6_c = c;
|
||||
// (a + 1, b + 1, c + 1)
|
||||
// }
|
||||
}
|
||||
|
||||
wit_bindgen_wasmtime::import!("../../tests/runtime/smw_functions/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!(
|
||||
store.data().imports.f1_called,
|
||||
"top-level JS imported and called `f1`",
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
store.data().imports.f2_arg,
|
||||
42,
|
||||
"f2 should have been called with 42",
|
||||
);
|
||||
|
||||
assert_eq!(store.data().imports.f3_a, 0);
|
||||
assert_eq!(store.data().imports.f3_b, u32::MAX);
|
||||
|
||||
assert!(
|
||||
store.data().imports.f4_called,
|
||||
"the top-level JS imported and called `f4`",
|
||||
);
|
||||
|
||||
// assert!(
|
||||
// store.data().imports.f5_called,
|
||||
// "the top-level JS imported and called `f5`"
|
||||
// );
|
||||
|
||||
// assert_eq!(store.data().imports.f6_a, 100);
|
||||
// assert_eq!(store.data().imports.f6_b, 200);
|
||||
// assert_eq!(store.data().imports.f6_c, 300);
|
||||
|
||||
// Test that the export instance behaves as we expect it to.
|
||||
|
||||
exports
|
||||
.f1(&mut store)
|
||||
.context("calling the `f1` export should succeed")?;
|
||||
|
||||
exports
|
||||
.f2(&mut store, 42)
|
||||
.context("calling the `f2` export should succeed")?;
|
||||
|
||||
exports
|
||||
.f3(&mut store, 0, u32::MAX)
|
||||
.context("calling the `f3` export should succeed")?;
|
||||
|
||||
let a = exports
|
||||
.f4(&mut store)
|
||||
.context("calling the `f4` export should succeed")?;
|
||||
assert_eq!(a, 1337);
|
||||
|
||||
// let (a, b) = exports
|
||||
// .f5(&mut store)
|
||||
// .context("calling the `f5` export should succeed")?;
|
||||
// assert_eq!(a, 1);
|
||||
// assert_eq!(b, 2);
|
||||
|
||||
// let (a, b, c) = exports
|
||||
// .f6(&mut store, 100, 200, 300)
|
||||
// .context("calling the `f6` export should succeed")?;
|
||||
// assert_eq!(a, 101);
|
||||
// assert_eq!(b, 201);
|
||||
// assert_eq!(c, 301);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
f1: func()
|
||||
f2: func(a: u32)
|
||||
f3: func(a: u32, b: u32)
|
||||
|
||||
f4: func() -> u32
|
||||
// TODO: should re-enable when re-implemented
|
||||
//f5: func() -> tuple<u32, u32>
|
||||
//
|
||||
//f6: func(a: u32, b: u32, c: u32) -> tuple<u32, u32, u32>
|
||||
@@ -0,0 +1,83 @@
|
||||
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, f3, f4, f5, f6 } = imports;
|
||||
const { f1, f2, f3, f4 } = imports;
|
||||
|
||||
//
|
||||
// Testing arguments.
|
||||
//
|
||||
|
||||
f1();
|
||||
|
||||
f2(42);
|
||||
|
||||
// Min and max `u32`.
|
||||
f3(0, 4294967295);
|
||||
|
||||
//
|
||||
// Testing returns.
|
||||
//
|
||||
|
||||
{
|
||||
const a = f4();
|
||||
assertEq(a, 1337);
|
||||
}
|
||||
|
||||
// {
|
||||
// const [a, b] = f5();
|
||||
// assertEq(a, 1);
|
||||
// assertEq(b, 2);
|
||||
// }
|
||||
|
||||
// {
|
||||
// const [a, b, c] = f6(100, 200, 300);
|
||||
// assertEq(a, 101);
|
||||
// assertEq(b, 201);
|
||||
// assertEq(c, 301);
|
||||
// }
|
||||
}
|
||||
|
||||
//
|
||||
// Testing arguments.
|
||||
//
|
||||
|
||||
export function f1() {}
|
||||
|
||||
export function f2(x) {
|
||||
assertEq(x, 42);
|
||||
}
|
||||
|
||||
export function f3(a, b) {
|
||||
assertEq(a, 0);
|
||||
assertEq(b, 4294967295);
|
||||
}
|
||||
|
||||
//
|
||||
// Testing returns.
|
||||
//
|
||||
|
||||
export function f4() {
|
||||
return 1337;
|
||||
}
|
||||
|
||||
export function f5() {
|
||||
return [1, 2];
|
||||
}
|
||||
|
||||
export function f6(a, b, c) {
|
||||
assertEq(a, 100);
|
||||
assertEq(b, 200);
|
||||
assertEq(c, 300);
|
||||
return [a + 1, b + 1, c + 1];
|
||||
}
|
||||
Reference in New Issue
Block a user