Lines
100 %
Functions
Branches
use super::*;
const NO_EXPORTS: &str = "";
/// Runs `alloc_value` for `value` inside a host fn of a module whose extra items are `wat`
/// and returns the refusal message.
async fn refusal(wat: &str, value: EvalValue) -> String {
let engine = build_engine(EngineOpts::baseline()).unwrap();
let wat = format!(
r#"(module (import "test" "encode" (func $encode))
{wat} (func (export "go") (call $encode)))"#
);
let module = compile_wat(&engine, &wat).unwrap();
let mut linker: Linker<Option<String>> = Linker::new(&engine);
linker
.func_wrap_async(
"test",
"encode",
move |mut caller: Caller<'_, Option<String>>, ()| {
let value = value.clone();
Box::new(async move {
let outcome = alloc_value(&mut caller, &value).await;
*caller.data_mut() = outcome.err().map(|err| err.to_string());
Ok(())
})
},
)
.unwrap();
let mut store = Store::new(&engine, None);
store.set_epoch_deadline(1_000);
let instance = linker.instantiate_async(&mut store, &module).await.unwrap();
let go = instance.get_func(&mut store, "go").unwrap();
go.call_async(&mut store, &[], &mut []).await.unwrap();
store.into_data().expect("alloc_value must refuse")
}
#[tokio::test(flavor = "current_thread")]
async fn bytes_have_no_host_built_form() {
let message = refusal(NO_EXPORTS, EvalValue::Bytes(vec![1])).await;
assert!(message.contains("bytes or a commodity"), "{message}");
async fn a_commodity_has_no_host_built_form() {
let commodity = EvalValue::Commodity {
numer: 1,
denom: 1,
commodity_hi: 0,
commodity_lo: 0,
};
let message = refusal(NO_EXPORTS, commodity).await;
async fn an_opaque_value_has_no_host_built_form() {
let message = refusal(NO_EXPORTS, EvalValue::Opaque).await;
assert!(message.contains("opaque value"), "{message}");
async fn an_index_beyond_31_bits_is_refused() {
let message = refusal(NO_EXPORTS, EvalValue::I32(i32::MAX)).await;
assert!(message.contains("exceeds 31 bits"), "{message}");
async fn t_needs_the_t_global_export() {
let message = refusal(NO_EXPORTS, EvalValue::T).await;
assert!(
message.contains("does not export the `t` global"),
"{message}"
async fn t_must_be_a_non_null_reference() {
let wat = r#"(global (export "t") anyref (ref.null any))"#;
let message = refusal(wat, EvalValue::T).await;
message.contains("not a non-null internal reference"),
async fn a_symbol_needs_the_symbol_new_export() {
let message = refusal(NO_EXPORTS, EvalValue::Symbol("A".into())).await;
message.contains("does not export `symbol_new`"),
async fn symbol_new_must_return_a_symbol() {
let wat = r#"(func (export "symbol_new") (param anyref) (result anyref)
(ref.null any))"#;
let message = refusal(wat, EvalValue::Symbol("A".into())).await;
message.contains("symbol_new returned no symbol"),