MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / variants

Function variants

tests/all/component_model/dynamic.rs:580–711  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

578
579#[test]
580fn variants() -> Result<()> {
581 let engine = super::engine();
582 let mut store = Store::new(&engine, ());
583
584 let fragment = r#"
585 (type $c' (record (field "D" bool) (field "E" u32)))
586 (export $c "c" (type $c'))
587 (type $Foo' (variant
588 (case "A" u32)
589 (case "B" float64)
590 (case "C" $c)
591 ))
592 "#;
593
594 let component = Component::new(
595 &engine,
596 make_echo_component_with_params(
597 fragment,
598 &[
599 Param(Type::U8, Some(0)),
600 Param(Type::I64, Some(8)),
601 Param(Type::I32, None),
602 ],
603 ),
604 )?;
605 let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
606 let func = instance.get_func(&mut store, "echo").unwrap();
607 let input = Val::Variant("B".into(), Some(Box::new(Val::Float64(3.14159265))));
608 let mut output = [Val::Bool(false)];
609 func.call(&mut store, &[input.clone()], &mut output)?;
610
611 assert_eq!(input, output[0]);
612
613 // Do it again, this time using case "C"
614
615 let component = Component::new(
616 &engine,
617 make_echo_component_with_params(
618 fragment,
619 &[
620 Param(Type::U8, Some(0)),
621 Param(Type::I64, Some(8)),
622 Param(Type::I32, Some(12)),
623 ],
624 ),
625 )?;
626 let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
627 let func = instance.get_func(&mut store, "echo").unwrap();
628 let input = Val::Variant(
629 "C".into(),
630 Some(Box::new(Val::Record(vec![
631 ("D".into(), Val::Bool(true)),
632 ("E".into(), Val::U32(314159265)),
633 ]))),
634 );
635 func.call(&mut store, &[input.clone()], &mut output)?;
636
637 assert_eq!(input, output[0]);

Callers

nothing calls this directly

Calls 13

ParamClass · 0.85
Float64Class · 0.85
OkFunction · 0.85
engineFunction · 0.50
newFunction · 0.50
VariantClass · 0.50
RecordClass · 0.50
instantiateMethod · 0.45
unwrapMethod · 0.45
get_funcMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected