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

Function thread_through_borrow

tests/all/component_model/resources.rs:779–834  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

777
778#[test]
779fn thread_through_borrow() -> Result<()> {
780 let engine = super::engine();
781 let c = Component::new(
782 &engine,
783 r#"
784 (component
785 (import "t" (type $t (sub resource)))
786 (import "f" (func $f (param "x" (borrow $t))))
787
788 (core func $f (canon lower (func $f)))
789 (core func $drop (canon resource.drop $t))
790
791 (core module $m
792 (import "" "f" (func $f (param i32)))
793 (import "" "drop" (func $drop (param i32)))
794 (func (export "f2") (param i32)
795 (call $f (local.get 0))
796 (call $f (local.get 0))
797 (call $drop (local.get 0))
798 )
799 )
800 (core instance $i (instantiate $m
801 (with "" (instance
802 (export "f" (func $f))
803 (export "drop" (func $drop))
804 ))
805 ))
806
807 (func (export "f2") (param "x" (borrow $t))
808 (canon lift (core func $i "f2")))
809 )
810 "#,
811 )?;
812
813 struct MyType;
814
815 let mut store = Store::new(&engine, ());
816 let mut linker = Linker::new(&engine);
817 linker
818 .root()
819 .resource("t", ResourceType::host::<MyType>(), |_, _| Ok(()))?;
820 linker
821 .root()
822 .func_wrap("f", |_cx, (r,): (Resource<MyType>,)| {
823 assert!(!r.owned());
824 assert_eq!(r.rep(), 100);
825 Ok(())
826 })?;
827 let i = linker.instantiate(&mut store, &c)?;
828
829 let f = i.get_typed_func::<(&Resource<MyType>,), ()>(&mut store, "f2")?;
830
831 let resource = Resource::new_own(100);
832 f.call(&mut store, (&resource,))?;
833 Ok(())
834}
835
836#[test]

Callers

nothing calls this directly

Calls 8

OkFunction · 0.85
engineFunction · 0.50
newFunction · 0.50
resourceMethod · 0.45
rootMethod · 0.45
func_wrapMethod · 0.45
instantiateMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected